Debian Codegraph @

internal/telemetry/attribute.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package telemetry // import "go.opentelemetry.io/collector/internal/telemetry"
5
6 import (
7 "go.opentelemetry.io/otel/attribute"
8 "go.uber.org/zap"
9 )
10
11 const (
12 ComponentKindKey = "otelcol.component.kind"
13 ComponentIDKey = "otelcol.component.id"
14 PipelineIDKey = "otelcol.pipeline.id"
15 SignalKey = "otelcol.signal"
16 SignalOutputKey = "otelcol.signal.output"
17 )
18
19 // ToZapFields converts an OTel Go attribute set to a slice of zap fields.
20 func ToZapFields(attrs []attribute.KeyValue) []zap.Field {
21 zapFields := make([]zap.Field, 0, len(attrs))
22 for _, attr := range attrs {
23 var zapField zap.Field
24 key := string(attr.Key)
25 switch attr.Value.Type() {
26 case attribute.BOOL:
27 zapField = zap.Bool(key, attr.Value.AsBool())
28 case attribute.INT64:
29 zapField = zap.Int64(key, attr.Value.AsInt64())
30 case attribute.FLOAT64:
31 zapField = zap.Float64(key, attr.Value.AsFloat64())
32 case attribute.STRING:
33 zapField = zap.String(key, attr.Value.AsString())
34 case attribute.BOOLSLICE:
35 zapField = zap.Bools(key, attr.Value.AsBoolSlice())
36 case attribute.INT64SLICE:
37 zapField = zap.Int64s(key, attr.Value.AsInt64Slice())
38 case attribute.FLOAT64SLICE:
39 zapField = zap.Float64s(key, attr.Value.AsFloat64Slice())
40 case attribute.STRINGSLICE:
41 zapField = zap.Strings(key, attr.Value.AsStringSlice())
42 default:
43 zapField = zap.Any(key, attr.Value.AsInterface())
44 }
45 zapFields = append(zapFields, zapField)
46 }
47 return zapFields
48 }