Skip to content

Commit 116dcde

Browse files
authored
bug(go): propagate telemetry labels from Reflection API to tracing (#2881)
1 parent 438ce08 commit 116dcde

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

go/genkit/reflection.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,10 @@ func handleRunAction(reg *registry.Registry) func(w http.ResponseWriter, r *http
265265
ctx := r.Context()
266266

267267
var body struct {
268-
Key string `json:"key"`
269-
Input json.RawMessage `json:"input"`
270-
Context json.RawMessage `json:"context"`
268+
Key string `json:"key"`
269+
Input json.RawMessage `json:"input"`
270+
Context json.RawMessage `json:"context"`
271+
TelemetryLabels json.RawMessage `json:"telemetryLabels"`
271272
}
272273
defer r.Body.Close()
273274
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
@@ -303,7 +304,7 @@ func handleRunAction(reg *registry.Registry) func(w http.ResponseWriter, r *http
303304
json.Unmarshal(body.Context, &contextMap)
304305
}
305306

306-
resp, err := runAction(ctx, reg, body.Key, body.Input, cb, contextMap)
307+
resp, err := runAction(ctx, reg, body.Key, body.Input, body.TelemetryLabels, cb, contextMap)
307308
if err != nil {
308309
if stream {
309310
reflectErr, err := json.Marshal(core.ToReflectionError(err))
@@ -379,7 +380,7 @@ type telemetry struct {
379380
TraceID string `json:"traceId"`
380381
}
381382

382-
func runAction(ctx context.Context, reg *registry.Registry, key string, input json.RawMessage, cb streamingCallback[json.RawMessage], runtimeContext map[string]any) (*runActionResponse, error) {
383+
func runAction(ctx context.Context, reg *registry.Registry, key string, input json.RawMessage, telemetryLabels json.RawMessage, cb streamingCallback[json.RawMessage], runtimeContext map[string]any) (*runActionResponse, error) {
383384
action := reg.LookupAction(key)
384385
if action == nil {
385386
return nil, core.NewError(core.NOT_FOUND, "action %q not found", key)
@@ -391,6 +392,17 @@ func runAction(ctx context.Context, reg *registry.Registry, key string, input js
391392
var traceID string
392393
output, err := tracing.RunInNewSpan(ctx, reg.TracingState(), "dev-run-action-wrapper", "", true, input, func(ctx context.Context, input json.RawMessage) (json.RawMessage, error) {
393394
tracing.SetCustomMetadataAttr(ctx, "genkit-dev-internal", "true")
395+
// Set telemetry labels from payload to span
396+
if telemetryLabels != nil {
397+
var telemetryAttributes map[string]string
398+
err := json.Unmarshal(telemetryLabels, &telemetryAttributes)
399+
if err != nil {
400+
return nil, core.NewError(core.INTERNAL, "Error unmarshalling telemetryLabels: %v", err)
401+
}
402+
for k, v := range telemetryAttributes {
403+
tracing.SetCustomMetadataAttr(ctx, k, v)
404+
}
405+
}
394406
traceID = trace.SpanContextFromContext(ctx).TraceID().String()
395407
return action.RunJSON(ctx, input, cb)
396408
})

go/genkit/reflection_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ func TestServeMux(t *testing.T) {
151151
wantStatus: http.StatusOK,
152152
wantResult: "2",
153153
},
154+
{
155+
name: "check telemetry labels",
156+
body: `{"key": "/custom/test/dec", "input": 3,"telemetryLabels":{"test_k":"test_v"}}`,
157+
wantStatus: http.StatusOK,
158+
wantResult: "2",
159+
},
154160
{
155161
name: "invalid action key",
156162
body: `{"key": "/custom/test/invalid", "input": 3}`,

0 commit comments

Comments
 (0)