| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package obsconsumer // import "go.opentelemetry.io/collector/service/internal/obsconsumer" |
| 5 |
|
| 6 |
import ( |
| 7 |
"go.opentelemetry.io/otel/attribute" |
| 8 |
"go.opentelemetry.io/otel/metric" |
| 9 |
) |
| 10 |
|
| 11 |
const ( |
| 12 |
ComponentOutcome = "otelcol.component.outcome" |
| 13 |
) |
| 14 |
|
| 15 |
// Option modifies the consumer behavior. |
| 16 |
type Option func(*options) |
| 17 |
|
| 18 |
type options struct { |
| 19 |
staticDataPointAttributes []attribute.KeyValue |
| 20 |
} |
| 21 |
|
| 22 |
// WithStaticDataPointAttribute returns an Option that adds a static attribute to data points. |
| 23 |
func WithStaticDataPointAttribute(attr attribute.KeyValue) Option { |
| 24 |
return func(opts *options) { |
| 25 |
opts.staticDataPointAttributes = append(opts.staticDataPointAttributes, attr) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
type compiledOptions struct { |
| 30 |
withSuccessAttrs metric.AddOption |
| 31 |
withFailureAttrs metric.AddOption |
| 32 |
withRefusedAttrs metric.AddOption |
| 33 |
} |
| 34 |
|
| 35 |
func (o *options) compile() compiledOptions { |
| 36 |
successAttrs := make([]attribute.KeyValue, 0, 1+len(o.staticDataPointAttributes)) |
| 37 |
successAttrs = append(successAttrs, attribute.String(ComponentOutcome, "success")) |
| 38 |
successAttrs = append(successAttrs, o.staticDataPointAttributes...) |
| 39 |
|
| 40 |
failureAttrs := make([]attribute.KeyValue, 0, 1+len(o.staticDataPointAttributes)) |
| 41 |
failureAttrs = append(failureAttrs, attribute.String(ComponentOutcome, "failure")) |
| 42 |
failureAttrs = append(failureAttrs, o.staticDataPointAttributes...) |
| 43 |
|
| 44 |
refusedAttrs := make([]attribute.KeyValue, 0, 1+len(o.staticDataPointAttributes)) |
| 45 |
refusedAttrs = append(refusedAttrs, attribute.String(ComponentOutcome, "refused")) |
| 46 |
refusedAttrs = append(refusedAttrs, o.staticDataPointAttributes...) |
| 47 |
|
| 48 |
return compiledOptions{ |
| 49 |
withSuccessAttrs: metric.WithAttributes(successAttrs...), |
| 50 |
withFailureAttrs: metric.WithAttributes(failureAttrs...), |
| 51 |
withRefusedAttrs: metric.WithAttributes(refusedAttrs...), |
| 52 |
} |
| 53 |
} |