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