package tracetransform
import (
commonpb "go.opentelemetry.io/proto/otlp/common/v1"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
)
func KeyValues (attrs []attribute .KeyValue ) []*commonpb .KeyValue {
if len (attrs ) == 0 {
return nil
}
out := make ([]*commonpb .KeyValue , 0 , len (attrs ))
for _ , kv := range attrs {
out = append (out , KeyValue (kv ))
}
return out
}
func Iterator (iter attribute .Iterator ) []*commonpb .KeyValue {
l := iter .Len ()
if l == 0 {
return nil
}
out := make ([]*commonpb .KeyValue , 0 , l )
for iter .Next () {
out = append (out , KeyValue (iter .Attribute ()))
}
return out
}
func ResourceAttributes (res *resource .Resource ) []*commonpb .KeyValue {
return Iterator (res .Iter ())
}
func KeyValue (kv attribute .KeyValue ) *commonpb .KeyValue {
return &commonpb .KeyValue {Key : string (kv .Key ), Value : Value (kv .Value )}
}
func Value (v attribute .Value ) *commonpb .AnyValue {
av := new (commonpb .AnyValue )
switch v .Type () {
case attribute .BOOL :
av .Value = &commonpb .AnyValue_BoolValue {
BoolValue : v .AsBool (),
}
case attribute .BOOLSLICE :
av .Value = &commonpb .AnyValue_ArrayValue {
ArrayValue : &commonpb .ArrayValue {
Values : boolSliceValues (v .AsBoolSlice ()),
},
}
case attribute .INT64 :
av .Value = &commonpb .AnyValue_IntValue {
IntValue : v .AsInt64 (),
}
case attribute .INT64SLICE :
av .Value = &commonpb .AnyValue_ArrayValue {
ArrayValue : &commonpb .ArrayValue {
Values : int64SliceValues (v .AsInt64Slice ()),
},
}
case attribute .FLOAT64 :
av .Value = &commonpb .AnyValue_DoubleValue {
DoubleValue : v .AsFloat64 (),
}
case attribute .FLOAT64SLICE :
av .Value = &commonpb .AnyValue_ArrayValue {
ArrayValue : &commonpb .ArrayValue {
Values : float64SliceValues (v .AsFloat64Slice ()),
},
}
case attribute .STRING :
av .Value = &commonpb .AnyValue_StringValue {
StringValue : v .AsString (),
}
case attribute .STRINGSLICE :
av .Value = &commonpb .AnyValue_ArrayValue {
ArrayValue : &commonpb .ArrayValue {
Values : stringSliceValues (v .AsStringSlice ()),
},
}
default :
av .Value = &commonpb .AnyValue_StringValue {
StringValue : "INVALID" ,
}
}
return av
}
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 ,
},
}
}
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 ,
},
}
}
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 ,
},
}
}
return converted
}
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 ,
},
}
}
return converted
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .