File: binary_test.go

package info (click to toggle)
golang-opentelemetry-contrib 0.56.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 4,884 kB
  • sloc: makefile: 278; sh: 211; sed: 1
file content (150 lines) | stat: -rw-r--r-- 3,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package opencensus

import (
	"context"
	"fmt"
	"net/http"
	"testing"

	"github.com/google/go-cmp/cmp"

	"go.opentelemetry.io/otel/propagation"
	"go.opentelemetry.io/otel/trace"
)

var (
	traceID     = trace.TraceID([16]byte{14, 54, 12})
	spanID      = trace.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1})
	childSpanID = trace.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 2})
	headerFmt   = "\x00\x00\x0e6\f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00%s\x02%s"
)

func TestFields(t *testing.T) {
	b := Binary{}
	fields := b.Fields()
	if len(fields) != 1 {
		t.Fatalf("Got %d fields, expected 1", len(fields))
	}
	if fields[0] != "grpc-trace-bin" {
		t.Errorf("Got fields[0] == %s, expected grpc-trace-bin", fields[0])
	}
}

func TestInject(t *testing.T) {
	prop := Binary{}
	for _, tt := range []struct {
		desc       string
		scc        trace.SpanContextConfig
		wantHeader string
	}{
		{
			desc:       "empty",
			scc:        trace.SpanContextConfig{},
			wantHeader: "",
		},
		{
			desc: "valid spancontext, sampled",
			scc: trace.SpanContextConfig{
				TraceID:    traceID,
				SpanID:     spanID,
				TraceFlags: trace.FlagsSampled,
			},
			wantHeader: fmt.Sprintf(headerFmt, "\x01", "\x01"),
		},
		{
			desc: "valid spancontext, not sampled",
			scc: trace.SpanContextConfig{
				TraceID: traceID,
				SpanID:  spanID,
			},
			wantHeader: fmt.Sprintf(headerFmt, "\x01", "\x00"),
		},
		{
			desc: "valid spancontext, with unsupported bit set in traceflags",
			scc: trace.SpanContextConfig{
				TraceID:    traceID,
				SpanID:     spanID,
				TraceFlags: 0xff,
			},
			wantHeader: fmt.Sprintf(headerFmt, "\x01", "\x01"),
		},
		{
			desc:       "invalid spancontext",
			scc:        trace.SpanContextConfig{},
			wantHeader: "",
		},
	} {
		t.Run(tt.desc, func(t *testing.T) {
			header := http.Header{}
			ctx := context.Background()
			if sc := trace.NewSpanContext(tt.scc); sc.IsValid() {
				ctx = trace.ContextWithRemoteSpanContext(ctx, sc)
			}
			prop.Inject(ctx, propagation.HeaderCarrier(header))

			gotHeader := header.Get("grpc-trace-bin")
			if gotHeader != tt.wantHeader {
				t.Errorf("Got header = %q, want %q", gotHeader, tt.wantHeader)
			}
		})
	}
}

func TestExtract(t *testing.T) {
	prop := Binary{}
	for _, tt := range []struct {
		desc    string
		header  string
		wantScc trace.SpanContextConfig
	}{
		{
			desc:    "empty",
			header:  "",
			wantScc: trace.SpanContextConfig{},
		},
		{
			desc:    "header not binary",
			header:  "5435j345io34t5904w3jt894j3t854w89tp95jgt9",
			wantScc: trace.SpanContextConfig{},
		},
		{
			desc:   "valid binary header",
			header: fmt.Sprintf(headerFmt, "\x02", "\x00"),
			wantScc: trace.SpanContextConfig{
				TraceID: traceID,
				SpanID:  childSpanID,
			},
		},
		{
			desc:   "valid binary and sampled",
			header: fmt.Sprintf(headerFmt, "\x02", "\x01"),
			wantScc: trace.SpanContextConfig{
				TraceID:    traceID,
				SpanID:     childSpanID,
				TraceFlags: trace.FlagsSampled,
			},
		},
	} {
		t.Run(tt.desc, func(t *testing.T) {
			header := http.Header{
				http.CanonicalHeaderKey("grpc-trace-bin"): []string{tt.header},
			}

			ctx := context.Background()
			ctx = prop.Extract(ctx, propagation.HeaderCarrier(header))
			gotSc := trace.SpanContextFromContext(ctx)
			comparer := cmp.Comparer(func(a, b trace.SpanContext) bool {
				// Do not compare remote field, it is unset on empty
				// SpanContext.
				newA := a.WithRemote(b.IsRemote())
				return newA.Equal(b)
			})
			if diff := cmp.Diff(gotSc, trace.NewSpanContext(tt.wantScc), comparer); diff != "" {
				t.Errorf("%s: -got +want %s", tt.desc, diff)
			}
		})
	}
}