diff --git a/.chloggen/32669-sumconnector-sum-logic.yaml b/.chloggen/32669-sumconnector-sum-logic.yaml new file mode 100644 index 000000000000..e2b38e64b94a --- /dev/null +++ b/.chloggen/32669-sumconnector-sum-logic.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: 'sumconnector' + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "adds connector and summing logic along with tests" + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [32669] + +# (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: [user] diff --git a/connector/sumconnector/README.md b/connector/sumconnector/README.md index 105783c17e1a..9d6f39bcfd40 100644 --- a/connector/sumconnector/README.md +++ b/connector/sumconnector/README.md @@ -27,12 +27,12 @@ The `sum` connector can be used to sum attribute values from spans, span events, If you are not already familiar with connectors, you may find it helpful to first visit the [Connectors README](https://github.com/open-telemetry/opentelemetry-collector/blob/main/connector/README.md). -### Configuration - -#### Basic configuration +### Basic configuration This configuration will sum numerical values found within the attribute `attribute.with.numerical.value` of any log telemetry routed to the connector. It will then output a metric time series with the name `my.example.metric.name` with those summed values. +Note: Values found within an attribute will be converted into a float regardless of their original type before being summed and output as a metric value. Non-convertible strings will be dropped and not included. + ```yaml receivers: foo: @@ -58,15 +58,15 @@ service: The sum connector has three required configuration settings and numerous optional settings -- Telemetry type: Nested below the `sum:` connector declaration. Declared as `logs:` in the [Basic Example](#basic-configuration). +- Telemetry type: Nested below the `sum:` connector declaration. Declared as `logs:` in the [Basic Example](#basic-configuration). - Can be any of `spans`, `spanevents`, `metrics`, `datapoints`, or `logs`. - Metric name: Nested below the telemetry type; this is the metric name the sum connector will output summed values to. Declared as `my.example.metric.name` in the [Basic Example](#basic-configuration) - `source_attribute`: A specific attribute to search for within the source telemetry being fed to the connector. This attribute is where the connector will look for numerical values to sum into the output metric value. Declared as `attribute.with.numerical.value` in the [Basic Example](#basic-configuration) #### Optional Settings -- `conditions`: [OTTL syntax](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/ottl/LANGUAGE.md) can be used to provide conditions for processing incoming telemetry. Conditions are ORed together, so if any condition is met the attribute's value will be included in the resulting sum. -- `attributes`: Declaration of attributes to include. Any of these attributes found will generate a separate sum for each set of unique combination of attribute values and output as its own datapoint in the metric time series. +- `conditions`: [OTTL syntax](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/ottl/LANGUAGE.md) can be used to provide conditions for processing incoming telemetry. Conditions are ORed together, so if any condition is met the attribute's value will be included in the resulting sum. +- `attributes`: Declaration of attributes to include. Any of these attributes found will generate a separate sum for each set of unique combination of attribute values and output as its own datapoint in the metric time series. - `key`: (required for `attributes`) the attribute name to match against - `default_value`: (optional for `attributes`) a default value for the attribute when no matches are found. The `default_value` value can be of type string, integer, or float. diff --git a/connector/sumconnector/connector.go b/connector/sumconnector/connector.go index 22ed13dd5c02..e9800c877a44 100644 --- a/connector/sumconnector/connector.go +++ b/connector/sumconnector/connector.go @@ -5,9 +5,12 @@ package sumconnector // import "github.com/open-telemetry/opentelemetry-collecto import ( "context" + "errors" + "fmt" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer" + "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" "go.opentelemetry.io/collector/pdata/pmetric" "go.opentelemetry.io/collector/pdata/ptrace" @@ -38,22 +41,158 @@ func (c *sum) Capabilities() consumer.Capabilities { } func (c *sum) ConsumeTraces(ctx context.Context, td ptrace.Traces) error { + var multiError error sumMetrics := pmetric.NewMetrics() sumMetrics.ResourceMetrics().EnsureCapacity(td.ResourceSpans().Len()) + for i := 0; i < td.ResourceSpans().Len(); i++ { + resourceSpan := td.ResourceSpans().At(i) + spansSummer := newSummer[ottlspan.TransformContext](c.spansMetricDefs) + spanEventsSummer := newSummer[ottlspanevent.TransformContext](c.spanEventsMetricDefs) + for j := 0; j < resourceSpan.ScopeSpans().Len(); j++ { + scopeSpan := resourceSpan.ScopeSpans().At(j) + + for k := 0; k < scopeSpan.Spans().Len(); k++ { + span := scopeSpan.Spans().At(k) + sCtx := ottlspan.NewTransformContext(span, scopeSpan.Scope(), resourceSpan.Resource(), scopeSpan, resourceSpan) + multiError = errors.Join(multiError, spansSummer.update(ctx, span.Attributes(), sCtx)) + + for l := 0; l < span.Events().Len(); l++ { + event := span.Events().At(l) + eCtx := ottlspanevent.NewTransformContext(event, span, scopeSpan.Scope(), resourceSpan.Resource(), scopeSpan, resourceSpan) + multiError = errors.Join(multiError, spanEventsSummer.update(ctx, event.Attributes(), eCtx)) + } + } + } + + if len(spansSummer.sums)+len(spanEventsSummer.sums) == 0 { + continue // don't add an empty resource + } + + sumResource := sumMetrics.ResourceMetrics().AppendEmpty() + resourceSpan.Resource().Attributes().CopyTo(sumResource.Resource().Attributes()) + + sumResource.ScopeMetrics().EnsureCapacity(resourceSpan.ScopeSpans().Len()) + sumScope := sumResource.ScopeMetrics().AppendEmpty() + + spansSummer.appendMetricsTo(sumScope.Metrics()) + spanEventsSummer.appendMetricsTo(sumScope.Metrics()) + } + if multiError != nil { + return multiError + } return c.metricsConsumer.ConsumeMetrics(ctx, sumMetrics) } func (c *sum) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error { + var multiError error sumMetrics := pmetric.NewMetrics() sumMetrics.ResourceMetrics().EnsureCapacity(md.ResourceMetrics().Len()) + for i := 0; i < md.ResourceMetrics().Len(); i++ { + resourceMetric := md.ResourceMetrics().At(i) + metricsSummer := newSummer[ottlmetric.TransformContext](c.metricsMetricDefs) + dataPointsSummer := newSummer[ottldatapoint.TransformContext](c.dataPointsMetricDefs) + + for j := 0; j < resourceMetric.ScopeMetrics().Len(); j++ { + scopeMetrics := resourceMetric.ScopeMetrics().At(j) + + for k := 0; k < scopeMetrics.Metrics().Len(); k++ { + metric := scopeMetrics.Metrics().At(k) + mCtx := ottlmetric.NewTransformContext(metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource(), scopeMetrics, resourceMetric) + multiError = errors.Join(multiError, metricsSummer.update(ctx, pcommon.NewMap(), mCtx)) + + //exhaustive:enforce + // For metric types each must be handled in exactly the same way + // Switch case required because each type calls DataPoints() differently + switch metric.Type() { + case pmetric.MetricTypeGauge: + dps := metric.Gauge().DataPoints() + for i := 0; i < dps.Len(); i++ { + dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource(), scopeMetrics, resourceMetric) + multiError = errors.Join(multiError, dataPointsSummer.update(ctx, dps.At(i).Attributes(), dCtx)) + } + case pmetric.MetricTypeSum: + dps := metric.Sum().DataPoints() + for i := 0; i < dps.Len(); i++ { + dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource(), scopeMetrics, resourceMetric) + multiError = errors.Join(multiError, dataPointsSummer.update(ctx, dps.At(i).Attributes(), dCtx)) + } + case pmetric.MetricTypeSummary: + dps := metric.Summary().DataPoints() + for i := 0; i < dps.Len(); i++ { + dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource(), scopeMetrics, resourceMetric) + multiError = errors.Join(multiError, dataPointsSummer.update(ctx, dps.At(i).Attributes(), dCtx)) + } + case pmetric.MetricTypeHistogram: + dps := metric.Histogram().DataPoints() + for i := 0; i < dps.Len(); i++ { + dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource(), scopeMetrics, resourceMetric) + multiError = errors.Join(multiError, dataPointsSummer.update(ctx, dps.At(i).Attributes(), dCtx)) + } + case pmetric.MetricTypeExponentialHistogram: + dps := metric.ExponentialHistogram().DataPoints() + for i := 0; i < dps.Len(); i++ { + dCtx := ottldatapoint.NewTransformContext(dps.At(i), metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource(), scopeMetrics, resourceMetric) + multiError = errors.Join(multiError, dataPointsSummer.update(ctx, dps.At(i).Attributes(), dCtx)) + } + case pmetric.MetricTypeEmpty: + multiError = errors.Join(multiError, fmt.Errorf("metric %q: invalid metric type: %v", metric.Name(), metric.Type())) + } + } + } + if len(metricsSummer.sums)+len(dataPointsSummer.sums) == 0 { + continue // don't add an empty resource + } + + sumResource := sumMetrics.ResourceMetrics().AppendEmpty() + resourceMetric.Resource().Attributes().CopyTo(sumResource.Resource().Attributes()) + + sumResource.ScopeMetrics().EnsureCapacity(resourceMetric.ScopeMetrics().Len()) + sumScope := sumResource.ScopeMetrics().AppendEmpty() + + metricsSummer.appendMetricsTo(sumScope.Metrics()) + dataPointsSummer.appendMetricsTo(sumScope.Metrics()) + } + if multiError != nil { + return multiError + } return c.metricsConsumer.ConsumeMetrics(ctx, sumMetrics) } func (c *sum) ConsumeLogs(ctx context.Context, ld plog.Logs) error { + var multiError error sumMetrics := pmetric.NewMetrics() sumMetrics.ResourceMetrics().EnsureCapacity(ld.ResourceLogs().Len()) + for i := 0; i < ld.ResourceLogs().Len(); i++ { + resourceLog := ld.ResourceLogs().At(i) + summer := newSummer[ottllog.TransformContext](c.logsMetricDefs) + + for j := 0; j < resourceLog.ScopeLogs().Len(); j++ { + scopeLogs := resourceLog.ScopeLogs().At(j) + + for k := 0; k < scopeLogs.LogRecords().Len(); k++ { + logRecord := scopeLogs.LogRecords().At(k) + + lCtx := ottllog.NewTransformContext(logRecord, scopeLogs.Scope(), resourceLog.Resource(), scopeLogs, resourceLog) + multiError = errors.Join(multiError, summer.update(ctx, logRecord.Attributes(), lCtx)) + } + } + + if len(summer.sums) == 0 { + continue // don't add an empty resource + } + + sumResource := sumMetrics.ResourceMetrics().AppendEmpty() + resourceLog.Resource().Attributes().CopyTo(sumResource.Resource().Attributes()) + + sumResource.ScopeMetrics().EnsureCapacity(resourceLog.ScopeLogs().Len()) + sumScope := sumResource.ScopeMetrics().AppendEmpty() + summer.appendMetricsTo(sumScope.Metrics()) + } + if multiError != nil { + return multiError + } return c.metricsConsumer.ConsumeMetrics(ctx, sumMetrics) } diff --git a/connector/sumconnector/connector_test.go b/connector/sumconnector/connector_test.go new file mode 100644 index 000000000000..4c2d4438c692 --- /dev/null +++ b/connector/sumconnector/connector_test.go @@ -0,0 +1,271 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package sumconnector + +import ( + "context" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/component/componenttest" + "go.opentelemetry.io/collector/connector/connectortest" + "go.opentelemetry.io/collector/consumer/consumertest" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest" +) + +// The test input file has a repetitive structure: +// - There are four resources, each with four spans, each with four span events. +// - The four resources have the following sets of attributes: +// - resource.required: foo, resource.optional: bar +// - resource.required: foo, resource.optional: notbar +// - resource.required: notfoo +// - (no attributes) +// +// - The four spans on each resource have the following sets of attributes: +// - span.required: foo, span.optional: bar +// - span.required: foo, span.optional: notbar +// - span.required: notfoo +// - (no attributes) +// +// - The four span events on each span have the following sets of attributes: +// - event.required: foo, event.optional: bar +// - event.required: foo, event.optional: notbar +// - event.required: notfoo +// - (no attributes) +func TestTracesToMetrics(t *testing.T) { + testCases := []struct { + name string + cfg *Config + }{ + { + name: "zero_conditions", + cfg: &Config{ + Spans: map[string]MetricInfo{ + "trace.span.sum": { + Description: "The sum of beep values observed in spans.", + SourceAttribute: "beep", + }, + }, + SpanEvents: map[string]MetricInfo{ + "trace.span.event.sum": { + Description: "The sum of beep values observed in span events.", + SourceAttribute: "beep", + }, + }, + }, + }, + { + name: "one_condition", + cfg: &Config{ + Spans: map[string]MetricInfo{ + "span.sum.if": { + Description: "Span sum if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + }, + }, + }, + SpanEvents: map[string]MetricInfo{ + "spanevent.sum.if": { + Description: "Span event sum if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + }, + }, + }, + }, + }, + { + name: "one_attribute", + cfg: &Config{ + Spans: map[string]MetricInfo{ + "span.sum.by_attr": { + Description: "Span sum by attribute", + SourceAttribute: "beep", + Attributes: []AttributeConfig{ + { + Key: "span.required", + }, + }, + }, + }, + SpanEvents: map[string]MetricInfo{ + "spanevent.sum.by_attr": { + Description: "Span event sum by attribute", + SourceAttribute: "beep", + Attributes: []AttributeConfig{ + { + Key: "event.required", + }, + }, + }, + }, + }, + }, + { + name: "multiple_conditions", + cfg: &Config{ + Spans: map[string]MetricInfo{ + "span.sum.if": { + Description: "Span sum if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + `attributes["span.optional"] != nil`, + }, + }, + }, + SpanEvents: map[string]MetricInfo{ + "spanevent.sum.if": { + Description: "Span event sum if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + `attributes["event.optional"] != nil`, + }, + }, + }, + }, + }, + { + name: "multiple_attributes", + cfg: &Config{ + Spans: map[string]MetricInfo{ + "span.sum.by_attr": { + Description: "Span sum by attributes", + SourceAttribute: "beep", + Attributes: []AttributeConfig{ + { + Key: "span.required", + }, + { + Key: "span.optional", + }, + }, + }, + }, + SpanEvents: map[string]MetricInfo{ + "spanevent.sum.by_attr": { + Description: "Span event sum by attributes", + SourceAttribute: "beep", + Attributes: []AttributeConfig{ + { + Key: "event.required", + }, + { + Key: "event.optional", + }, + }, + }, + }, + }, + }, + { + name: "multiple_metrics", + cfg: &Config{ + Spans: map[string]MetricInfo{ + "span.sum.all": { + Description: "All spans sum", + SourceAttribute: "beep", + }, + "span.sum.if": { + Description: "Span sum if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + `attributes["span.optional"] != nil`, + }, + }, + }, + SpanEvents: map[string]MetricInfo{ + "spanevent.sum.all": { + Description: "All span events sum", + SourceAttribute: "beep", + }, + "spanevent.sum.if": { + Description: "Span event sum if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + `attributes["event.optional"] != nil`, + }, + }, + }, + }, + }, + { + name: "condition_and_attribute", + cfg: &Config{ + Spans: map[string]MetricInfo{ + "span.sum.if.by_attr": { + Description: "Span sum if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + }, + Attributes: []AttributeConfig{ + { + Key: "span.required", + }, + }, + }, + }, + SpanEvents: map[string]MetricInfo{ + "spanevent.sum.if.by_attr": { + Description: "Span event sum by attribute if ...", + SourceAttribute: "beep", + Conditions: []string{ + `resource.attributes["resource.optional"] != nil`, + }, + Attributes: []AttributeConfig{ + { + Key: "event.required", + }, + }, + }, + }, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + require.NoError(t, tc.cfg.Validate()) + factory := NewFactory() + sink := &consumertest.MetricsSink{} + conn, err := factory.CreateTracesToMetrics(context.Background(), + connectortest.NewNopSettings(), tc.cfg, sink) + require.NoError(t, err) + require.NotNil(t, conn) + assert.False(t, conn.Capabilities().MutatesData) + + require.NoError(t, conn.Start(context.Background(), componenttest.NewNopHost())) + defer func() { + assert.NoError(t, conn.Shutdown(context.Background())) + }() + + testSpans, err := golden.ReadTraces(filepath.Join("testdata", "traces", "input.yaml")) + assert.NoError(t, err) + assert.NoError(t, conn.ConsumeTraces(context.Background(), testSpans)) + + allMetrics := sink.AllMetrics() + assert.Len(t, allMetrics, 1) + + expected, err := golden.ReadMetrics(filepath.Join("testdata", "traces", tc.name+".yaml")) + assert.NoError(t, err) + assert.NoError(t, pmetrictest.CompareMetrics(expected, allMetrics[0], + pmetrictest.IgnoreTimestamp(), + pmetrictest.IgnoreResourceMetricsOrder(), + pmetrictest.IgnoreMetricsOrder(), + pmetrictest.IgnoreMetricFloatPrecision(3), + pmetrictest.IgnoreMetricDataPointsOrder())) + }) + } +} diff --git a/connector/sumconnector/go.mod b/connector/sumconnector/go.mod index 816ee84627d3..62619074a6d7 100644 --- a/connector/sumconnector/go.mod +++ b/connector/sumconnector/go.mod @@ -4,7 +4,10 @@ go 1.22.0 require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter v0.110.0 + github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.110.0 github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl v0.110.0 + github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.110.0 + github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.110.0 github.com/stretchr/testify v1.9.0 go.opentelemetry.io/collector/component v0.110.0 go.opentelemetry.io/collector/confmap v1.16.0 @@ -21,6 +24,7 @@ require ( github.com/alecthomas/participle/v2 v2.1.1 // indirect github.com/antchfx/xmlquery v1.4.1 // indirect github.com/antchfx/xpath v1.3.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/elastic/go-grok v0.3.1 // indirect github.com/elastic/lunes v0.1.0 // indirect diff --git a/connector/sumconnector/go.sum b/connector/sumconnector/go.sum index 29b74fc6858e..90ec98e1af59 100644 --- a/connector/sumconnector/go.sum +++ b/connector/sumconnector/go.sum @@ -8,6 +8,8 @@ github.com/antchfx/xmlquery v1.4.1 h1:YgpSwbeWvLp557YFTi8E3z6t6/hYjmFEtiEKbDfEbl github.com/antchfx/xmlquery v1.4.1/go.mod h1:lKezcT8ELGt8kW5L+ckFMTbgdR61/odpPgDv8Gvi1fI= github.com/antchfx/xpath v1.3.1 h1:PNbFuUqHwWl0xRjvUPjJ95Agbmdj2uzzIwmQKgu4oCk= github.com/antchfx/xpath v1.3.1/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 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= diff --git a/connector/sumconnector/sum.go b/connector/sumconnector/sum.go new file mode 100644 index 000000000000..5c89ed19e42e --- /dev/null +++ b/connector/sumconnector/sum.go @@ -0,0 +1,154 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package sumconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/sumconnector" + +import ( + "context" + "errors" + "fmt" + "strconv" + "time" + + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pmetric" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil" +) + +var noAttributes = [16]byte{} + +func newSummer[K any](metricDefs map[string]metricDef[K]) *summer[K] { + return &summer[K]{ + metricDefs: metricDefs, + sums: make(map[string]map[[16]byte]*attrSummer, len(metricDefs)), + timestamp: time.Now(), + } +} + +type summer[K any] struct { + metricDefs map[string]metricDef[K] + sums map[string]map[[16]byte]*attrSummer + timestamp time.Time +} + +type attrSummer struct { + attrs pcommon.Map + sum float64 +} + +func (c *summer[K]) update(ctx context.Context, attrs pcommon.Map, tCtx K) error { + var multiError error + for name, md := range c.metricDefs { + sourceAttribute := md.sourceAttr + sumAttrs := pcommon.NewMap() + var sumVal float64 + + // Get source attribute value + if sourceAttrVal, ok := attrs.Get(sourceAttribute); ok { + switch { + case sourceAttrVal.Str() != "": + sumVal, _ = strconv.ParseFloat(sourceAttrVal.Str(), 64) + case sourceAttrVal.Double() != 0: + sumVal = sourceAttrVal.Double() + case sourceAttrVal.Int() != 0: + sumVal = float64(sourceAttrVal.Int()) + } + } + + // Get attribute values to include otherwise use default value + for _, attr := range md.attrs { + if attrVal, ok := attrs.Get(attr.Key); ok { + switch { + case attrVal.Str() != "": + sumAttrs.PutStr(attr.Key, attrVal.Str()) + case attrVal.Double() != 0: + sumAttrs.PutStr(attr.Key, fmt.Sprintf("%v", attrVal.Double())) + case attrVal.Int() != 0: + sumAttrs.PutStr(attr.Key, fmt.Sprintf("%v", attrVal.Int())) + } + } else if attr.DefaultValue != nil { + switch v := attr.DefaultValue.(type) { + case string: + if v != "" { + sumAttrs.PutStr(attr.Key, v) + } + case int: + if v != 0 { + sumAttrs.PutInt(attr.Key, int64(v)) + } + case float64: + if v != 0 { + sumAttrs.PutDouble(attr.Key, float64(v)) + } + } + } + } + + // Missing necessary attributes + if sumAttrs.Len() != len(md.attrs) { + continue + } + + // Perform condition matching or not + if md.condition == nil { + multiError = errors.Join(multiError, c.increment(name, sumVal, sumAttrs)) + continue + } + + if match, err := md.condition.Eval(ctx, tCtx); err != nil { + multiError = errors.Join(multiError, err) + } else if match { + multiError = errors.Join(multiError, c.increment(name, sumVal, sumAttrs)) + } + } + return multiError +} + +func (c *summer[K]) increment(metricName string, sumVal float64, attrs pcommon.Map) error { + if _, ok := c.sums[metricName]; !ok { + c.sums[metricName] = make(map[[16]byte]*attrSummer) + } + + key := noAttributes + if attrs.Len() > 0 { + key = pdatautil.MapHash(attrs) + } + + if _, ok := c.sums[metricName][key]; !ok { + c.sums[metricName][key] = &attrSummer{attrs: attrs} + } + + for strings := range c.sums[metricName][key].attrs.AsRaw() { + if _, ok := c.sums[metricName][key].attrs.Get(strings); ok { + c.sums[metricName][key].sum += sumVal + } + } + + if attrs.Len() == 0 { + c.sums[metricName][key].sum += sumVal + } + + return nil +} + +func (c *summer[K]) appendMetricsTo(metricSlice pmetric.MetricSlice) { + for name, md := range c.metricDefs { + if len(c.sums[name]) == 0 { + continue + } + sumMetric := metricSlice.AppendEmpty() + sumMetric.SetName(name) + sumMetric.SetDescription(md.desc) + sum := sumMetric.SetEmptySum() + // The delta value is always positive, so a value accumulated downstream is monotonic + sum.SetIsMonotonic(true) + sum.SetAggregationTemporality(pmetric.AggregationTemporalityDelta) + for _, dpSum := range c.sums[name] { + dp := sum.DataPoints().AppendEmpty() + dpSum.attrs.CopyTo(dp.Attributes()) + dp.SetDoubleValue(dpSum.sum) + dp.SetTimestamp(pcommon.NewTimestampFromTime(c.timestamp)) + } + } +} diff --git a/connector/sumconnector/testdata/traces/condition_and_attribute.yaml b/connector/sumconnector/testdata/traces/condition_and_attribute.yaml new file mode 100644 index 000000000000..1c26091ef995 --- /dev/null +++ b/connector/sumconnector/testdata/traces/condition_and_attribute.yaml @@ -0,0 +1,95 @@ +resourceMetrics: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + attributes: + - key: span.required + value: + stringValue: foo + timeUnixNano: "1678392127929005000" + - asDouble: "3.1" + attributes: + - key: span.required + value: + stringValue: notfoo + timeUnixNano: "1678392127929005000" + isMonotonic: true + - description: Span event sum by attribute if ... + name: spanevent.sum.if.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15.6" + attributes: + - key: event.required + value: + stringValue: foo + timeUnixNano: "1678392127929006000" + - asDouble: "8.3" + attributes: + - key: event.required + value: + stringValue: notfoo + timeUnixNano: "1678392127929006000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + attributes: + - key: span.required + value: + stringValue: foo + timeUnixNano: "1678392127929018000" + - asDouble: "3.1" + attributes: + - key: span.required + value: + stringValue: notfoo + timeUnixNano: "1678392127929018000" + isMonotonic: true + - description: Span event sum by attribute if ... + name: spanevent.sum.if.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15.2" + attributes: + - key: event.required + value: + stringValue: foo + timeUnixNano: "1678392127929018000" + - asDouble: "8.1" + attributes: + - key: event.required + value: + stringValue: notfoo + timeUnixNano: "1678392127929018000" + isMonotonic: true + diff --git a/connector/sumconnector/testdata/traces/input.yaml b/connector/sumconnector/testdata/traces/input.yaml new file mode 100644 index 000000000000..b6d74c2a11ac --- /dev/null +++ b/connector/sumconnector/testdata/traces/input.yaml @@ -0,0 +1,852 @@ +resourceSpans: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeSpans: + - scope: {} + spans: + - name: span-with-attrs-foo-bar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2.1 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2.1 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2.1 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attrs-foo-notbar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + - key: beep + value: + intValue: 3 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + intValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + stringValue: "3" + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + stringValue: "2" + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attr-notfoo + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 3.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2.1 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2.1 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2.1 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-no-attrs + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 1.1 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 1.1 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2.1 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeSpans: + - scope: {} + spans: + - name: span-with-attrs-foo-bar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2.1 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2.1 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2.1 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attrs-foo-notbar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + - key: beep + value: + intValue: 3 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + intValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + stringValue: "3" + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attr-notfoo + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 3.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-no-attrs + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 1 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 1 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - resource: + attributes: + - key: resource.required + value: + stringValue: notfoo + scopeSpans: + - scope: {} + spans: + - name: span-with-attrs-foo-bar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attrs-foo-notbar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + - key: beep + value: + intValue: 3 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + intValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + stringValue: "3" + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attr-notfoo + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 3.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-no-attrs + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 1 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 1 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - resource: {} + scopeSpans: + - scope: {} + spans: + - name: span-with-attrs-foo-bar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attrs-foo-notbar + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + - key: beep + value: + intValue: 3 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + intValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + stringValue: "3" + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-attr-notfoo + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + - key: span.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 3.1 + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 2 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + - name: span-with-no-attrs + parentSpanId: "" + spanId: "" + startTimeUnixNano: "1581452772000000321" + status: {} + traceId: "" + attributes: + endTimeUnixNano: "1581452773000000789" + events: + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + - key: beep + value: + doubleValue: 1 + name: event-with-attrs-foo-bar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + - key: beep + value: + doubleValue: 1 + name: event-with-attrs-foo-notbar + timeUnixNano: "1581452773000000123" + - attributes: + - key: event.required + value: + stringValue: notfoo + - key: beep + value: + doubleValue: 2 + name: event-with-attr-notfoo + timeUnixNano: "1581452773000000123" + - name: event-with-no-attrs + timeUnixNano: "1581452773000000123" + + diff --git a/connector/sumconnector/testdata/traces/multiple_attributes.yaml b/connector/sumconnector/testdata/traces/multiple_attributes.yaml new file mode 100644 index 000000000000..7407a91f2eb5 --- /dev/null +++ b/connector/sumconnector/testdata/traces/multiple_attributes.yaml @@ -0,0 +1,227 @@ +resourceMetrics: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeMetrics: + - metrics: + - description: Span sum by attributes + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "6" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926637000" + - asDouble: "4.2" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + timeUnixNano: "1678392127926637000" + isMonotonic: true + - description: Span event sum by attributes + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "14.6" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + timeUnixNano: "1678392127926637000" + - asDouble: "16.6" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926637000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeMetrics: + - metrics: + - description: Span sum by attributes + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "4.2" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + timeUnixNano: "1678392127926647000" + - asDouble: "6" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926647000" + isMonotonic: true + - description: Span event sum by attributes + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "14.2" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + timeUnixNano: "1678392127926647000" + - asDouble: "16.2" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926647000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: notfoo + scopeMetrics: + - metrics: + - description: Span sum by attributes + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "4.2" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + timeUnixNano: "1678392127926654000" + - asDouble: "6" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926654000" + isMonotonic: true + - description: Span event sum by attributes + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "14" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + timeUnixNano: "1678392127926654000" + - asDouble: "16" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926654000" + isMonotonic: true + + - resource: {} + scopeMetrics: + - metrics: + - description: Span sum by attributes + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "4.2" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: bar + timeUnixNano: "1678392127926661000" + - asDouble: "6" + attributes: + - key: span.required + value: + stringValue: foo + - key: span.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926661000" + isMonotonic: true + - description: Span event sum by attributes + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "14" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: bar + timeUnixNano: "1678392127926661000" + - asDouble: "16" + attributes: + - key: event.required + value: + stringValue: foo + - key: event.optional + value: + stringValue: notbar + timeUnixNano: "1678392127926661000" + isMonotonic: true + diff --git a/connector/sumconnector/testdata/traces/multiple_conditions.yaml b/connector/sumconnector/testdata/traces/multiple_conditions.yaml new file mode 100644 index 000000000000..eea3335df200 --- /dev/null +++ b/connector/sumconnector/testdata/traces/multiple_conditions.yaml @@ -0,0 +1,99 @@ +resourceMetrics: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127923826000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.9" + timeUnixNano: "1678392127923826000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127923836000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.3" + timeUnixNano: "1678392127923836000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: notfoo + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + timeUnixNano: "1678392127923843000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15" + timeUnixNano: "1678392127923843000" + isMonotonic: true + + - resource: {} + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + timeUnixNano: "1678392127923849000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15" + timeUnixNano: "1678392127923849000" + isMonotonic: true + diff --git a/connector/sumconnector/testdata/traces/multiple_metrics.yaml b/connector/sumconnector/testdata/traces/multiple_metrics.yaml new file mode 100644 index 000000000000..c19731ff7fac --- /dev/null +++ b/connector/sumconnector/testdata/traces/multiple_metrics.yaml @@ -0,0 +1,163 @@ +resourceMetrics: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeMetrics: + - metrics: + - description: All spans sum + name: span.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127924753000" + isMonotonic: true + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127924753000" + isMonotonic: true + - description: All span events sum + name: spanevent.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.9" + timeUnixNano: "1678392127924753000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.9" + timeUnixNano: "1678392127924753000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeMetrics: + - metrics: + - description: All spans sum + name: span.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127924764000" + isMonotonic: true + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127924764000" + isMonotonic: true + - description: All span events sum + name: spanevent.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.3" + timeUnixNano: "1678392127924764000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.3" + timeUnixNano: "1678392127924764000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: notfoo + scopeMetrics: + - metrics: + - description: All spans sum + name: span.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127924772000" + isMonotonic: true + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + timeUnixNano: "1678392127924772000" + isMonotonic: true + - description: All span events sum + name: spanevent.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23" + timeUnixNano: "1678392127924772000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15" + timeUnixNano: "1678392127924772000" + isMonotonic: true + + - resource: {} + scopeMetrics: + - metrics: + - description: All spans sum + name: span.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127924780000" + isMonotonic: true + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + timeUnixNano: "1678392127924780000" + isMonotonic: true + - description: All span events sum + name: spanevent.sum.all + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23" + timeUnixNano: "1678392127924780000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15" + timeUnixNano: "1678392127924780000" + isMonotonic: true + diff --git a/connector/sumconnector/testdata/traces/one_attribute.yaml b/connector/sumconnector/testdata/traces/one_attribute.yaml new file mode 100644 index 000000000000..466f1558822e --- /dev/null +++ b/connector/sumconnector/testdata/traces/one_attribute.yaml @@ -0,0 +1,182 @@ +resourceMetrics: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeMetrics: + - metrics: + - description: Span sum by attribute + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + attributes: + - key: span.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "3.1" + attributes: + - key: span.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + - description: Span event sum by attribute + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15.6" + attributes: + - key: event.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "8.3" + attributes: + - key: event.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeMetrics: + - metrics: + - description: Span sum by attribute + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + attributes: + - key: span.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "3.1" + attributes: + - key: span.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + - description: Span event sum by attribute + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15.2" + attributes: + - key: event.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "8.1" + attributes: + - key: event.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + + + - resource: + attributes: + - key: resource.required + value: + stringValue: notfoo + scopeMetrics: + - metrics: + - description: Span sum by attribute + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + attributes: + - key: span.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "3.1" + attributes: + - key: span.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + - description: Span event sum by attribute + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15" + attributes: + - key: event.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "8" + attributes: + - key: event.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + + + - resource: {} + scopeMetrics: + - metrics: + - description: Span sum by attribute + name: span.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "5.1" + attributes: + - key: span.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "3.1" + attributes: + - key: span.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + - description: Span event sum by attribute + name: spanevent.sum.by_attr + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "15" + attributes: + - key: event.required + value: + stringValue: foo + timeUnixNano: "1678392127925459000" + - asDouble: "8" + attributes: + - key: event.required + value: + stringValue: notfoo + timeUnixNano: "1678392127925459000" + isMonotonic: true + diff --git a/connector/sumconnector/testdata/traces/one_condition.yaml b/connector/sumconnector/testdata/traces/one_condition.yaml new file mode 100644 index 000000000000..9c95a0555a85 --- /dev/null +++ b/connector/sumconnector/testdata/traces/one_condition.yaml @@ -0,0 +1,55 @@ +resourceMetrics: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127922309000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.900000" + timeUnixNano: "1678392127922310000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeMetrics: + - metrics: + - description: Span sum if ... + name: span.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127922363000" + isMonotonic: true + - description: Span event sum if ... + name: spanevent.sum.if + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.3" + timeUnixNano: "1678392127922364000" + isMonotonic: true + diff --git a/connector/sumconnector/testdata/traces/zero_conditions.yaml b/connector/sumconnector/testdata/traces/zero_conditions.yaml new file mode 100644 index 000000000000..1c287e188dff --- /dev/null +++ b/connector/sumconnector/testdata/traces/zero_conditions.yaml @@ -0,0 +1,99 @@ +resourceMetrics: + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: bar + scopeMetrics: + - metrics: + - description: The sum of beep values observed in spans. + name: trace.span.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127920605000" + isMonotonic: true + - description: The sum of beep values observed in span events. + name: trace.span.event.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.9" + timeUnixNano: "1678392127920605000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: foo + - key: resource.optional + value: + stringValue: notbar + scopeMetrics: + - metrics: + - description: The sum of beep values observed in spans. + name: trace.span.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127920632000" + isMonotonic: true + - description: The sum of beep values observed in span events. + name: trace.span.event.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23.3" + timeUnixNano: "1678392127920632000" + isMonotonic: true + + - resource: + attributes: + - key: resource.required + value: + stringValue: notfoo + scopeMetrics: + - metrics: + - description: The sum of beep values observed in spans. + name: trace.span.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127920635000" + isMonotonic: true + - description: The sum of beep values observed in span events. + name: trace.span.event.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23" + timeUnixNano: "1678392127920635000" + isMonotonic: true + + - resource: {} + scopeMetrics: + - metrics: + - description: The sum of beep values observed in spans. + name: trace.span.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "8.2" + timeUnixNano: "1678392127920638000" + isMonotonic: true + - description: The sum of beep values observed in span events. + name: trace.span.event.sum + sum: + aggregationTemporality: 1 + dataPoints: + - asDouble: "23" + timeUnixNano: "1678392127920638000" + isMonotonic: true +