Skip to content

bug(go):Propagate telemetry labels from Reflection API to tracing #2881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions go/genkit/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,10 @@ func handleRunAction(reg *registry.Registry) func(w http.ResponseWriter, r *http
ctx := r.Context()

var body struct {
Key string `json:"key"`
Input json.RawMessage `json:"input"`
Context json.RawMessage `json:"context"`
Key string `json:"key"`
Input json.RawMessage `json:"input"`
Context json.RawMessage `json:"context"`
TelemetryLabels json.RawMessage `json:"telemetryLabels"`
}
defer r.Body.Close()
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
Expand Down Expand Up @@ -303,7 +304,7 @@ func handleRunAction(reg *registry.Registry) func(w http.ResponseWriter, r *http
json.Unmarshal(body.Context, &contextMap)
}

resp, err := runAction(ctx, reg, body.Key, body.Input, cb, contextMap)
resp, err := runAction(ctx, reg, body.Key, body.Input, body.TelemetryLabels, cb, contextMap)
if err != nil {
if stream {
reflectErr, err := json.Marshal(core.ToReflectionError(err))
Expand Down Expand Up @@ -379,7 +380,7 @@ type telemetry struct {
TraceID string `json:"traceId"`
}

func runAction(ctx context.Context, reg *registry.Registry, key string, input json.RawMessage, cb streamingCallback[json.RawMessage], runtimeContext map[string]any) (*runActionResponse, error) {
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) {
action := reg.LookupAction(key)
if action == nil {
return nil, core.NewError(core.NOT_FOUND, "action %q not found", key)
Expand All @@ -391,6 +392,17 @@ func runAction(ctx context.Context, reg *registry.Registry, key string, input js
var traceID string
output, err := tracing.RunInNewSpan(ctx, reg.TracingState(), "dev-run-action-wrapper", "", true, input, func(ctx context.Context, input json.RawMessage) (json.RawMessage, error) {
tracing.SetCustomMetadataAttr(ctx, "genkit-dev-internal", "true")
// Set telemetry labels from payload to span
if telemetryLabels != nil {
var telemetryAttributes map[string]string
err := json.Unmarshal(telemetryLabels, &telemetryAttributes)
if err != nil {
return nil, core.NewError(core.INTERNAL, "Error unmarshalling telemetryLabels: %v", err)
}
for k, v := range telemetryAttributes {
tracing.SetCustomMetadataAttr(ctx, k, v)
}
}
traceID = trace.SpanContextFromContext(ctx).TraceID().String()
return action.RunJSON(ctx, input, cb)
})
Expand Down
6 changes: 6 additions & 0 deletions go/genkit/reflection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func TestServeMux(t *testing.T) {
wantStatus: http.StatusOK,
wantResult: "2",
},
{
name: "check telemetry labels",
body: `{"key": "/custom/test/dec", "input": 3,"telemetryLabels":{"test_k":"test_v"}}`,
wantStatus: http.StatusOK,
wantResult: "2",
},
{
name: "invalid action key",
body: `{"key": "/custom/test/invalid", "input": 3}`,
Expand Down
Loading