This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
stackdriver_test.go
178 lines (150 loc) · 4.84 KB
/
stackdriver_test.go
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2020, OpenCensus Authors
//
// 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.
package stackdriver
import (
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"contrib.go.opencensus.io/exporter/stackdriver/internal/testpb"
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource/gcp"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"golang.org/x/net/context/ctxhttp"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var dummyAutodetect = func() gcp.Interface {
return nil
}
func init() {
// monitoredresource.Autodetect() takes a few seconds to return when
// run outside of a cloud environment, so use a dummy autodetect for tests
autodetectFunc = dummyAutodetect
}
func TestExport(t *testing.T) {
projectID, ok := os.LookupEnv("STACKDRIVER_TEST_PROJECT_ID")
if !ok {
t.Skip("STACKDRIVER_TEST_PROJECT_ID not set")
}
var exportErrors []error
exporter, err := NewExporter(Options{ProjectID: projectID, OnError: func(err error) {
exportErrors = append(exportErrors, err)
}})
if err != nil {
t.Fatal(err)
}
defer exporter.Flush()
trace.RegisterExporter(exporter)
defer trace.UnregisterExporter(exporter)
view.RegisterExporter(exporter)
defer view.UnregisterExporter(exporter)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
_, span := trace.StartSpan(context.Background(), "custom-span")
time.Sleep(10 * time.Millisecond)
span.End()
// Test HTTP spans
handler := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
_, backgroundSpan := trace.StartSpan(context.Background(), "BackgroundWork")
spanContext := backgroundSpan.SpanContext()
time.Sleep(10 * time.Millisecond)
backgroundSpan.End()
_, span := trace.StartSpan(req.Context(), "Sleep")
span.AddLink(trace.Link{Type: trace.LinkTypeChild, TraceID: spanContext.TraceID, SpanID: spanContext.SpanID})
time.Sleep(150 * time.Millisecond) // do work
span.End()
rw.Write([]byte("Hello, world!"))
})
server := httptest.NewServer(&ochttp.Handler{Handler: handler})
defer server.Close()
ctx := context.Background()
client := &http.Client{
Transport: &ochttp.Transport{},
}
resp, err := ctxhttp.Get(ctx, client, server.URL+"/test/123?abc=xyz")
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if want, got := "Hello, world!", string(body); want != got {
t.Fatalf("resp.Body = %q; want %q", want, got)
}
// Flush twice to expose issue of exporter creating traces internally (#557)
exporter.Flush()
exporter.Flush()
for _, err := range exportErrors {
t.Error(err)
}
}
func TestGRPC(t *testing.T) {
projectID, ok := os.LookupEnv("STACKDRIVER_TEST_PROJECT_ID")
if !ok {
t.Skip("STACKDRIVER_TEST_PROJECT_ID not set")
}
exporter, err := NewExporter(Options{ProjectID: projectID})
if err != nil {
t.Fatal(err)
}
defer exporter.Flush()
trace.RegisterExporter(exporter)
defer trace.UnregisterExporter(exporter)
view.RegisterExporter(exporter)
defer view.UnregisterExporter(exporter)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
client, done := testpb.NewTestClient(t)
defer done()
client.Single(context.Background(), &testpb.FooRequest{SleepNanos: int64(42 * time.Millisecond)})
}
func TestUserAgent(t *testing.T) {
e, err := NewExporter(Options{UserAgent: "OpenCensus Service"})
if err != nil {
t.Fatal(err)
}
if want, got := "OpenCensus Service", e.statsExporter.o.UserAgent; want != got {
t.Fatalf("UserAgent = %q; want %q", got, want)
}
}
func TestClose(t *testing.T) {
projectID, ok := os.LookupEnv("STACKDRIVER_TEST_PROJECT_ID")
if !ok {
t.Skip("STACKDRIVER_TEST_PROJECT_ID not set")
}
conn, err := grpc.Dial("monitoring.googleapis.com:443", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("cannot configure grpc conn: %v", err)
}
copts := []option.ClientOption{option.WithGRPCConn(conn)}
// option.WithGRPCConn option takes precedent over all other supplied options so the
// following user agent will be used by both exporters
exporter, err := NewExporter(Options{
ProjectID: projectID,
MonitoringClientOptions: copts,
TraceClientOptions: copts,
})
if err != nil {
t.Fatal(err)
}
err = exporter.Close()
if err != nil {
t.Fatal(err)
}
}