Skip to content

Commit b44dfc9

Browse files
authored
otelgrpc: Remove high cardinality metric attributes (#4322)
1 parent 2a5fe23 commit b44dfc9

File tree

3 files changed

+18
-47
lines changed

3 files changed

+18
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4646
- Upgrade dependencies of OpenTelemetry Go to use the new [`v1.19.0`/`v0.42.0`/`v0.0.7` release](https://github.com/open-telemetry/opentelemetry-go/releases/tag/v1.19.0).
4747
- Use `grpc.StatsHandler` for gRPC instrumentation in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc/example`. (#4325)
4848

49+
### Removed
50+
51+
- The `net.sock.peer.*` and `net.peer.*` high cardinality attributes are removed from the metrics generated by `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#4322)
52+
4953
## [1.19.0/0.44.0/0.13.0] - 2023-09-12
5054

5155
### Added

instrumentation/google.golang.org/grpc/otelgrpc/interceptor.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func UnaryClientInterceptor(opts ...Option) grpc.UnaryClientInterceptor {
8383
return invoker(ctx, method, req, reply, cc, callOpts...)
8484
}
8585

86-
name, attr := spanInfo(method, cc.Target())
86+
name, attr, _ := telemetryAttributes(method, cc.Target())
8787

8888
startOpts := append([]trace.SpanStartOption{
8989
trace.WithSpanKind(trace.SpanKindClient),
@@ -277,7 +277,7 @@ func StreamClientInterceptor(opts ...Option) grpc.StreamClientInterceptor {
277277
return streamer(ctx, desc, cc, method, callOpts...)
278278
}
279279

280-
name, attr := spanInfo(method, cc.Target())
280+
name, attr, _ := telemetryAttributes(method, cc.Target())
281281

282282
startOpts := append([]trace.SpanStartOption{
283283
trace.WithSpanKind(trace.SpanKindClient),
@@ -346,7 +346,7 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
346346
}
347347

348348
ctx = extract(ctx, cfg.Propagators)
349-
name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
349+
name, attr, metricAttrs := telemetryAttributes(info.FullMethod, peerFromCtx(ctx))
350350

351351
startOpts := append([]trace.SpanStartOption{
352352
trace.WithSpanKind(trace.SpanKindServer),
@@ -386,8 +386,8 @@ func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
386386
span.SetAttributes(grpcStatusCodeAttr)
387387

388388
elapsedTime := time.Since(before).Milliseconds()
389-
attr = append(attr, grpcStatusCodeAttr)
390-
cfg.rpcDuration.Record(ctx, float64(elapsedTime), metric.WithAttributes(attr...))
389+
metricAttrs = append(metricAttrs, grpcStatusCodeAttr)
390+
cfg.rpcDuration.Record(ctx, float64(elapsedTime), metric.WithAttributes(metricAttrs...))
391391

392392
return resp, err
393393
}
@@ -468,7 +468,7 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
468468
}
469469

470470
ctx = extract(ctx, cfg.Propagators)
471-
name, attr := spanInfo(info.FullMethod, peerFromCtx(ctx))
471+
name, attr, _ := telemetryAttributes(info.FullMethod, peerFromCtx(ctx))
472472

473473
startOpts := append([]trace.SpanStartOption{
474474
trace.WithSpanKind(trace.SpanKindServer),
@@ -498,17 +498,18 @@ func StreamServerInterceptor(opts ...Option) grpc.StreamServerInterceptor {
498498
}
499499
}
500500

501-
// spanInfo returns a span name and all appropriate attributes from the gRPC
502-
// method and peer address.
503-
func spanInfo(fullMethod, peerAddress string) (string, []attribute.KeyValue) {
504-
name, mAttrs := internal.ParseFullMethod(fullMethod)
501+
// telemetryAttributes returns a span name and span and metric attributes from
502+
// the gRPC method and peer address.
503+
func telemetryAttributes(fullMethod, peerAddress string) (string, []attribute.KeyValue, []attribute.KeyValue) {
504+
name, methodAttrs := internal.ParseFullMethod(fullMethod)
505505
peerAttrs := peerAttr(peerAddress)
506506

507-
attrs := make([]attribute.KeyValue, 0, 1+len(mAttrs)+len(peerAttrs))
507+
attrs := make([]attribute.KeyValue, 0, 1+len(methodAttrs)+len(peerAttrs))
508508
attrs = append(attrs, RPCSystemGRPC)
509-
attrs = append(attrs, mAttrs...)
509+
attrs = append(attrs, methodAttrs...)
510+
metricAttrs := attrs[:1+len(methodAttrs)]
510511
attrs = append(attrs, peerAttrs...)
511-
return name, attrs
512+
return name, attrs, metricAttrs
512513
}
513514

514515
// peerAttr returns attributes about the peer address.

instrumentation/google.golang.org/grpc/otelgrpc/test/grpc_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package test
1616

1717
import (
1818
"context"
19-
"fmt"
2019
"net"
2120
"strconv"
2221
"testing"
@@ -665,12 +664,6 @@ func checkUnaryServerRecords(t *testing.T, reader metric.Reader) {
665664
assert.NoError(t, err)
666665
require.Len(t, rm.ScopeMetrics, 1)
667666

668-
// TODO: Remove these #4322
669-
address, ok := findScopeMetricAttribute(rm.ScopeMetrics[0], semconv.NetSockPeerAddrKey)
670-
assert.True(t, ok)
671-
port, ok := findScopeMetricAttribute(rm.ScopeMetrics[0], semconv.NetSockPeerPortKey)
672-
assert.True(t, ok)
673-
674667
want := metricdata.ScopeMetrics{
675668
Scope: wantInstrumentationScope,
676669
Metrics: []metricdata.Metrics{
@@ -687,8 +680,6 @@ func checkUnaryServerRecords(t *testing.T, reader metric.Reader) {
687680
semconv.RPCService("grpc.testing.TestService"),
688681
otelgrpc.RPCSystemGRPC,
689682
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
690-
address,
691-
port,
692683
),
693684
},
694685
{
@@ -697,8 +688,6 @@ func checkUnaryServerRecords(t *testing.T, reader metric.Reader) {
697688
semconv.RPCService("grpc.testing.TestService"),
698689
otelgrpc.RPCSystemGRPC,
699690
otelgrpc.GRPCStatusCodeKey.Int64(int64(codes.OK)),
700-
address,
701-
port,
702691
),
703692
},
704693
},
@@ -718,26 +707,3 @@ func findAttribute(kvs []attribute.KeyValue, key attribute.Key) (attribute.KeyVa
718707
}
719708
return attribute.KeyValue{}, false
720709
}
721-
722-
func findScopeMetricAttribute(sm metricdata.ScopeMetrics, key attribute.Key) (attribute.KeyValue, bool) {
723-
for _, m := range sm.Metrics {
724-
// This only needs to cover data types used by the instrumentation.
725-
switch d := m.Data.(type) {
726-
case metricdata.Histogram[int64]:
727-
for _, dp := range d.DataPoints {
728-
if kv, ok := findAttribute(dp.Attributes.ToSlice(), key); ok {
729-
return kv, true
730-
}
731-
}
732-
case metricdata.Histogram[float64]:
733-
for _, dp := range d.DataPoints {
734-
if kv, ok := findAttribute(dp.Attributes.ToSlice(), key); ok {
735-
return kv, true
736-
}
737-
}
738-
default:
739-
panic(fmt.Sprintf("unexpected data type %T - name %s", d, m.Name))
740-
}
741-
}
742-
return attribute.KeyValue{}, false
743-
}

0 commit comments

Comments
 (0)