2021-05-27 22:47:01 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tracetransform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
)
|
|
|
|
|
2021-08-11 20:30:05 +02:00
|
|
|
// KeyValues transforms a slice of attribute KeyValues into OTLP key-values.
|
|
|
|
func KeyValues(attrs []attribute.KeyValue) []*commonpb.KeyValue {
|
2021-05-27 22:47:01 +02:00
|
|
|
if len(attrs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make([]*commonpb.KeyValue, 0, len(attrs))
|
|
|
|
for _, kv := range attrs {
|
2021-08-11 20:30:05 +02:00
|
|
|
out = append(out, KeyValue(kv))
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-08-11 20:30:05 +02:00
|
|
|
// Iterator transforms an attribute iterator into OTLP key-values.
|
|
|
|
func Iterator(iter attribute.Iterator) []*commonpb.KeyValue {
|
|
|
|
l := iter.Len()
|
|
|
|
if l == 0 {
|
2021-05-27 22:47:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 20:30:05 +02:00
|
|
|
out := make([]*commonpb.KeyValue, 0, l)
|
|
|
|
for iter.Next() {
|
|
|
|
out = append(out, KeyValue(iter.Attribute()))
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2021-08-11 20:30:05 +02:00
|
|
|
// ResourceAttributes transforms a Resource OTLP key-values.
|
|
|
|
func ResourceAttributes(resource *resource.Resource) []*commonpb.KeyValue {
|
|
|
|
return Iterator(resource.Iter())
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyValue transforms an attribute KeyValue into an OTLP key-value.
|
|
|
|
func KeyValue(kv attribute.KeyValue) *commonpb.KeyValue {
|
|
|
|
return &commonpb.KeyValue{Key: string(kv.Key), Value: Value(kv.Value)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value transforms an attribute Value into an OTLP AnyValue.
|
|
|
|
func Value(v attribute.Value) *commonpb.AnyValue {
|
|
|
|
av := new(commonpb.AnyValue)
|
|
|
|
switch v.Type() {
|
2021-05-27 22:47:01 +02:00
|
|
|
case attribute.BOOL:
|
2021-08-11 20:30:05 +02:00
|
|
|
av.Value = &commonpb.AnyValue_BoolValue{
|
|
|
|
BoolValue: v.AsBool(),
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
case attribute.BOOLSLICE:
|
|
|
|
av.Value = &commonpb.AnyValue_ArrayValue{
|
|
|
|
ArrayValue: &commonpb.ArrayValue{
|
|
|
|
Values: boolSliceValues(v.AsBoolSlice()),
|
|
|
|
},
|
|
|
|
}
|
2021-05-27 22:47:01 +02:00
|
|
|
case attribute.INT64:
|
2021-08-11 20:30:05 +02:00
|
|
|
av.Value = &commonpb.AnyValue_IntValue{
|
|
|
|
IntValue: v.AsInt64(),
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
case attribute.INT64SLICE:
|
|
|
|
av.Value = &commonpb.AnyValue_ArrayValue{
|
|
|
|
ArrayValue: &commonpb.ArrayValue{
|
|
|
|
Values: int64SliceValues(v.AsInt64Slice()),
|
|
|
|
},
|
|
|
|
}
|
2021-05-27 22:47:01 +02:00
|
|
|
case attribute.FLOAT64:
|
2021-08-11 20:30:05 +02:00
|
|
|
av.Value = &commonpb.AnyValue_DoubleValue{
|
|
|
|
DoubleValue: v.AsFloat64(),
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
case attribute.FLOAT64SLICE:
|
|
|
|
av.Value = &commonpb.AnyValue_ArrayValue{
|
|
|
|
ArrayValue: &commonpb.ArrayValue{
|
|
|
|
Values: float64SliceValues(v.AsFloat64Slice()),
|
|
|
|
},
|
|
|
|
}
|
2021-05-27 22:47:01 +02:00
|
|
|
case attribute.STRING:
|
2021-08-11 20:30:05 +02:00
|
|
|
av.Value = &commonpb.AnyValue_StringValue{
|
|
|
|
StringValue: v.AsString(),
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
case attribute.STRINGSLICE:
|
2021-08-11 20:30:05 +02:00
|
|
|
av.Value = &commonpb.AnyValue_ArrayValue{
|
2021-05-27 22:47:01 +02:00
|
|
|
ArrayValue: &commonpb.ArrayValue{
|
2021-08-12 17:05:42 +02:00
|
|
|
Values: stringSliceValues(v.AsStringSlice()),
|
2021-05-27 22:47:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
2021-08-11 20:30:05 +02:00
|
|
|
av.Value = &commonpb.AnyValue_StringValue{
|
2021-05-27 22:47:01 +02:00
|
|
|
StringValue: "INVALID",
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 20:30:05 +02:00
|
|
|
return av
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
func boolSliceValues(vals []bool) []*commonpb.AnyValue {
|
|
|
|
converted := make([]*commonpb.AnyValue, len(vals))
|
|
|
|
for i, v := range vals {
|
|
|
|
converted[i] = &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_BoolValue{
|
|
|
|
BoolValue: v,
|
|
|
|
},
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func int64SliceValues(vals []int64) []*commonpb.AnyValue {
|
|
|
|
converted := make([]*commonpb.AnyValue, len(vals))
|
|
|
|
for i, v := range vals {
|
|
|
|
converted[i] = &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_IntValue{
|
|
|
|
IntValue: v,
|
|
|
|
},
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func float64SliceValues(vals []float64) []*commonpb.AnyValue {
|
|
|
|
converted := make([]*commonpb.AnyValue, len(vals))
|
|
|
|
for i, v := range vals {
|
|
|
|
converted[i] = &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_DoubleValue{
|
|
|
|
DoubleValue: v,
|
|
|
|
},
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
return converted
|
|
|
|
}
|
2021-05-27 22:47:01 +02:00
|
|
|
|
2021-08-12 17:05:42 +02:00
|
|
|
func stringSliceValues(vals []string) []*commonpb.AnyValue {
|
|
|
|
converted := make([]*commonpb.AnyValue, len(vals))
|
|
|
|
for i, v := range vals {
|
|
|
|
converted[i] = &commonpb.AnyValue{
|
|
|
|
Value: &commonpb.AnyValue_StringValue{
|
|
|
|
StringValue: v,
|
|
|
|
},
|
|
|
|
}
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|
2021-08-12 17:05:42 +02:00
|
|
|
return converted
|
2021-05-27 22:47:01 +02:00
|
|
|
}
|