...

Source file src/go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform/attribute.go

Documentation: go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tracetransform // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/internal/tracetransform"
    16  
    17  import (
    18  	"go.opentelemetry.io/otel/attribute"
    19  	"go.opentelemetry.io/otel/sdk/resource"
    20  	commonpb "go.opentelemetry.io/proto/otlp/common/v1"
    21  )
    22  
    23  // KeyValues transforms a slice of attribute KeyValues into OTLP key-values.
    24  func KeyValues(attrs []attribute.KeyValue) []*commonpb.KeyValue {
    25  	if len(attrs) == 0 {
    26  		return nil
    27  	}
    28  
    29  	out := make([]*commonpb.KeyValue, 0, len(attrs))
    30  	for _, kv := range attrs {
    31  		out = append(out, KeyValue(kv))
    32  	}
    33  	return out
    34  }
    35  
    36  // Iterator transforms an attribute iterator into OTLP key-values.
    37  func Iterator(iter attribute.Iterator) []*commonpb.KeyValue {
    38  	l := iter.Len()
    39  	if l == 0 {
    40  		return nil
    41  	}
    42  
    43  	out := make([]*commonpb.KeyValue, 0, l)
    44  	for iter.Next() {
    45  		out = append(out, KeyValue(iter.Attribute()))
    46  	}
    47  	return out
    48  }
    49  
    50  // ResourceAttributes transforms a Resource OTLP key-values.
    51  func ResourceAttributes(res *resource.Resource) []*commonpb.KeyValue {
    52  	return Iterator(res.Iter())
    53  }
    54  
    55  // KeyValue transforms an attribute KeyValue into an OTLP key-value.
    56  func KeyValue(kv attribute.KeyValue) *commonpb.KeyValue {
    57  	return &commonpb.KeyValue{Key: string(kv.Key), Value: Value(kv.Value)}
    58  }
    59  
    60  // Value transforms an attribute Value into an OTLP AnyValue.
    61  func Value(v attribute.Value) *commonpb.AnyValue {
    62  	av := new(commonpb.AnyValue)
    63  	switch v.Type() {
    64  	case attribute.BOOL:
    65  		av.Value = &commonpb.AnyValue_BoolValue{
    66  			BoolValue: v.AsBool(),
    67  		}
    68  	case attribute.BOOLSLICE:
    69  		av.Value = &commonpb.AnyValue_ArrayValue{
    70  			ArrayValue: &commonpb.ArrayValue{
    71  				Values: boolSliceValues(v.AsBoolSlice()),
    72  			},
    73  		}
    74  	case attribute.INT64:
    75  		av.Value = &commonpb.AnyValue_IntValue{
    76  			IntValue: v.AsInt64(),
    77  		}
    78  	case attribute.INT64SLICE:
    79  		av.Value = &commonpb.AnyValue_ArrayValue{
    80  			ArrayValue: &commonpb.ArrayValue{
    81  				Values: int64SliceValues(v.AsInt64Slice()),
    82  			},
    83  		}
    84  	case attribute.FLOAT64:
    85  		av.Value = &commonpb.AnyValue_DoubleValue{
    86  			DoubleValue: v.AsFloat64(),
    87  		}
    88  	case attribute.FLOAT64SLICE:
    89  		av.Value = &commonpb.AnyValue_ArrayValue{
    90  			ArrayValue: &commonpb.ArrayValue{
    91  				Values: float64SliceValues(v.AsFloat64Slice()),
    92  			},
    93  		}
    94  	case attribute.STRING:
    95  		av.Value = &commonpb.AnyValue_StringValue{
    96  			StringValue: v.AsString(),
    97  		}
    98  	case attribute.STRINGSLICE:
    99  		av.Value = &commonpb.AnyValue_ArrayValue{
   100  			ArrayValue: &commonpb.ArrayValue{
   101  				Values: stringSliceValues(v.AsStringSlice()),
   102  			},
   103  		}
   104  	default:
   105  		av.Value = &commonpb.AnyValue_StringValue{
   106  			StringValue: "INVALID",
   107  		}
   108  	}
   109  	return av
   110  }
   111  
   112  func boolSliceValues(vals []bool) []*commonpb.AnyValue {
   113  	converted := make([]*commonpb.AnyValue, len(vals))
   114  	for i, v := range vals {
   115  		converted[i] = &commonpb.AnyValue{
   116  			Value: &commonpb.AnyValue_BoolValue{
   117  				BoolValue: v,
   118  			},
   119  		}
   120  	}
   121  	return converted
   122  }
   123  
   124  func int64SliceValues(vals []int64) []*commonpb.AnyValue {
   125  	converted := make([]*commonpb.AnyValue, len(vals))
   126  	for i, v := range vals {
   127  		converted[i] = &commonpb.AnyValue{
   128  			Value: &commonpb.AnyValue_IntValue{
   129  				IntValue: v,
   130  			},
   131  		}
   132  	}
   133  	return converted
   134  }
   135  
   136  func float64SliceValues(vals []float64) []*commonpb.AnyValue {
   137  	converted := make([]*commonpb.AnyValue, len(vals))
   138  	for i, v := range vals {
   139  		converted[i] = &commonpb.AnyValue{
   140  			Value: &commonpb.AnyValue_DoubleValue{
   141  				DoubleValue: v,
   142  			},
   143  		}
   144  	}
   145  	return converted
   146  }
   147  
   148  func stringSliceValues(vals []string) []*commonpb.AnyValue {
   149  	converted := make([]*commonpb.AnyValue, len(vals))
   150  	for i, v := range vals {
   151  		converted[i] = &commonpb.AnyValue{
   152  			Value: &commonpb.AnyValue_StringValue{
   153  				StringValue: v,
   154  			},
   155  		}
   156  	}
   157  	return converted
   158  }
   159  

View as plain text