|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controlapi |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "go.opentelemetry.io/otel" |
| 22 | + "go.opentelemetry.io/otel/attribute" |
| 23 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 24 | + "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 25 | + |
| 26 | + "github.com/agent-substrate/substrate/internal/ateattr" |
| 27 | + "github.com/agent-substrate/substrate/pkg/proto/ateapipb" |
| 28 | +) |
| 29 | + |
| 30 | +// The functional harness registers no otelgrpc StatsHandler, so there is no |
| 31 | +// server span on the client path. These tests instead call the Service methods |
| 32 | +// in-process under a self-provided recording root span, which stands in for the |
| 33 | +// span the otelgrpc handler injects in production and exercises the same |
| 34 | +// trace.SpanFromContext(ctx).SetAttributes path. |
| 35 | +func installSpanRecorder(t *testing.T) *tracetest.SpanRecorder { |
| 36 | + t.Helper() |
| 37 | + sr := tracetest.NewSpanRecorder() |
| 38 | + tp := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
| 39 | + prev := otel.GetTracerProvider() |
| 40 | + otel.SetTracerProvider(tp) |
| 41 | + t.Cleanup(func() { otel.SetTracerProvider(prev) }) |
| 42 | + return sr |
| 43 | +} |
| 44 | + |
| 45 | +func rootSpanAttrs(t *testing.T, sr *tracetest.SpanRecorder, fn func(ctx context.Context)) map[attribute.Key]attribute.Value { |
| 46 | + t.Helper() |
| 47 | + ctx, root := otel.Tracer("test").Start(context.Background(), "root") |
| 48 | + fn(ctx) |
| 49 | + root.End() |
| 50 | + for _, s := range sr.Ended() { |
| 51 | + if s.Name() == "root" { |
| 52 | + m := make(map[attribute.Key]attribute.Value, len(s.Attributes())) |
| 53 | + for _, kv := range s.Attributes() { |
| 54 | + m[kv.Key] = kv.Value |
| 55 | + } |
| 56 | + return m |
| 57 | + } |
| 58 | + } |
| 59 | + t.Fatal("root span not recorded") |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func assertSpanStr(t *testing.T, attrs map[attribute.Key]attribute.Value, key attribute.Key, want string) { |
| 64 | + t.Helper() |
| 65 | + v, ok := attrs[key] |
| 66 | + if !ok { |
| 67 | + t.Errorf("missing %s", key) |
| 68 | + return |
| 69 | + } |
| 70 | + if v.AsString() != want { |
| 71 | + t.Errorf("%s = %q, want %q", key, v.AsString(), want) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestCreateActor_StampsFullSpanIdentity(t *testing.T) { |
| 76 | + ns := namespaceForTest("ns-span-create") |
| 77 | + tc := setupTest(t, ns) |
| 78 | + defer tc.cleanup() |
| 79 | + createTemplate(t, tc, ns) |
| 80 | + |
| 81 | + sr := installSpanRecorder(t) |
| 82 | + attrs := rootSpanAttrs(t, sr, func(ctx context.Context) { |
| 83 | + if _, err := tc.service.CreateActor(ctx, &ateapipb.CreateActorRequest{ |
| 84 | + ActorRef: &ateapipb.ActorRef{Atespace: testAtespace, Name: "id1"}, |
| 85 | + ActorTemplateNamespace: ns, |
| 86 | + ActorTemplateName: "tmpl1", |
| 87 | + }); err != nil { |
| 88 | + t.Fatalf("CreateActor: %v", err) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + assertSpanStr(t, attrs, ateattr.AtespaceKey, testAtespace) |
| 93 | + assertSpanStr(t, attrs, ateattr.ActorIDKey, "id1") |
| 94 | + assertSpanStr(t, attrs, ateattr.ActorTemplateNameKey, "tmpl1") |
| 95 | + assertSpanStr(t, attrs, ateattr.ActorTemplateNamespaceKey, ns) |
| 96 | + if v, ok := attrs[ateattr.ActorVersionKey]; !ok || v.Type() != attribute.INT64 || v.AsInt64() != 1 { |
| 97 | + t.Errorf("%s = %v, want int64 1", ateattr.ActorVersionKey, v.Emit()) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestDeleteActor_StampsRefSpanIdentity(t *testing.T) { |
| 102 | + ns := namespaceForTest("ns-span-delete") |
| 103 | + tc := setupTest(t, ns) |
| 104 | + defer tc.cleanup() |
| 105 | + createTemplate(t, tc, ns) |
| 106 | + if _, err := tc.service.CreateActor(context.Background(), &ateapipb.CreateActorRequest{ |
| 107 | + ActorRef: &ateapipb.ActorRef{Atespace: testAtespace, Name: "id1"}, |
| 108 | + ActorTemplateNamespace: ns, |
| 109 | + ActorTemplateName: "tmpl1", |
| 110 | + }); err != nil { |
| 111 | + t.Fatalf("seed CreateActor: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + sr := installSpanRecorder(t) |
| 115 | + attrs := rootSpanAttrs(t, sr, func(ctx context.Context) { |
| 116 | + if _, err := tc.service.DeleteActor(ctx, &ateapipb.DeleteActorRequest{ |
| 117 | + ActorRef: &ateapipb.ActorRef{Atespace: testAtespace, Name: "id1"}, |
| 118 | + }); err != nil { |
| 119 | + t.Fatalf("DeleteActor: %v", err) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + assertSpanStr(t, attrs, ateattr.AtespaceKey, testAtespace) |
| 124 | + assertSpanStr(t, attrs, ateattr.ActorIDKey, "id1") |
| 125 | +} |
| 126 | + |
| 127 | +// The early ref stamp must land on the span even when the operation fails, so a |
| 128 | +// failed resume is still attributable to who/where. |
| 129 | +func TestResumeActor_ErrorStillStampsRefSpanIdentity(t *testing.T) { |
| 130 | + ns := namespaceForTest("ns-span-resume-err") |
| 131 | + tc := setupTest(t, ns) |
| 132 | + defer tc.cleanup() |
| 133 | + |
| 134 | + sr := installSpanRecorder(t) |
| 135 | + attrs := rootSpanAttrs(t, sr, func(ctx context.Context) { |
| 136 | + if _, err := tc.service.ResumeActor(ctx, &ateapipb.ResumeActorRequest{ |
| 137 | + ActorRef: &ateapipb.ActorRef{Atespace: testAtespace, Name: "missing"}, |
| 138 | + }); err == nil { |
| 139 | + t.Fatal("expected error resuming missing actor") |
| 140 | + } |
| 141 | + }) |
| 142 | + |
| 143 | + assertSpanStr(t, attrs, ateattr.AtespaceKey, testAtespace) |
| 144 | + assertSpanStr(t, attrs, ateattr.ActorIDKey, "missing") |
| 145 | +} |
0 commit comments