Skip to content

Commit

Permalink
otelgrpc: add custom attributes to the stats handler
Browse files Browse the repository at this point in the history
Fixes #3894
  • Loading branch information
inigohu committed Jul 10, 2024
1 parent c399f9a commit 4249103
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Support for stdoutlog exporter in `go.opentelemetry.io/contrib/config`. (#5850)
- Add macOS ARM64 platform to the compatibility testing suite. (#5868)
- Add custom attributes to the stats handler in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#5133)

### Removed

Expand Down
14 changes: 14 additions & 0 deletions instrumentation/google.golang.org/grpc/otelgrpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type config struct {
TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
SpanStartOptions []trace.SpanStartOption
Attributes []attribute.KeyValue

ReceivedEvent bool
SentEvent bool
Expand Down Expand Up @@ -257,3 +258,16 @@ func (o spanStartOption) apply(c *config) {
func WithSpanOptions(opts ...trace.SpanStartOption) Option {
return spanStartOption{opts}
}

type attributesOption struct{ a []attribute.KeyValue }

func (o attributesOption) apply(c *config) {
if o.a != nil {
c.Attributes = o.a
}
}

// WithAttributes returns an Option to add custom attributes to the spans and metrics.
func WithAttributes(a ...attribute.KeyValue) Option {
return attributesOption{a: a}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (h *serverHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) cont

name, attrs := internal.ParseFullMethod(info.FullMethodName)
attrs = append(attrs, RPCSystemGRPC)

attrs = append(attrs, h.config.Attributes...)

ctx, _ = h.tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
name,
Expand Down Expand Up @@ -98,6 +101,9 @@ func NewClientHandler(opts ...Option) stats.Handler {
func (h *clientHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
name, attrs := internal.ParseFullMethod(info.FullMethodName)
attrs = append(attrs, RPCSystemGRPC)

attrs = append(attrs, h.config.Attributes...)

ctx, _ = h.tracer.Start(
ctx,
name,
Expand Down
Loading

0 comments on commit 4249103

Please sign in to comment.