Skip to content

Commit d8b00ff

Browse files
austindrenskirueianCopilot
authored
Set server.* attrs in more cases (#95)
* Set `server.*` attrs in more cases https://opentelemetry.io/docs/specs/semconv/registry/attributes/server/ Signed-off-by: Austin Drenski <[email protected]> * Keep server attrs separate from user-provided trace attrs Signed-off-by: Austin Drenski <[email protected]> * Update valkeyotel/trace.go Co-authored-by: Copilot <[email protected]> Signed-off-by: Rueian <[email protected]> --------- Signed-off-by: Austin Drenski <[email protected]> Signed-off-by: Rueian <[email protected]> Co-authored-by: Rueian <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 659155c commit d8b00ff

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

valkeyotel/metrics.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func NewClient(clientOption valkey.ClientOption, opts ...Option) (valkey.Client,
8585
return nil, err
8686
}
8787

88+
if len(clientOption.InitAddress) == 1 {
89+
oclient.sAttrs = serverAttrs(clientOption.InitAddress[0])
90+
}
91+
8892
if clientOption.DialCtxFn == nil {
8993
clientOption.DialCtxFn = defaultDialFn
9094
if clientOption.DialFn != nil {
@@ -136,6 +140,7 @@ func NewClient(clientOption valkey.ClientOption, opts ...Option) (valkey.Client,
136140

137141
func newClient(opts ...Option) (*otelclient, error) {
138142
cli := &otelclient{
143+
sAttrs: trace.WithAttributes(),
139144
tAttrs: trace.WithAttributes(),
140145
}
141146
for _, opt := range opts {
@@ -235,7 +240,7 @@ func defaultDialFn(ctx context.Context, dst string, dialer *net.Dialer, cfg *tls
235240
return dialer.DialContext(ctx, "tcp", dst)
236241
}
237242

238-
func serverAttrs(dst string) trace.SpanStartOption {
243+
func serverAttrs(dst string) trace.SpanStartEventOption {
239244
if addr, port, err := net.SplitHostPort(dst); err == nil {
240245
if port, err := strconv.Atoi(port); err == nil {
241246
return trace.WithAttributes(attribute.String("server.address", addr), attribute.Int("server.port", port))

valkeyotel/trace.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ type otelclient struct {
163163
meter metric.Meter
164164
cscMiss metric.Int64Counter
165165
cscHits metric.Int64Counter
166+
sAttrs trace.SpanStartEventOption
166167
tAttrs trace.SpanStartEventOption
167168
dbStmtFunc StatementFunc
168169
addOpts []metric.AddOption
@@ -269,6 +270,7 @@ func (o *otelclient) Dedicated(fn func(valkey.DedicatedClient) error) (err error
269270
return o.client.Dedicated(func(client valkey.DedicatedClient) error {
270271
return fn(&dedicated{
271272
client: client,
273+
sAttrs: o.sAttrs,
272274
tAttrs: o.tAttrs,
273275
tracer: o.tracer,
274276
dbStmtFunc: o.dbStmtFunc,
@@ -281,6 +283,7 @@ func (o *otelclient) Dedicate() (valkey.DedicatedClient, func()) {
281283
client, cancel := o.client.Dedicate()
282284
return &dedicated{
283285
client: client,
286+
sAttrs: o.sAttrs,
284287
tAttrs: o.tAttrs,
285288
tracer: o.tracer,
286289
dbStmtFunc: o.dbStmtFunc,
@@ -312,6 +315,7 @@ func (o *otelclient) Nodes() map[string]valkey.Client {
312315
cscHits: o.cscHits,
313316
addOpts: o.addOpts,
314317
recordOpts: o.recordOpts,
318+
sAttrs: serverAttrs(addr),
315319
tAttrs: o.tAttrs,
316320
histogramOption: o.histogramOption,
317321
dbStmtFunc: o.dbStmtFunc,
@@ -350,6 +354,7 @@ var _ valkey.DedicatedClient = (*dedicated)(nil)
350354
type dedicated struct {
351355
client valkey.DedicatedClient
352356
tracer trace.Tracer
357+
sAttrs trace.SpanStartEventOption
353358
tAttrs trace.SpanStartEventOption
354359
dbStmtFunc StatementFunc
355360
commandMetrics
@@ -489,23 +494,23 @@ func multiCacheableFirst(multi []valkey.CacheableTTL) string {
489494
}
490495

491496
func (o *otelclient) start(ctx context.Context, op string, size int) (context.Context, trace.Span) {
492-
return startSpan(o.tracer, ctx, op, size, o.tAttrs)
497+
return startSpan(o.tracer, ctx, op, size, o.sAttrs, o.tAttrs)
493498
}
494499

495500
func (o *otelclient) end(span trace.Span, err error) {
496501
endSpan(span, err)
497502
}
498503

499504
func (d *dedicated) start(ctx context.Context, op string, size int) (context.Context, trace.Span) {
500-
return startSpan(d.tracer, ctx, op, size, d.tAttrs)
505+
return startSpan(d.tracer, ctx, op, size, d.sAttrs, d.tAttrs)
501506
}
502507

503508
func (d *dedicated) end(span trace.Span, err error) {
504509
endSpan(span, err)
505510
}
506511

507-
func startSpan(tracer trace.Tracer, ctx context.Context, op string, size int, attrs trace.SpanStartEventOption) (context.Context, trace.Span) {
508-
return tracer.Start(ctx, op, kind, attr(op, size), attrs)
512+
func startSpan(tracer trace.Tracer, ctx context.Context, op string, size int, sAttrs trace.SpanStartEventOption, tAttrs trace.SpanStartEventOption) (context.Context, trace.Span) {
513+
return tracer.Start(ctx, op, kind, attr(op, size), sAttrs, tAttrs)
509514
}
510515

511516
func endSpan(span trace.Span, err error) {

valkeyotel/trace_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,8 @@ func validateTrace(t *testing.T, exp *tracetest.InMemoryExporter, op string, cod
297297
if name := exp.GetSpans().Snapshots()[0].Name(); name != op {
298298
t.Fatalf("unexpected span name %v", name)
299299
}
300-
if operation := exp.GetSpans().Snapshots()[0].Attributes()[1].Value.AsString(); operation != op {
301-
t.Fatalf("unexpected span name %v", operation)
302-
}
303-
customAttr := exp.GetSpans().Snapshots()[0].Attributes()[3]
304-
if string(customAttr.Key) != "any" || customAttr.Value.AsString() != "label" {
305-
t.Fatalf("unexpected custom attr %v", customAttr)
306-
}
300+
validateSpanHasAttribute(t, exp.GetSpans().Snapshots()[0], attribute.String("db.operation", op))
301+
validateSpanHasAttribute(t, exp.GetSpans().Snapshots()[0], attribute.String("any", "label"))
307302
if c := exp.GetSpans().Snapshots()[0].Status().Code; c != code {
308303
t.Fatalf("unexpected span status code %v", c)
309304
}

0 commit comments

Comments
 (0)