Skip to content

Commit

Permalink
Work around high cardinality metrics from otelcol receivers
Browse files Browse the repository at this point in the history
Fixes #4764
  • Loading branch information
glindstedt committed Aug 9, 2023
1 parent 0be02ad commit a1fc6a6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
53 changes: 53 additions & 0 deletions component/otelcol/internal/views/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package views

import (
semconv "go.opentelemetry.io/collector/semconv/v1.13.0"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric"
)

var (
grpcScope = "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
// grpcUnacceptableKeyValues is a list of high cardinality grpc attributes that should be filtered out.
grpcUnacceptableKeyValues = []attribute.KeyValue{
attribute.String(semconv.AttributeNetSockPeerAddr, ""),
attribute.String(semconv.AttributeNetSockPeerPort, ""),
attribute.String(semconv.AttributeNetSockPeerName, ""),
}

httpScope = "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
// httpUnacceptableKeyValues is a list of high cardinality http attributes that should be filtered out.
httpUnacceptableKeyValues = []attribute.KeyValue{
attribute.String(semconv.AttributeNetHostName, ""),
attribute.String(semconv.AttributeNetHostPort, ""),
attribute.String(semconv.AttributeNetSockPeerPort, ""),
attribute.String(semconv.AttributeNetSockPeerAddr, ""),
attribute.String(semconv.AttributeHTTPClientIP, ""),
}
)

func cardinalityFilter(kvs ...attribute.KeyValue) attribute.Filter {
filter := attribute.NewSet(kvs...)
return func(kv attribute.KeyValue) bool {
return !filter.HasValue(kv.Key)
}
}

// DropHighCardinalityServerAttributes drops certain high cardinality attributes from grpc/http server metrics
//
// This is a hopefully temporary fix to this upstream issue https://github.com/open-telemetry/opentelemetry-go-contrib/issues/3765
func DropHighCardinalityServerAttributes() []metric.View {
var views []metric.View

views = append(views, metric.NewView(
metric.Instrument{Scope: instrumentation.Scope{Name: grpcScope}},
metric.Stream{AttributeFilter: cardinalityFilter(grpcUnacceptableKeyValues...)}))

views = append(views, metric.NewView(
metric.Instrument{Scope: instrumentation.Scope{Name: httpScope}},
metric.Stream{AttributeFilter: cardinalityFilter(httpUnacceptableKeyValues...)},
))

return views
}
6 changes: 5 additions & 1 deletion component/otelcol/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/grafana/agent/component/otelcol/internal/fanoutconsumer"
"github.com/grafana/agent/component/otelcol/internal/lazycollector"
"github.com/grafana/agent/component/otelcol/internal/scheduler"
"github.com/grafana/agent/component/otelcol/internal/views"
"github.com/grafana/agent/pkg/build"
"github.com/grafana/agent/pkg/util/zapadapter"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -125,7 +126,10 @@ func (r *Receiver) Update(args component.Arguments) error {
Logger: zapadapter.New(r.opts.Logger),

TracerProvider: r.opts.Tracer,
MeterProvider: metric.NewMeterProvider(metric.WithReader(promExporter)),
MeterProvider: metric.NewMeterProvider(
metric.WithReader(promExporter),
metric.WithView(views.DropHighCardinalityServerAttributes()...),
),
},

BuildInfo: otelcomponent.BuildInfo{
Expand Down

0 comments on commit a1fc6a6

Please sign in to comment.