Skip to content

Commit 529dc6e

Browse files
committed
go: address review feedback
Sets gen_ai.span.kind so spans classify as LLM calls, scopes WithContentCapture to prompt/completion only so model and usage survive it, and switches the grpc exporter to WithEndpointURL. Adds exporter tests for the ingest path and auth headers. Drops the unused Instrumentor interface and Tracer helper. Signed-off-by: pmady <[email protected]>
1 parent c7dad53 commit 529dc6e

7 files changed

Lines changed: 150 additions & 67 deletions

File tree

‎go/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353

5454
## Span Attributes
5555

56-
Spans follow the [OTel GenAI semantic conventions](https://opentelemetry.io/docs/specs/semconv/gen-ai/) — `gen_ai.system`, `gen_ai.request.model`, `gen_ai.usage.*` tokens, `gen_ai.response.*`, etc. Prompt and completion content is captured by default; disable with `WithContentCapture(false)` for PII-sensitive workloads.
56+
Spans follow the [OTel GenAI semantic conventions](https://opentelemetry.io/docs/specs/semconv/gen-ai/) — `gen_ai.system`, `gen_ai.request.model`, `gen_ai.usage.*` tokens, `gen_ai.response.*`, etc. Prompt and completion content is captured by default; disable with `WithContentCapture(false)` for PII-sensitive workloads. Only `gen_ai.prompt` and `gen_ai.completion` are gated by that toggle — model, token usage, response id and finish reasons are always recorded.
5757

5858
## Configuration
5959

‎go/traceai/config.go‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
)
1818

1919
const (
20-
envAPIKey = "FI_API_KEY"
21-
envSecretKey = "FI_SECRET_KEY"
22-
envBaseURL = "FI_BASE_URL"
23-
envGRPCURL = "FI_GRPC_URL"
24-
envProjectName = "FI_PROJECT_NAME"
20+
envAPIKey = "FI_API_KEY"
21+
envSecretKey = "FI_SECRET_KEY"
22+
envBaseURL = "FI_BASE_URL"
23+
envGRPCURL = "FI_GRPC_URL"
24+
envProjectName = "FI_PROJECT_NAME"
2525

26-
defaultBaseURL = "https://api.futureagi.com"
27-
defaultGRPCURL = "https://grpc.futureagi.com"
28-
defaultProjectName = "DEFAULT_PROJECT_NAME"
26+
defaultBaseURL = "https://api.futureagi.com"
27+
defaultGRPCURL = "https://grpc.futureagi.com"
28+
defaultProjectName = "DEFAULT_PROJECT_NAME"
2929

3030
headerAPIKey = "X-Api-Key"
3131
headerSecretKey = "X-Secret-Key"
@@ -41,14 +41,14 @@ const (
4141

4242
// Config holds the options for initializing a traceAI TracerProvider.
4343
type Config struct {
44-
ProjectName string
45-
Transport Transport
46-
BaseURL string
47-
GRPCURL string
48-
APIKey string
49-
SecretKey string
50-
BatchExport bool
51-
SetGlobal bool
44+
ProjectName string
45+
Transport Transport
46+
BaseURL string
47+
GRPCURL string
48+
APIKey string
49+
SecretKey string
50+
BatchExport bool
51+
SetGlobal bool
5252
ShutdownTimeout time.Duration
5353
}
5454

@@ -184,8 +184,9 @@ func newExporter(ctx context.Context, cfg Config) (sdktrace.SpanExporter, error)
184184

185185
switch cfg.Transport {
186186
case TransportGRPC:
187+
// WithEndpoint wants host:port, GRPCURL has a scheme
187188
return otlptracegrpc.New(ctx,
188-
otlptracegrpc.WithEndpoint(cfg.GRPCURL),
189+
otlptracegrpc.WithEndpointURL(cfg.GRPCURL),
189190
otlptracegrpc.WithHeaders(headers),
190191
)
191192
default:

‎go/traceai/config_test.go‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package traceai
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestDefaultConfigReadsEnv(t *testing.T) {
12+
t.Setenv(envProjectName, "my-project")
13+
t.Setenv(envBaseURL, "https://example.test")
14+
t.Setenv(envAPIKey, "key")
15+
t.Setenv(envSecretKey, "secret")
16+
17+
cfg := DefaultConfig()
18+
19+
if cfg.ProjectName != "my-project" {
20+
t.Errorf("ProjectName = %q, want my-project", cfg.ProjectName)
21+
}
22+
if cfg.BaseURL != "https://example.test" {
23+
t.Errorf("BaseURL = %q, want https://example.test", cfg.BaseURL)
24+
}
25+
if cfg.APIKey != "key" || cfg.SecretKey != "secret" {
26+
t.Errorf("credentials = %q/%q, want key/secret", cfg.APIKey, cfg.SecretKey)
27+
}
28+
if cfg.GRPCURL != defaultGRPCURL {
29+
t.Errorf("GRPCURL = %q, want the default %q", cfg.GRPCURL, defaultGRPCURL)
30+
}
31+
}
32+
33+
func TestRegisterHTTPWiring(t *testing.T) {
34+
type received struct {
35+
path string
36+
apiKey string
37+
secretKey string
38+
}
39+
got := make(chan received, 1)
40+
41+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42+
select {
43+
case got <- received{r.URL.Path, r.Header.Get(headerAPIKey), r.Header.Get(headerSecretKey)}:
44+
default:
45+
}
46+
w.WriteHeader(http.StatusOK)
47+
}))
48+
defer srv.Close()
49+
50+
provider, err := Register(
51+
WithBaseURL(srv.URL),
52+
WithCredentials("key", "secret"),
53+
WithBatchExport(false),
54+
WithSetGlobal(false),
55+
)
56+
if err != nil {
57+
t.Fatalf("Register: %v", err)
58+
}
59+
60+
_, span := provider.TracerProvider().Tracer("test").Start(context.Background(), "span")
61+
span.End()
62+
63+
if err := provider.Shutdown(context.Background()); err != nil {
64+
t.Fatalf("Shutdown: %v", err)
65+
}
66+
67+
select {
68+
case r := <-got:
69+
if r.path != "/tracer/v1/traces" {
70+
t.Errorf("path = %q, want /tracer/v1/traces", r.path)
71+
}
72+
if r.apiKey != "key" {
73+
t.Errorf("%s = %q, want key", headerAPIKey, r.apiKey)
74+
}
75+
if r.secretKey != "secret" {
76+
t.Errorf("%s = %q, want secret", headerSecretKey, r.secretKey)
77+
}
78+
case <-time.After(5 * time.Second):
79+
t.Fatal("exporter never reached the server")
80+
}
81+
}
82+
83+
// default GRPCURL has a scheme, WithEndpoint would choke on it
84+
func TestNewExporterGRPCAcceptsSchemeURL(t *testing.T) {
85+
cfg := DefaultConfig()
86+
cfg.Transport = TransportGRPC
87+
88+
exporter, err := newExporter(context.Background(), cfg)
89+
if err != nil {
90+
t.Fatalf("newExporter(grpc) with %q: %v", cfg.GRPCURL, err)
91+
}
92+
if err := exporter.Shutdown(context.Background()); err != nil {
93+
t.Errorf("Shutdown: %v", err)
94+
}
95+
}

‎go/traceai/instrumentor.go‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎go/traceai/semconv.go‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import "go.opentelemetry.io/otel/attribute"
55
// GenAI semantic convention keys.
66
// See https://opentelemetry.io/docs/specs/semconv/gen-ai/
77
const (
8-
AttrGenAISystem = attribute.Key("gen_ai.system")
9-
AttrGenAIRequestModel = attribute.Key("gen_ai.request.model")
10-
AttrGenAIResponseModel = attribute.Key("gen_ai.response.model")
11-
AttrGenAIOperationName = attribute.Key("gen_ai.operation.name")
8+
AttrGenAISystem = attribute.Key("gen_ai.system")
9+
AttrGenAIRequestModel = attribute.Key("gen_ai.request.model")
10+
AttrGenAIResponseModel = attribute.Key("gen_ai.response.model")
11+
AttrGenAIOperationName = attribute.Key("gen_ai.operation.name")
1212

13-
AttrGenAIRequestMaxTokens = attribute.Key("gen_ai.request.max_tokens")
14-
AttrGenAIRequestTemperature = attribute.Key("gen_ai.request.temperature")
15-
AttrGenAIRequestTopP = attribute.Key("gen_ai.request.top_p")
16-
AttrGenAIRequestStopSequences = attribute.Key("gen_ai.request.stop_sequences")
13+
AttrGenAIRequestMaxTokens = attribute.Key("gen_ai.request.max_tokens")
14+
AttrGenAIRequestTemperature = attribute.Key("gen_ai.request.temperature")
15+
AttrGenAIRequestTopP = attribute.Key("gen_ai.request.top_p")
1716

1817
AttrGenAIUsageInputTokens = attribute.Key("gen_ai.usage.input_tokens")
1918
AttrGenAIUsageOutputTokens = attribute.Key("gen_ai.usage.output_tokens")
@@ -23,17 +22,17 @@ const (
2322

2423
AttrGenAIPrompt = attribute.Key("gen_ai.prompt")
2524
AttrGenAICompletion = attribute.Key("gen_ai.completion")
26-
)
2725

28-
const (
29-
GenAISystemOpenAI = "openai"
30-
GenAISystemAnthropic = "anthropic"
31-
GenAISystemCohere = "cohere"
32-
GenAISystemGoogle = "google"
26+
// platform uses this to classify the span as an LLM call
27+
AttrGenAISpanKind = attribute.Key("gen_ai.span.kind")
3328
)
3429

30+
const SpanKindLLM = "LLM"
31+
32+
const GenAISystemOpenAI = "openai"
33+
3534
const (
3635
OpChat = "chat"
37-
OpCompletion = "completion"
38-
OpEmbedding = "embedding"
36+
OpCompletion = "text_completion"
37+
OpEmbedding = "embeddings"
3938
)

‎go/traceai_openai/openai.go‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
package traceai_openai
1212

1313
import (
14+
"bytes"
1415
"encoding/json"
1516
"io"
1617
"net/http"
1718
"strings"
18-
"time"
1919

2020
"github.com/future-agi/traceAI/go/traceai"
2121
"go.opentelemetry.io/otel"
@@ -60,24 +60,21 @@ func Middleware(opts ...Option) func(req *http.Request, next func(req *http.Requ
6060
span.SetAttributes(
6161
traceai.AttrGenAISystem.String(traceai.GenAISystemOpenAI),
6262
traceai.AttrGenAIOperationName.String(opName),
63+
traceai.AttrGenAISpanKind.String(traceai.SpanKindLLM),
6364
)
6465

65-
if req.Body != nil && cfg.captureContent {
66+
if req.Body != nil {
6667
bodyBytes, err := io.ReadAll(req.Body)
6768
if err == nil {
68-
req.Body = io.NopCloser(strings.NewReader(string(bodyBytes)))
69-
extractRequestAttributes(span, bodyBytes)
69+
req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
70+
extractRequestAttributes(span, bodyBytes, cfg.captureContent)
7071
}
7172
}
7273

7374
req = req.WithContext(ctx)
74-
start := time.Now()
7575

7676
resp, err := next(req)
7777

78-
span.SetAttributes(attribute.Float64("gen_ai.request.duration_ms",
79-
float64(time.Since(start).Milliseconds())))
80-
8178
if err != nil {
8279
span.RecordError(err)
8380
span.SetStatus(codes.Error, err.Error())
@@ -90,12 +87,12 @@ func Middleware(opts ...Option) func(req *http.Request, next func(req *http.Requ
9087
return resp, err
9188
}
9289

93-
// read response for token counts
94-
if resp != nil && resp.Body != nil && cfg.captureContent {
90+
// model and usage live in the body too, only the text is gated
91+
if resp != nil && resp.Body != nil {
9592
bodyBytes, readErr := io.ReadAll(resp.Body)
9693
if readErr == nil {
97-
resp.Body = io.NopCloser(strings.NewReader(string(bodyBytes)))
98-
extractResponseAttributes(span, bodyBytes)
94+
resp.Body = io.NopCloser(bytes.NewReader(bodyBytes))
95+
extractResponseAttributes(span, bodyBytes, cfg.captureContent)
9996
}
10097
}
10198

@@ -125,7 +122,7 @@ type chatRequest struct {
125122
TopP *float64 `json:"top_p,omitempty"`
126123
}
127124

128-
func extractRequestAttributes(span trace.Span, body []byte) {
125+
func extractRequestAttributes(span trace.Span, body []byte, captureContent bool) {
129126
var req chatRequest
130127
if err := json.Unmarshal(body, &req); err != nil {
131128
return
@@ -143,7 +140,7 @@ func extractRequestAttributes(span trace.Span, body []byte) {
143140
if req.TopP != nil {
144141
span.SetAttributes(traceai.AttrGenAIRequestTopP.Float64(*req.TopP))
145142
}
146-
if len(req.Messages) > 0 {
143+
if captureContent && len(req.Messages) > 0 {
147144
span.SetAttributes(traceai.AttrGenAIPrompt.String(string(req.Messages)))
148145
}
149146
}
@@ -163,7 +160,7 @@ type chatResponse struct {
163160
} `json:"usage"`
164161
}
165162

166-
func extractResponseAttributes(span trace.Span, body []byte) {
163+
func extractResponseAttributes(span trace.Span, body []byte, captureContent bool) {
167164
var resp chatResponse
168165
if err := json.Unmarshal(body, &resp); err != nil {
169166
return
@@ -196,7 +193,7 @@ func extractResponseAttributes(span trace.Span, body []byte) {
196193
if len(reasons) > 0 {
197194
span.SetAttributes(traceai.AttrGenAIResponseFinishReasons.StringSlice(reasons))
198195
}
199-
if len(completions) > 0 {
196+
if captureContent && len(completions) > 0 {
200197
span.SetAttributes(traceai.AttrGenAICompletion.String(strings.Join(completions, "\n")))
201198
}
202199
}

‎go/traceai_openai/openai_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func TestMiddleware_ChatCompletion(t *testing.T) {
6161
}
6262

6363
assertAttr(t, span.Attributes, "gen_ai.system", "openai")
64+
assertAttr(t, span.Attributes, "gen_ai.span.kind", "LLM")
6465
assertAttr(t, span.Attributes, "gen_ai.request.model", "gpt-4")
6566
assertAttr(t, span.Attributes, "gen_ai.response.model", "gpt-4")
6667
assertAttr(t, span.Attributes, "gen_ai.response.id", "chatcmpl-abc")
@@ -153,6 +154,13 @@ func TestMiddleware_ContentCaptureDisabled(t *testing.T) {
153154
t.Errorf("content capture disabled but found attribute %s", a.Key)
154155
}
155156
}
157+
158+
// model and usage aren't content, they stay
159+
assertAttr(t, spans[0].Attributes, "gen_ai.request.model", "gpt-4")
160+
assertAttr(t, spans[0].Attributes, "gen_ai.response.model", "gpt-4")
161+
assertAttr(t, spans[0].Attributes, "gen_ai.response.id", "chatcmpl-abc")
162+
assertIntAttr(t, spans[0].Attributes, "gen_ai.usage.input_tokens", 5)
163+
assertIntAttr(t, spans[0].Attributes, "gen_ai.usage.output_tokens", 3)
156164
}
157165

158166
func assertAttr(t *testing.T, attrs []attribute.KeyValue, key, want string) {

0 commit comments

Comments
 (0)