File: opencensus_test.go

package info (click to toggle)
golang-github-azure-go-autorest 14.2.0%2Bgit20220726.711dde1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,320 kB
  • sloc: makefile: 15
file content (161 lines) | stat: -rw-r--r-- 4,792 bytes parent folder | download | duplicates (2)
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
151
152
153
154
155
156
157
158
159
160
161
package opencensus

// Copyright 2018 Microsoft Corporation
//
//  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.

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

	"contrib.go.opencensus.io/exporter/ocagent"
	"github.com/Azure/go-autorest/tracing"
	"go.opencensus.io/plugin/ochttp"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/trace"
)

func TestNoTracingByDefault(t *testing.T) {
	if expected, got := false, tracing.IsEnabled(); expected != got {
		t.Fatalf("By default expected %t, got %t", expected, got)
	}

	if defaultTracer == nil {
		t.Fatal("unexpected nil defaultTracer")
	}

	if defaultTracer.sampler == nil {
		t.Fatal("By default expected non nil sampler")
	}

	if tr := defaultTracer.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler == nil {
		t.Fatalf("By default expected configured Sampler to be non-nil")
	}

	for n := range defaultTracer.views {
		v := view.Find(n)
		if v != nil {
			t.Fatalf("By default expected no registered views, found %s", v.Name)
		}
	}
}

func TestEnableTracing(t *testing.T) {
	err := Enable()

	if err != nil {
		t.Fatalf("Enable failed, got error %v", err)
	}
	if e := tracing.IsEnabled(); !e {
		t.Fatalf("Enable failed, IsEnabled() is %t", e)
	}
	if defaultTracer.sampler != nil {
		t.Fatalf("Enable failed, expected nil sampler, got %v", defaultTracer.sampler)
	}

	if tr := tracing.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler != nil {
		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
	}

	for n, v := range defaultTracer.views {
		fv := view.Find(n)
		if fv == nil || !reflect.DeepEqual(v, fv) {
			t.Fatalf("Enable failed, view %s was not registered", n)
		}
	}
}

func TestTracingByEnv(t *testing.T) {
	os.Setenv("AZURE_SDK_TRACING_ENABLED", "")
	enableFromEnv()
	if e := tracing.IsEnabled(); !e {
		t.Fatalf("Enable failed, IsEnabled() is %t", e)
	}
	if defaultTracer.sampler != nil {
		t.Fatalf("Enable failed, expected nil sampler, got %v", defaultTracer.sampler)
	}

	if tr := tracing.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler != nil {
		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
	}

	for n, v := range defaultTracer.views {
		fv := view.Find(n)
		if fv == nil || !reflect.DeepEqual(v, fv) {
			t.Fatalf("Enable failed, view %s was not registered", n)
		}
	}
}

func TestEnableTracingWithAIError(t *testing.T) {
	agentEndpoint := fmt.Sprintf("%s:%d", ocagent.DefaultAgentHost, ocagent.DefaultAgentPort)
	if err := EnableWithAIForwarding(agentEndpoint); err != nil {
		// note that even though the agent isn't running no error is
		// returned, the exporter's state is simply set to disconnected.
		t.Fatalf("EnableWithAIForwarding failed: %v", err)
	}
	if e := tracing.IsEnabled(); !e {
		t.Fatalf("Enable failed, IsEnabled() is %t", e)
	}
	if defaultTracer.sampler != nil {
		t.Fatalf("Enable failed, expected nil sampler, got %v", defaultTracer.sampler)
	}

	if tr := tracing.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler != nil {
		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
	}

	for n, v := range defaultTracer.views {
		fv := view.Find(n)
		if fv == nil || !reflect.DeepEqual(v, fv) {
			t.Fatalf("Enable failed, view %s was not registered", n)
		}
	}
}

func TestDisableTracing(t *testing.T) {
	Enable()
	Disable()
	if expected, got := false, tracing.IsEnabled(); expected != got {
		t.Fatalf("By default expected %t, got %t", expected, got)
	}

	if defaultTracer.sampler == nil {
		t.Fatal("By default expected non nil sampler")
	}

	if tr := defaultTracer.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler == nil {
		t.Fatalf("By default expected configured Sampler to be non-nil")
	}

	for n := range defaultTracer.views {
		v := view.Find(n)
		if v != nil {
			t.Fatalf("By default expected no registered views, found %s", v.Name)
		}
	}
}

func TestStartSpan(t *testing.T) {
	ctx := defaultTracer.StartSpan(context.Background(), "testSpan")
	defer defaultTracer.EndSpan(ctx, 200, nil)

	span := trace.FromContext(ctx)
	if span == nil {
		t.Fatal("StartSpan failed, expected non-nil span")
	}
}