Debian Codegraph @

bridge/opencensus/internal/oc2otel/attributes_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package oc2otel
5
6 import (
7 "testing"
8
9 octrace "go.opencensus.io/trace"
10
11 "go.opentelemetry.io/otel/attribute"
12 )
13
14 func TestAttributes(t *testing.T) {
15 in := []octrace.Attribute{
16 octrace.BoolAttribute("bool", true),
17 octrace.Int64Attribute("int64", 49),
18 octrace.Float64Attribute("float64", 1.618),
19 octrace.StringAttribute("key", "val"),
20 }
21
22 want := []attribute.KeyValue{
23 attribute.Bool("bool", true),
24 attribute.Int64("int64", 49),
25 attribute.Float64("float64", 1.618),
26 attribute.String("key", "val"),
27 }
28 got := Attributes(in)
29
30 if len(got) != len(want) {
31 t.Errorf("Attributes conversion failed: want %#v, got %#v", want, got)
32 }
33 for i := range got {
34 if g, w := got[i], want[i]; g != w {
35 t.Errorf("Attributes conversion: want %#v, got %#v", w, g)
36 }
37 }
38 }
39
40 func TestAttributesFromMap(t *testing.T) {
41 in := map[string]any{
42 "bool": true,
43 "int64": int64(49),
44 "float64": float64(1.618),
45 "key": "val",
46 }
47
48 want := []attribute.KeyValue{
49 attribute.Bool("bool", true),
50 attribute.Int64("int64", 49),
51 attribute.Float64("float64", 1.618),
52 attribute.String("key", "val"),
53 }
54 got := AttributesFromMap(in)
55
56 gotAttributeSet := attribute.NewSet(got...)
57 wantAttributeSet := attribute.NewSet(want...)
58 if !gotAttributeSet.Equals(&wantAttributeSet) {
59 t.Errorf(
60 "Attributes conversion want %v, got %v",
61 wantAttributeSet.Encoded(attribute.DefaultEncoder()),
62 gotAttributeSet.Encoded(attribute.DefaultEncoder()),
63 )
64 }
65 }
66
67 func TestAttributeValueUnknown(t *testing.T) {
68 got := AttributeValue([]byte{})
69 if got != attribute.StringValue("unknown") {
70 t.Errorf("AttributeValue of unknown wrong: %#v", got)
71 }
72 }