1package opencensus
2
3// Copyright 2018 Microsoft Corporation
4//
5//  Licensed under the Apache License, Version 2.0 (the "License");
6//  you may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at
8//
9//      http://www.apache.org/licenses/LICENSE-2.0
10//
11//  Unless required by applicable law or agreed to in writing, software
12//  distributed under the License is distributed on an "AS IS" BASIS,
13//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14//  See the License for the specific language governing permissions and
15//  limitations under the License.
16
17import (
18	"context"
19	"fmt"
20	"net/http"
21	"os"
22	"reflect"
23	"testing"
24
25	"contrib.go.opencensus.io/exporter/ocagent"
26	"github.com/Azure/go-autorest/tracing"
27	"go.opencensus.io/plugin/ochttp"
28	"go.opencensus.io/stats/view"
29	"go.opencensus.io/trace"
30)
31
32func TestNoTracingByDefault(t *testing.T) {
33	if expected, got := false, tracing.IsEnabled(); expected != got {
34		t.Fatalf("By default expected %t, got %t", expected, got)
35	}
36
37	if defaultTracer == nil {
38		t.Fatal("unexpected nil defaultTracer")
39	}
40
41	if defaultTracer.sampler == nil {
42		t.Fatal("By default expected non nil sampler")
43	}
44
45	if tr := defaultTracer.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler == nil {
46		t.Fatalf("By default expected configured Sampler to be non-nil")
47	}
48
49	for n := range defaultTracer.views {
50		v := view.Find(n)
51		if v != nil {
52			t.Fatalf("By default expected no registered views, found %s", v.Name)
53		}
54	}
55}
56
57func TestEnableTracing(t *testing.T) {
58	err := Enable()
59
60	if err != nil {
61		t.Fatalf("Enable failed, got error %v", err)
62	}
63	if e := tracing.IsEnabled(); !e {
64		t.Fatalf("Enable failed, IsEnabled() is %t", e)
65	}
66	if defaultTracer.sampler != nil {
67		t.Fatalf("Enable failed, expected nil sampler, got %v", defaultTracer.sampler)
68	}
69
70	if tr := tracing.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler != nil {
71		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
72	}
73
74	for n, v := range defaultTracer.views {
75		fv := view.Find(n)
76		if fv == nil || !reflect.DeepEqual(v, fv) {
77			t.Fatalf("Enable failed, view %s was not registered", n)
78		}
79	}
80}
81
82func TestTracingByEnv(t *testing.T) {
83	os.Setenv("AZURE_SDK_TRACING_ENABLED", "")
84	enableFromEnv()
85	if e := tracing.IsEnabled(); !e {
86		t.Fatalf("Enable failed, IsEnabled() is %t", e)
87	}
88	if defaultTracer.sampler != nil {
89		t.Fatalf("Enable failed, expected nil sampler, got %v", defaultTracer.sampler)
90	}
91
92	if tr := tracing.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler != nil {
93		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
94	}
95
96	for n, v := range defaultTracer.views {
97		fv := view.Find(n)
98		if fv == nil || !reflect.DeepEqual(v, fv) {
99			t.Fatalf("Enable failed, view %s was not registered", n)
100		}
101	}
102}
103
104func TestEnableTracingWithAIError(t *testing.T) {
105	agentEndpoint := fmt.Sprintf("%s:%d", ocagent.DefaultAgentHost, ocagent.DefaultAgentPort)
106	if err := EnableWithAIForwarding(agentEndpoint); err != nil {
107		// note that even though the agent isn't running no error is
108		// returned, the exporter's state is simply set to disconnected.
109		t.Fatalf("EnableWithAIForwarding failed: %v", err)
110	}
111	if e := tracing.IsEnabled(); !e {
112		t.Fatalf("Enable failed, IsEnabled() is %t", e)
113	}
114	if defaultTracer.sampler != nil {
115		t.Fatalf("Enable failed, expected nil sampler, got %v", defaultTracer.sampler)
116	}
117
118	if tr := tracing.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler != nil {
119		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
120	}
121
122	for n, v := range defaultTracer.views {
123		fv := view.Find(n)
124		if fv == nil || !reflect.DeepEqual(v, fv) {
125			t.Fatalf("Enable failed, view %s was not registered", n)
126		}
127	}
128}
129
130func TestDisableTracing(t *testing.T) {
131	Enable()
132	Disable()
133	if expected, got := false, tracing.IsEnabled(); expected != got {
134		t.Fatalf("By default expected %t, got %t", expected, got)
135	}
136
137	if defaultTracer.sampler == nil {
138		t.Fatal("By default expected non nil sampler")
139	}
140
141	if tr := defaultTracer.NewTransport(nil); tr.(*ochttp.Transport).GetStartOptions(&http.Request{}).Sampler == nil {
142		t.Fatalf("By default expected configured Sampler to be non-nil")
143	}
144
145	for n := range defaultTracer.views {
146		v := view.Find(n)
147		if v != nil {
148			t.Fatalf("By default expected no registered views, found %s", v.Name)
149		}
150	}
151}
152
153func TestStartSpan(t *testing.T) {
154	ctx := defaultTracer.StartSpan(context.Background(), "testSpan")
155	defer defaultTracer.EndSpan(ctx, 200, nil)
156
157	span := trace.FromContext(ctx)
158	if span == nil {
159		t.Fatal("StartSpan failed, expected non-nil span")
160	}
161}
162