From 8c25f7f9a254458fb4ef4a94e63bef41ae9d06fb Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Thu, 1 Feb 2024 12:06:39 -0800 Subject: [PATCH] [processor/groupbyattrs] move from OpenCensus to OpenTelemetry (#30764) Fixes #30763 --------- Signed-off-by: Alex Boten --- .../codeboten_rm-groupbyattrs-census.yaml | 27 ++ processor/filterprocessor/telemetry_test.go | 10 +- processor/groupbyattrsprocessor/factory.go | 34 ++- .../groupbyattrsprocessor/factory_test.go | 8 +- processor/groupbyattrsprocessor/go.mod | 5 +- processor/groupbyattrsprocessor/go.sum | 76 ------ processor/groupbyattrsprocessor/metrics.go | 184 ++++++++----- .../groupbyattrsprocessor/metrics_test.go | 22 -- processor/groupbyattrsprocessor/processor.go | 24 +- .../groupbyattrsprocessor/processor_test.go | 35 ++- .../groupbyattrsprocessor/telemetry_test.go | 258 ++++++++++++++++++ 11 files changed, 467 insertions(+), 216 deletions(-) create mode 100755 .chloggen/codeboten_rm-groupbyattrs-census.yaml create mode 100644 processor/groupbyattrsprocessor/telemetry_test.go diff --git a/.chloggen/codeboten_rm-groupbyattrs-census.yaml b/.chloggen/codeboten_rm-groupbyattrs-census.yaml new file mode 100755 index 000000000000..8f182b671588 --- /dev/null +++ b/.chloggen/codeboten_rm-groupbyattrs-census.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: groupbyattrsprocessor + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: move metrics from OpenCensus to OpenTelemetry + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [30763] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/processor/filterprocessor/telemetry_test.go b/processor/filterprocessor/telemetry_test.go index 054651549ab6..d18cb8c7ff8e 100644 --- a/processor/filterprocessor/telemetry_test.go +++ b/processor/filterprocessor/telemetry_test.go @@ -12,7 +12,7 @@ import ( "go.opentelemetry.io/collector/processor" "go.opentelemetry.io/collector/processor/processortest" "go.opentelemetry.io/otel/attribute" - sdkmetric "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/metric/metricdata" "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" @@ -20,8 +20,8 @@ import ( ) type testTelemetry struct { - reader *sdkmetric.ManualReader - meterProvider *sdkmetric.MeterProvider + reader *metric.ManualReader + meterProvider *metric.MeterProvider } type expectedMetrics struct { @@ -40,10 +40,10 @@ func telemetryTest(t *testing.T, name string, testFunc func(t *testing.T, tel te } func setupTelemetry() testTelemetry { - reader := sdkmetric.NewManualReader() + reader := metric.NewManualReader() return testTelemetry{ reader: reader, - meterProvider: sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)), + meterProvider: metric.NewMeterProvider(metric.WithReader(reader)), } } diff --git a/processor/groupbyattrsprocessor/factory.go b/processor/groupbyattrsprocessor/factory.go index 5fcaf18bc723..a9e654a4f580 100644 --- a/processor/groupbyattrsprocessor/factory.go +++ b/processor/groupbyattrsprocessor/factory.go @@ -5,9 +5,7 @@ package groupbyattrsprocessor // import "github.com/open-telemetry/opentelemetry import ( "context" - "sync" - "go.opencensus.io/stats/view" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/processor" @@ -21,15 +19,8 @@ var ( consumerCapabilities = consumer.Capabilities{MutatesData: true} ) -var once sync.Once - // NewFactory returns a new factory for the Filter processor. func NewFactory() processor.Factory { - once.Do(func() { - // TODO: as with other -contrib factories registering metrics, this is causing the error being ignored - _ = view.Register(metricViews()...) - }) - return processor.NewFactory( metadata.Type, createDefaultConfig, @@ -45,7 +36,7 @@ func createDefaultConfig() component.Config { } } -func createGroupByAttrsProcessor(logger *zap.Logger, attributes []string) *groupByAttrsProcessor { +func createGroupByAttrsProcessor(set processor.CreateSettings, attributes []string) (*groupByAttrsProcessor, error) { var nonEmptyAttributes []string presentAttributes := make(map[string]struct{}) @@ -53,7 +44,7 @@ func createGroupByAttrsProcessor(logger *zap.Logger, attributes []string) *group if str != "" { _, isPresent := presentAttributes[str] if isPresent { - logger.Warn("A grouping key is already present", zap.String("key", str)) + set.Logger.Warn("A grouping key is already present", zap.String("key", str)) } else { nonEmptyAttributes = append(nonEmptyAttributes, str) presentAttributes[str] = struct{}{} @@ -61,7 +52,11 @@ func createGroupByAttrsProcessor(logger *zap.Logger, attributes []string) *group } } - return &groupByAttrsProcessor{logger: logger, groupByKeys: nonEmptyAttributes} + it, err := newProcessorTelemetry(set.TelemetrySettings) + if err != nil { + return nil, err + } + return &groupByAttrsProcessor{logger: set.Logger, groupByKeys: nonEmptyAttributes, internalTelemetry: it}, nil } // createTracesProcessor creates a trace processor based on this config. @@ -72,7 +67,10 @@ func createTracesProcessor( nextConsumer consumer.Traces) (processor.Traces, error) { oCfg := cfg.(*Config) - gap := createGroupByAttrsProcessor(set.Logger, oCfg.GroupByKeys) + gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys) + if err != nil { + return nil, err + } return processorhelper.NewTracesProcessor( ctx, @@ -91,7 +89,10 @@ func createLogsProcessor( nextConsumer consumer.Logs) (processor.Logs, error) { oCfg := cfg.(*Config) - gap := createGroupByAttrsProcessor(set.Logger, oCfg.GroupByKeys) + gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys) + if err != nil { + return nil, err + } return processorhelper.NewLogsProcessor( ctx, @@ -110,7 +111,10 @@ func createMetricsProcessor( nextConsumer consumer.Metrics) (processor.Metrics, error) { oCfg := cfg.(*Config) - gap := createGroupByAttrsProcessor(set.Logger, oCfg.GroupByKeys) + gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys) + if err != nil { + return nil, err + } return processorhelper.NewMetricsProcessor( ctx, diff --git a/processor/groupbyattrsprocessor/factory_test.go b/processor/groupbyattrsprocessor/factory_test.go index 2146a521a552..01892cd50f7b 100644 --- a/processor/groupbyattrsprocessor/factory_test.go +++ b/processor/groupbyattrsprocessor/factory_test.go @@ -8,9 +8,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/processor/processortest" - "go.uber.org/zap" ) func TestDefaultConfiguration(t *testing.T) { @@ -41,12 +41,14 @@ func TestCreateTestProcessor(t *testing.T) { func TestNoKeys(t *testing.T) { // This is allowed since can be used for compacting data - gap := createGroupByAttrsProcessor(zap.NewNop(), []string{}) + gap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), []string{}) + require.NoError(t, err) assert.NotNil(t, gap) } func TestDuplicateKeys(t *testing.T) { - gbap := createGroupByAttrsProcessor(zap.NewNop(), []string{"foo", "foo", ""}) + gbap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), []string{"foo", "foo", ""}) + require.NoError(t, err) assert.NotNil(t, gbap) assert.EqualValues(t, []string{"foo"}, gbap.groupByKeys) } diff --git a/processor/groupbyattrsprocessor/go.mod b/processor/groupbyattrsprocessor/go.mod index 953987ab2152..21f8137ea1a8 100644 --- a/processor/groupbyattrsprocessor/go.mod +++ b/processor/groupbyattrsprocessor/go.mod @@ -6,13 +6,14 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.93.0 github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.93.0 github.com/stretchr/testify v1.8.4 - go.opencensus.io v0.24.0 go.opentelemetry.io/collector/component v0.93.1-0.20240130182548-89388addcc7f go.opentelemetry.io/collector/confmap v0.93.1-0.20240130182548-89388addcc7f go.opentelemetry.io/collector/consumer v0.93.1-0.20240130182548-89388addcc7f go.opentelemetry.io/collector/pdata v1.0.2-0.20240130181942-9c7177496fd5 go.opentelemetry.io/collector/processor v0.93.1-0.20240130182548-89388addcc7f + go.opentelemetry.io/otel v1.22.0 go.opentelemetry.io/otel/metric v1.22.0 + go.opentelemetry.io/otel/sdk/metric v1.22.0 go.opentelemetry.io/otel/trace v1.22.0 go.uber.org/goleak v1.3.0 go.uber.org/zap v1.26.0 @@ -42,10 +43,8 @@ require ( github.com/prometheus/procfs v0.12.0 // indirect go.opentelemetry.io/collector v0.93.1-0.20240130182548-89388addcc7f // indirect go.opentelemetry.io/collector/config/configtelemetry v0.93.1-0.20240130182548-89388addcc7f // indirect - go.opentelemetry.io/otel v1.22.0 // indirect go.opentelemetry.io/otel/exporters/prometheus v0.45.0 // indirect go.opentelemetry.io/otel/sdk v1.22.0 // indirect - go.opentelemetry.io/otel/sdk/metric v1.22.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/net v0.20.0 // indirect golang.org/x/sys v0.16.0 // indirect diff --git a/processor/groupbyattrsprocessor/go.sum b/processor/groupbyattrsprocessor/go.sum index 4555de25c8a6..fab34e91971a 100644 --- a/processor/groupbyattrsprocessor/go.sum +++ b/processor/groupbyattrsprocessor/go.sum @@ -1,19 +1,10 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= -github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -21,32 +12,12 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= -github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= @@ -74,7 +45,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y= @@ -83,18 +53,11 @@ github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= -go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/collector v0.93.1-0.20240130182548-89388addcc7f h1:NgQcLNPP/6YnIfNUkDkKu1uiRTXUa9kMtfpsKSVC4dY= go.opentelemetry.io/collector v0.93.1-0.20240130182548-89388addcc7f/go.mod h1:rEqeAsOy8Mu6HA6oMjGk7NrTb7958XmShSaapxFEeTs= go.opentelemetry.io/collector/component v0.93.1-0.20240130182548-89388addcc7f h1:DNFOx9vwV7g+lWz9htP/Biiwwp+oFifR+J2zKnQKV3U= @@ -130,30 +93,17 @@ go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -164,10 +114,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= @@ -175,37 +121,15 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 h1:Jyp0Hsi0bmHXG6k9eATXoYtjd6e2UzZ1SCn/wIupY14= google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:oQ5rr10WTTMvP4A36n8JpR1OrO1BEiV4f78CneXZxkA= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= -google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0= google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/processor/groupbyattrsprocessor/metrics.go b/processor/groupbyattrsprocessor/metrics.go index 72d51f418758..2f0237500dab 100644 --- a/processor/groupbyattrsprocessor/metrics.go +++ b/processor/groupbyattrsprocessor/metrics.go @@ -4,87 +4,123 @@ package groupbyattrsprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbyattrsprocessor" import ( - "go.opencensus.io/stats" - "go.opencensus.io/stats/view" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/processor/processorhelper" + "go.opentelemetry.io/otel/metric" "github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbyattrsprocessor/internal/metadata" ) -var ( - mNumGroupedSpans = stats.Int64("num_grouped_spans", "Number of spans that had attributes grouped", stats.UnitDimensionless) - mNumNonGroupedSpans = stats.Int64("num_non_grouped_spans", "Number of spans that did not have attributes grouped", stats.UnitDimensionless) - mDistSpanGroups = stats.Int64("span_groups", "Distribution of groups extracted for spans", stats.UnitDimensionless) +type internalTelemetry struct { + mNumGroupedSpans metric.Int64Counter + mNumNonGroupedSpans metric.Int64Counter + mDistSpanGroups metric.Int64Histogram - mNumGroupedLogs = stats.Int64("num_grouped_logs", "Number of logs that had attributes grouped", stats.UnitDimensionless) - mNumNonGroupedLogs = stats.Int64("num_non_grouped_logs", "Number of logs that did not have attributes grouped", stats.UnitDimensionless) - mDistLogGroups = stats.Int64("log_groups", "Distribution of groups extracted for logs", stats.UnitDimensionless) + mNumGroupedLogs metric.Int64Counter + mNumNonGroupedLogs metric.Int64Counter + mDistLogGroups metric.Int64Histogram - mNumGroupedMetrics = stats.Int64("num_grouped_metrics", "Number of metrics that had attributes grouped", stats.UnitDimensionless) - mNumNonGroupedMetrics = stats.Int64("num_non_grouped_metrics", "Number of metrics that did not have attributes grouped", stats.UnitDimensionless) - mDistMetricGroups = stats.Int64("metric_groups", "Distribution of groups extracted for metrics", stats.UnitDimensionless) -) + mNumGroupedMetrics metric.Int64Counter + mNumNonGroupedMetrics metric.Int64Counter + mDistMetricGroups metric.Int64Histogram +} + +func newProcessorTelemetry(set component.TelemetrySettings) (*internalTelemetry, error) { + it := internalTelemetry{} + + counter, err := metadata.Meter(set).Int64Counter( + processorhelper.BuildCustomMetricName(metadata.Type, "num_grouped_spans"), + metric.WithDescription("Number of spans that had attributes grouped"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } + it.mNumGroupedSpans = counter + + counter, err = metadata.Meter(set).Int64Counter( + processorhelper.BuildCustomMetricName(metadata.Type, "num_non_grouped_spans"), + metric.WithDescription("Number of spans that did not have attributes grouped"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } + + it.mNumNonGroupedSpans = counter + + histo, err := metadata.Meter(set).Int64Histogram( + processorhelper.BuildCustomMetricName(metadata.Type, "span_groups"), + metric.WithDescription("Distribution of groups extracted for spans"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } + + it.mDistSpanGroups = histo + + counter, err = metadata.Meter(set).Int64Counter( + processorhelper.BuildCustomMetricName(metadata.Type, "num_grouped_logs"), + metric.WithDescription("Number of logs that had attributes grouped"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } + it.mNumGroupedLogs = counter + + counter, err = metadata.Meter(set).Int64Counter( + processorhelper.BuildCustomMetricName(metadata.Type, "num_non_grouped_logs"), + metric.WithDescription("Number of logs that did not have attributes grouped"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } + + it.mNumNonGroupedLogs = counter + + histo, err = metadata.Meter(set).Int64Histogram( + processorhelper.BuildCustomMetricName(metadata.Type, "log_groups"), + metric.WithDescription("Distribution of groups extracted for logs"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } -// metricViews return the metrics views according to given telemetry level. -func metricViews() []*view.View { - distributionGroups := view.Distribution(1, 2, 5, 10, 20, 50, 100, 500, 2000) - - return []*view.View{ - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumGroupedSpans.Name()), - Measure: mNumGroupedSpans, - Description: mNumGroupedSpans.Description(), - Aggregation: view.Sum(), - }, - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumNonGroupedSpans.Name()), - Measure: mNumNonGroupedSpans, - Description: mNumNonGroupedSpans.Description(), - Aggregation: view.Sum(), - }, - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mDistSpanGroups.Name()), - Measure: mDistSpanGroups, - Description: mDistSpanGroups.Description(), - Aggregation: distributionGroups, - }, - - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumGroupedLogs.Name()), - Measure: mNumGroupedLogs, - Description: mNumGroupedLogs.Description(), - Aggregation: view.Sum(), - }, - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumNonGroupedLogs.Name()), - Measure: mNumNonGroupedLogs, - Description: mNumNonGroupedLogs.Description(), - Aggregation: view.Sum(), - }, - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mDistLogGroups.Name()), - Measure: mDistLogGroups, - Description: mDistLogGroups.Description(), - Aggregation: distributionGroups, - }, - - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumGroupedMetrics.Name()), - Measure: mNumGroupedMetrics, - Description: mNumGroupedMetrics.Description(), - Aggregation: view.Sum(), - }, - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumNonGroupedMetrics.Name()), - Measure: mNumNonGroupedMetrics, - Description: mNumNonGroupedMetrics.Description(), - Aggregation: view.Sum(), - }, - { - Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mDistMetricGroups.Name()), - Measure: mDistMetricGroups, - Description: mDistMetricGroups.Description(), - Aggregation: distributionGroups, - }, + it.mDistLogGroups = histo + + counter, err = metadata.Meter(set).Int64Counter( + processorhelper.BuildCustomMetricName(metadata.Type, "num_grouped_metrics"), + metric.WithDescription("Number of metrics that had attributes grouped"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } + it.mNumGroupedMetrics = counter + + counter, err = metadata.Meter(set).Int64Counter( + processorhelper.BuildCustomMetricName(metadata.Type, "num_non_grouped_metrics"), + metric.WithDescription("Number of metrics that did not have attributes grouped"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err } + it.mNumNonGroupedMetrics = counter + + histo, err = metadata.Meter(set).Int64Histogram( + processorhelper.BuildCustomMetricName(metadata.Type, "metric_groups"), + metric.WithDescription("Distribution of groups extracted for metrics"), + metric.WithUnit("1"), + ) + if err != nil { + return nil, err + } + it.mDistMetricGroups = histo + + return &it, nil } diff --git a/processor/groupbyattrsprocessor/metrics_test.go b/processor/groupbyattrsprocessor/metrics_test.go index 759baf88b6aa..9c5d46562be0 100644 --- a/processor/groupbyattrsprocessor/metrics_test.go +++ b/processor/groupbyattrsprocessor/metrics_test.go @@ -2,25 +2,3 @@ // SPDX-License-Identifier: Apache-2.0 package groupbyattrsprocessor - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestProcessorMetrics(t *testing.T) { - expectedViewNames := []string{ - "processor/groupbyattrs/num_grouped_spans", - "processor/groupbyattrs/num_non_grouped_spans", - "processor/groupbyattrs/span_groups", - "processor/groupbyattrs/num_grouped_logs", - "processor/groupbyattrs/num_non_grouped_logs", - "processor/groupbyattrs/log_groups", - } - - views := metricViews() - for i, viewName := range expectedViewNames { - assert.Equal(t, viewName, views[i].Name) - } -} diff --git a/processor/groupbyattrsprocessor/processor.go b/processor/groupbyattrsprocessor/processor.go index 9bcd6a9c587c..ee322f5b7f3c 100644 --- a/processor/groupbyattrsprocessor/processor.go +++ b/processor/groupbyattrsprocessor/processor.go @@ -6,7 +6,6 @@ package groupbyattrsprocessor // import "github.com/open-telemetry/opentelemetry import ( "context" - "go.opencensus.io/stats" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" "go.opentelemetry.io/collector/pdata/pmetric" @@ -15,8 +14,9 @@ import ( ) type groupByAttrsProcessor struct { - logger *zap.Logger - groupByKeys []string + logger *zap.Logger + groupByKeys []string + internalTelemetry *internalTelemetry } // ProcessTraces process traces and groups traces by attribute. @@ -35,12 +35,12 @@ func (gap *groupByAttrsProcessor) processTraces(ctx context.Context, td ptrace.T toBeGrouped, requiredAttributes := gap.extractGroupingAttributes(span.Attributes()) if toBeGrouped { - stats.Record(ctx, mNumGroupedSpans.M(1)) + gap.internalTelemetry.mNumGroupedSpans.Add(ctx, 1) // Some attributes are going to be moved from span to resource level, // so we can delete those on the record level deleteAttributes(requiredAttributes, span.Attributes()) } else { - stats.Record(ctx, mNumNonGroupedSpans.M(1)) + gap.internalTelemetry.mNumNonGroupedSpans.Add(ctx, 1) } // Lets combine the base resource attributes + the extracted (grouped) attributes @@ -53,7 +53,7 @@ func (gap *groupByAttrsProcessor) processTraces(ctx context.Context, td ptrace.T } // Copy the grouped data into output - stats.Record(ctx, mDistSpanGroups.M(int64(tg.traces.ResourceSpans().Len()))) + gap.internalTelemetry.mDistSpanGroups.Record(ctx, int64(tg.traces.ResourceSpans().Len())) return tg.traces, nil } @@ -73,12 +73,12 @@ func (gap *groupByAttrsProcessor) processLogs(ctx context.Context, ld plog.Logs) toBeGrouped, requiredAttributes := gap.extractGroupingAttributes(log.Attributes()) if toBeGrouped { - stats.Record(ctx, mNumGroupedLogs.M(1)) + gap.internalTelemetry.mNumGroupedLogs.Add(ctx, 1) // Some attributes are going to be moved from log record to resource level, // so we can delete those on the record level deleteAttributes(requiredAttributes, log.Attributes()) } else { - stats.Record(ctx, mNumNonGroupedLogs.M(1)) + gap.internalTelemetry.mNumNonGroupedLogs.Add(ctx, 1) } // Lets combine the base resource attributes + the extracted (grouped) attributes @@ -92,7 +92,7 @@ func (gap *groupByAttrsProcessor) processLogs(ctx context.Context, ld plog.Logs) } // Copy the grouped data into output - stats.Record(ctx, mDistLogGroups.M(int64(lg.logs.ResourceLogs().Len()))) + gap.internalTelemetry.mDistLogGroups.Record(ctx, int64(lg.logs.ResourceLogs().Len())) return lg.logs, nil } @@ -154,7 +154,7 @@ func (gap *groupByAttrsProcessor) processMetrics(ctx context.Context, md pmetric } } - stats.Record(ctx, mDistMetricGroups.M(int64(mg.metrics.ResourceMetrics().Len()))) + gap.internalTelemetry.mDistMetricGroups.Record(ctx, int64(mg.metrics.ResourceMetrics().Len())) return mg.metrics, nil } @@ -243,12 +243,12 @@ func (gap *groupByAttrsProcessor) getGroupedMetricsFromAttributes( toBeGrouped, requiredAttributes := gap.extractGroupingAttributes(attributes) if toBeGrouped { - stats.Record(ctx, mNumGroupedMetrics.M(1)) + gap.internalTelemetry.mNumGroupedMetrics.Add(ctx, 1) // These attributes are going to be moved from datapoint to resource level, // so we can delete those on the datapoint deleteAttributes(requiredAttributes, attributes) } else { - stats.Record(ctx, mNumNonGroupedMetrics.M(1)) + gap.internalTelemetry.mNumNonGroupedMetrics.Add(ctx, 1) } // Get the ResourceMetrics matching with these attributes diff --git a/processor/groupbyattrsprocessor/processor_test.go b/processor/groupbyattrsprocessor/processor_test.go index 924283aa7f8f..863b393841d7 100644 --- a/processor/groupbyattrsprocessor/processor_test.go +++ b/processor/groupbyattrsprocessor/processor_test.go @@ -12,11 +12,12 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" "go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/collector/pdata/ptrace" - "go.uber.org/zap" + "go.opentelemetry.io/collector/processor/processortest" ) var ( @@ -269,7 +270,9 @@ func TestComplexAttributeGrouping(t *testing.T) { inputMetrics := someComplexMetrics(tt.withResourceAttrIndex, tt.inputResourceCount, tt.inputInstrumentationLibraryCount, 2) inputHistogramMetrics := someComplexHistogramMetrics(tt.withResourceAttrIndex, tt.inputResourceCount, tt.inputInstrumentationLibraryCount, 2, 2) - gap := createGroupByAttrsProcessor(zap.NewNop(), tt.groupByKeys) + tel := setupTelemetry() + gap, err := createGroupByAttrsProcessor(tel.NewProcessorCreateSettings(), tt.groupByKeys) + require.NoError(t, err) processedLogs, err := gap.processLogs(context.Background(), inputLogs) assert.NoError(t, err) @@ -369,6 +372,22 @@ func TestComplexAttributeGrouping(t *testing.T) { } } + expected := expectedMetrics{} + if tt.shouldMoveCommonGroupedAttr { + expected.mDistLogGroups = int64(tt.outputResourceCount) + expected.mNumGroupedLogs = int64(tt.outputTotalRecordsCount) + + expected.mDistMetricGroups = int64(tt.outputResourceCount) + expected.mNumGroupedMetrics = 4 * int64(tt.outputTotalRecordsCount) + + expected.mDistSpanGroups = int64(tt.outputResourceCount) + expected.mNumGroupedSpans = int64(tt.outputTotalRecordsCount) + } else { + expected.mNumNonGroupedLogs = int64(tt.outputTotalRecordsCount) + expected.mNumNonGroupedMetrics = 4 * int64(tt.outputTotalRecordsCount) + expected.mNumNonGroupedSpans = int64(tt.outputTotalRecordsCount) + } + tel.assertMetrics(t, expected) }) } } @@ -416,7 +435,8 @@ func TestAttributeGrouping(t *testing.T) { histogramMetrics := someHistogramMetrics(attrMap, 1, tt.count) exponentialHistogramMetrics := someExponentialHistogramMetrics(attrMap, 1, tt.count) - gap := createGroupByAttrsProcessor(zap.NewNop(), tt.groupByKeys) + gap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), tt.groupByKeys) + require.NoError(t, err) expectedResource := prepareResource(attrMap, tt.groupByKeys) expectedAttributes := filterAttributeMap(attrMap, tt.nonGroupedKeys) @@ -727,7 +747,8 @@ func TestMetricAdvancedGrouping(t *testing.T) { datapoint.Attributes().PutStr("id", "eth0") // Perform the test - gap := createGroupByAttrsProcessor(zap.NewNop(), []string{"host.name"}) + gap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), []string{"host.name"}) + require.NoError(t, err) processedMetrics, err := gap.processMetrics(context.Background(), metrics) assert.NoError(t, err) @@ -811,7 +832,8 @@ func TestCompacting(t *testing.T) { assert.Equal(t, 100, logs.ResourceLogs().Len()) assert.Equal(t, 100, metrics.ResourceMetrics().Len()) - gap := createGroupByAttrsProcessor(zap.NewNop(), []string{}) + gap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), []string{}) + require.NoError(t, err) processedSpans, err := gap.processTraces(context.Background(), spans) assert.NoError(t, err) @@ -869,7 +891,8 @@ func BenchmarkCompacting(bb *testing.B) { for _, run := range runs { bb.Run(fmt.Sprintf("instrumentation_library_count=%d, spans_per_library_count=%d", run.ilCount, run.spanCount), func(b *testing.B) { spans := someSpans(attrMap, run.ilCount, run.spanCount) - gap := createGroupByAttrsProcessor(zap.NewNop(), []string{}) + gap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), []string{}) + require.NoError(b, err) b.ResetTimer() for n := 0; n < b.N; n++ { diff --git a/processor/groupbyattrsprocessor/telemetry_test.go b/processor/groupbyattrsprocessor/telemetry_test.go new file mode 100644 index 000000000000..f3e9ebb0c510 --- /dev/null +++ b/processor/groupbyattrsprocessor/telemetry_test.go @@ -0,0 +1,258 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package groupbyattrsprocessor + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component" + "go.opentelemetry.io/collector/processor" + "go.opentelemetry.io/collector/processor/processortest" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/sdk/metric" + "go.opentelemetry.io/otel/sdk/metric/metricdata" + "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" + + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbyattrsprocessor/internal/metadata" +) + +type testTelemetry struct { + reader *metric.ManualReader + meterProvider *metric.MeterProvider +} + +type expectedMetrics struct { + mNumGroupedSpans int64 + mNumNonGroupedSpans int64 + mDistSpanGroups int64 + + mNumGroupedLogs int64 + mNumNonGroupedLogs int64 + mDistLogGroups int64 + + mNumGroupedMetrics int64 + mNumNonGroupedMetrics int64 + mDistMetricGroups int64 +} + +func setupTelemetry() testTelemetry { + reader := metric.NewManualReader() + return testTelemetry{ + reader: reader, + meterProvider: metric.NewMeterProvider(metric.WithReader(reader)), + } +} + +func (tt *testTelemetry) assertMetrics(t *testing.T, expected expectedMetrics) { + var md metricdata.ResourceMetrics + require.NoError(t, tt.reader.Collect(context.Background(), &md)) + if expected.mNumGroupedLogs > 0 { + name := "processor/groupbyattrs/num_grouped_logs" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Number of logs that had attributes grouped", + Unit: "1", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Value: expected.mNumGroupedLogs, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp()) + } + if expected.mNumGroupedMetrics > 0 { + name := "processor/groupbyattrs/num_grouped_metrics" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Number of metrics that had attributes grouped", + Unit: "1", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Value: expected.mNumGroupedMetrics, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp()) + } + if expected.mNumGroupedSpans > 0 { + name := "processor/groupbyattrs/num_grouped_spans" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Number of spans that had attributes grouped", + Unit: "1", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Value: expected.mNumGroupedSpans, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp()) + } + if expected.mNumNonGroupedLogs > 0 { + name := "processor/groupbyattrs/num_non_grouped_logs" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Number of logs that did not have attributes grouped", + Unit: "1", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Value: expected.mNumNonGroupedLogs, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp()) + } + if expected.mNumNonGroupedMetrics > 0 { + name := "processor/groupbyattrs/num_non_grouped_metrics" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Number of metrics that did not have attributes grouped", + Unit: "1", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Value: expected.mNumNonGroupedMetrics, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp()) + } + if expected.mNumNonGroupedSpans > 0 { + name := "processor/groupbyattrs/num_non_grouped_spans" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Number of spans that did not have attributes grouped", + Unit: "1", + Data: metricdata.Sum[int64]{ + Temporality: metricdata.CumulativeTemporality, + IsMonotonic: true, + DataPoints: []metricdata.DataPoint[int64]{ + { + Value: expected.mNumNonGroupedSpans, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp()) + } + if expected.mDistLogGroups > 0 { + name := "processor/groupbyattrs/log_groups" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Distribution of groups extracted for logs", + Unit: "1", + Data: metricdata.Histogram[int64]{ + Temporality: metricdata.CumulativeTemporality, + DataPoints: []metricdata.HistogramDataPoint[int64]{ + { + Attributes: *attribute.EmptySet(), + Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}, + BucketCounts: []uint64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + Count: 1, + Min: metricdata.NewExtrema(expected.mDistLogGroups), + Max: metricdata.NewExtrema(expected.mDistLogGroups), + Sum: expected.mDistLogGroups, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreExemplars()) + } + if expected.mDistMetricGroups > 0 { + name := "processor/groupbyattrs/metric_groups" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Distribution of groups extracted for metrics", + Unit: "1", + Data: metricdata.Histogram[int64]{ + Temporality: metricdata.CumulativeTemporality, + DataPoints: []metricdata.HistogramDataPoint[int64]{ + { + Attributes: *attribute.EmptySet(), + Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}, + BucketCounts: []uint64{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + Count: 2, + Min: metricdata.NewExtrema(expected.mDistMetricGroups), + Max: metricdata.NewExtrema(expected.mDistMetricGroups), + Sum: 2 * expected.mDistMetricGroups, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreExemplars()) + } + if expected.mDistSpanGroups > 0 { + name := "processor/groupbyattrs/span_groups" + got := tt.getMetric(name, md) + want := metricdata.Metrics{ + Name: name, + Description: "Distribution of groups extracted for spans", + Unit: "1", + Data: metricdata.Histogram[int64]{ + Temporality: metricdata.CumulativeTemporality, + DataPoints: []metricdata.HistogramDataPoint[int64]{ + { + Attributes: *attribute.EmptySet(), + Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000}, + BucketCounts: []uint64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + Count: 1, + Min: metricdata.NewExtrema(expected.mDistSpanGroups), + Max: metricdata.NewExtrema(expected.mDistSpanGroups), + Sum: expected.mDistSpanGroups, + }, + }, + }, + } + metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreExemplars()) + } +} + +func (tt *testTelemetry) NewProcessorCreateSettings() processor.CreateSettings { + settings := processortest.NewNopCreateSettings() + settings.MeterProvider = tt.meterProvider + settings.ID = component.NewID(metadata.Type) + + return settings +} + +func (tt *testTelemetry) getMetric(name string, got metricdata.ResourceMetrics) metricdata.Metrics { + for _, sm := range got.ScopeMetrics { + for _, m := range sm.Metrics { + if m.Name == name { + return m + } + } + } + + return metricdata.Metrics{} +}