Debian Codegraph @

exporters/otlp/otlptrace/internal/tracetransform/attribute.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package tracetransform provides conversion functionality for the otlptrace
5 // exporters.
6 package tracetransform // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
7
8 import (
9 commonpb "go.opentelemetry.io/proto/otlp/common/v1"
10
11 "go.opentelemetry.io/otel/attribute"
12 "go.opentelemetry.io/otel/sdk/resource"
13 )
14
15 // KeyValues transforms a slice of attribute KeyValues into OTLP key-values.
16 func KeyValues(attrs []attribute.KeyValue) []*commonpb.KeyValue {
17 if len(attrs) == 0 {
18 return nil
19 }
20
21 out := make([]*commonpb.KeyValue, 0, len(attrs))
22 for _, kv := range attrs {
23 out = append(out, KeyValue(kv))
24 }
25 return out
26 }
27
28 // Iterator transforms an attribute iterator into OTLP key-values.
29 func Iterator(iter attribute.Iterator) []*commonpb.KeyValue {
30 l := iter.Len()
31 if l == 0 {
32 return nil
33 }
34
35 out := make([]*commonpb.KeyValue, 0, l)
36 for iter.Next() {
37 out = append(out, KeyValue(iter.Attribute()))
38 }
39 return out
40 }
41
42 // ResourceAttributes transforms a Resource OTLP key-values.
43 func ResourceAttributes(res *resource.Resource) []*commonpb.KeyValue {
44 return Iterator(res.Iter())
45 }
46
47 // KeyValue transforms an attribute KeyValue into an OTLP key-value.
48 func KeyValue(kv attribute.KeyValue) *commonpb.KeyValue {
49 return &commonpb.KeyValue{Key: string(kv.Key), Value: Value(kv.Value)}
50 }
51
52 // Value transforms an attribute Value into an OTLP AnyValue.
53 func Value(v attribute.Value) *commonpb.AnyValue {
54 av := new(commonpb.AnyValue)
55 switch v.Type() {
56 case attribute.BOOL:
57 av.Value = &commonpb.AnyValue_BoolValue{
58 BoolValue: v.AsBool(),
59 }
60 case attribute.BOOLSLICE:
61 av.Value = &commonpb.AnyValue_ArrayValue{
62 ArrayValue: &commonpb.ArrayValue{
63 Values: boolSliceValues(v.AsBoolSlice()),
64 },
65 }
66 case attribute.INT64:
67 av.Value = &commonpb.AnyValue_IntValue{
68 IntValue: v.AsInt64(),
69 }
70 case attribute.INT64SLICE:
71 av.Value = &commonpb.AnyValue_ArrayValue{
72 ArrayValue: &commonpb.ArrayValue{
73 Values: int64SliceValues(v.AsInt64Slice()),
74 },
75 }
76 case attribute.FLOAT64:
77 av.Value = &commonpb.AnyValue_DoubleValue{
78 DoubleValue: v.AsFloat64(),
79 }
80 case attribute.FLOAT64SLICE:
81 av.Value = &commonpb.AnyValue_ArrayValue{
82 ArrayValue: &commonpb.ArrayValue{
83 Values: float64SliceValues(v.AsFloat64Slice()),
84 },
85 }
86 case attribute.STRING:
87 av.Value = &commonpb.AnyValue_StringValue{
88 StringValue: v.AsString(),
89 }
90 case attribute.STRINGSLICE:
91 av.Value = &commonpb.AnyValue_ArrayValue{
92 ArrayValue: &commonpb.ArrayValue{
93 Values: stringSliceValues(v.AsStringSlice()),
94 },
95 }
96 case attribute.EMPTY:
97 default:
98 av.Value = &commonpb.AnyValue_StringValue{
99 StringValue: "INVALID",
100 }
101 }
102 return av
103 }
104
105 func boolSliceValues(vals []bool) []*commonpb.AnyValue {
106 converted := make([]*commonpb.AnyValue, len(vals))
107 for i, v := range vals {
108 converted[i] = &commonpb.AnyValue{
109 Value: &commonpb.AnyValue_BoolValue{
110 BoolValue: v,
111 },
112 }
113 }
114 return converted
115 }
116
117 func int64SliceValues(vals []int64) []*commonpb.AnyValue {
118 converted := make([]*commonpb.AnyValue, len(vals))
119 for i, v := range vals {
120 converted[i] = &commonpb.AnyValue{
121 Value: &commonpb.AnyValue_IntValue{
122 IntValue: v,
123 },
124 }
125 }
126 return converted
127 }
128
129 func float64SliceValues(vals []float64) []*commonpb.AnyValue {
130 converted := make([]*commonpb.AnyValue, len(vals))
131 for i, v := range vals {
132 converted[i] = &commonpb.AnyValue{
133 Value: &commonpb.AnyValue_DoubleValue{
134 DoubleValue: v,
135 },
136 }
137 }
138 return converted
139 }
140
141 func stringSliceValues(vals []string) []*commonpb.AnyValue {
142 converted := make([]*commonpb.AnyValue, len(vals))
143 for i, v := range vals {
144 converted[i] = &commonpb.AnyValue{
145 Value: &commonpb.AnyValue_StringValue{
146 StringValue: v,
147 },
148 }
149 }
150 return converted
151 }