Skip to content

Commit d214b40

Browse files
authored
refactor: update all unit tests in instrumetation libraries with InitCollector API (#1036)
refactor: update all unit tests in instrumentation libraries with InitCollector API
1 parent 462115a commit d214b40

File tree

91 files changed

+1768
-1138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+1768
-1138
lines changed

example_httpserver_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import (
1313

1414
// This example shows how to instrument an HTTP server with Instana tracing
1515
func Example_tracingNamedHandlerFunc() {
16-
sensor := instana.NewSensor("my-http-server")
16+
c := instana.InitCollector(&instana.Options{
17+
Service: "my-http-server",
18+
})
19+
defer instana.ShutdownCollector()
1720

1821
// To instrument a handler function, pass it as an argument to instana.TracingHandlerFunc()
19-
http.HandleFunc("/", instana.TracingHandlerFunc(sensor, "/", func(w http.ResponseWriter, req *http.Request) {
22+
http.HandleFunc("/", instana.TracingHandlerFunc(c, "/", func(w http.ResponseWriter, req *http.Request) {
2023
// Extract the parent span and use its tracer to initialize any child spans to trace the calls
2124
// inside the handler, e.g. database queries, 3rd-party API requests, etc.
2225
if parent, ok := instana.SpanFromContext(req.Context()); ok {
@@ -32,7 +35,7 @@ func Example_tracingNamedHandlerFunc() {
3235
// In case your handler is implemented as an http.Handler, pass its ServeHTTP method instead.
3336
// You can also use instana.TracingNamedHandlerFunc() to provide a unique route identifier to
3437
// group the calls to this route later in Instana UI.
35-
http.HandleFunc("/files", instana.TracingNamedHandlerFunc(sensor, "index", "/:path", http.FileServer(http.Dir("./")).ServeHTTP))
38+
http.HandleFunc("/files", instana.TracingNamedHandlerFunc(c, "index", "/:path", http.FileServer(http.Dir("./")).ServeHTTP))
3639

3740
if err := http.ListenAndServe(":0", nil); err != nil {
3841
log.Fatalf("failed to start server: %s", err)
@@ -42,7 +45,10 @@ func Example_tracingNamedHandlerFunc() {
4245
// This example demonstrates how to instrument a 3rd-party HTTP router that uses pattern matching to make sure
4346
// the original path template is forwarded to Instana
4447
func Example_httpRoutePatternMatching() {
45-
sensor := instana.NewSensor("my-http-server")
48+
c := instana.InitCollector(&instana.Options{
49+
Service: "my-http-server",
50+
})
51+
defer instana.ShutdownCollector()
4652

4753
// Initialize your router. Here for simplicity we use stdlib http.ServeMux, however you
4854
// can use any router that supports http.Handler/http.HandlerFunc, such as github.com/gorilla/mux
@@ -53,7 +59,7 @@ func Example_httpRoutePatternMatching() {
5359
// in UI as `http.path_tpl` if the request path is different (we assume that this request
5460
// has been routed to the handler via a matched pattern)
5561
r.HandleFunc("/articles/{category}/{id:[0-9]+}", instana.TracingHandlerFunc(
56-
sensor,
62+
c,
5763
"/articles/{category}/{id:[0-9]+}",
5864
func(w http.ResponseWriter, req *http.Request) {
5965
// ...

example_instrumentation_go1.10_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ import (
1414
func ExampleWrapSQLConnector() {
1515
// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended
1616
// to use a single instance throughout your application
17-
sensor := instana.NewSensor("my-http-client")
17+
c := instana.InitCollector(&instana.Options{
18+
Service: "my-http-client",
19+
})
20+
defer instana.ShutdownCollector()
1821

1922
// Instrument the connector. Normally this would be a type provided by the driver library.
2023
// Here we use a test mock to avoid bringing external dependencies.
2124
//
2225
// Note that instana.WrapSQLConnector() requires the connection string to send it later
2326
// along with database spans.
24-
connector := instana.WrapSQLConnector(sensor, "driver connection string", &sqlConnector{})
27+
connector := instana.WrapSQLConnector(c, "driver connection string", &sqlConnector{})
2528

2629
// Use wrapped connector to initialize the database client. Note that
2730
db := sql.OpenDB(connector)
2831

2932
// Inject parent span into the context
30-
span := sensor.Tracer().StartSpan("entry")
33+
span := c.Tracer().StartSpan("entry")
3134
ctx := instana.ContextWithSpan(context.Background(), span)
3235

3336
// Query the database, passing the context containing the active span

example_instrumentation_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import (
1515
func ExampleTracingHandlerFunc() {
1616
// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended
1717
// to use a single instance throughout your application
18-
sensor := instana.NewSensor("my-http-server")
18+
c := instana.InitCollector(&instana.Options{
19+
Service: "my-http-server",
20+
})
21+
defer instana.ShutdownCollector()
1922

20-
http.HandleFunc("/", instana.TracingNamedHandlerFunc(sensor, "root", "/", func(w http.ResponseWriter, req *http.Request) {
23+
http.HandleFunc("/", instana.TracingNamedHandlerFunc(c, "root", "/", func(w http.ResponseWriter, req *http.Request) {
2124
// handler code
2225
}))
2326
}
@@ -26,16 +29,20 @@ func ExampleTracingHandlerFunc() {
2629
func ExampleRoundTripper() {
2730
// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended
2831
// to use a single instance throughout your application
29-
sensor := instana.NewSensor("my-http-client")
30-
span := sensor.Tracer().StartSpan("entry")
32+
c := instana.InitCollector(&instana.Options{
33+
Service: "my-http-server",
34+
})
35+
defer instana.ShutdownCollector()
36+
37+
span := c.Tracer().StartSpan("entry")
3138

3239
// Make sure to finish the span so it can be properly recorded in the backend
3340
defer span.Finish()
3441

3542
// http.DefaultTransport is used as a default RoundTripper, however you can provide
3643
// your own implementation
3744
client := &http.Client{
38-
Transport: instana.RoundTripper(sensor, nil),
45+
Transport: instana.RoundTripper(c, nil),
3946
}
4047

4148
// Inject parent span into the request context
@@ -50,17 +57,20 @@ func ExampleRoundTripper() {
5057
func ExampleSQLOpen() {
5158
// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended
5259
// to use a single instance throughout your application
53-
sensor := instana.NewSensor("my-http-client")
60+
c := instana.InitCollector(&instana.Options{
61+
Service: "my-http-server",
62+
})
63+
defer instana.ShutdownCollector()
5464

5565
// Instrument the driver. Normally this would be a type provided by the driver library, e.g.
5666
// pq.Driver{} or mysql.Driver{}, but here we use a test mock to avoid bringing external dependencies
57-
instana.InstrumentSQLDriver(sensor, "your_db_driver", sqlDriver{})
67+
instana.InstrumentSQLDriver(c, "your_db_driver", sqlDriver{})
5868

5969
// Replace sql.Open() with instana.SQLOpen()
6070
db, _ := instana.SQLOpen("your_db_driver", "driver connection string")
6171

6272
// Inject parent span into the context
63-
span := sensor.Tracer().StartSpan("entry")
73+
span := c.Tracer().StartSpan("entry")
6474
ctx := instana.ContextWithSpan(context.Background(), span)
6575

6676
// Query the database, passing the context containing the active span

instrumentation/cloud.google.com/go/pubsub/example_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ import (
1515
// This example show how to instrument and HTTP handler that receives Google Cloud Pub/Sub messages
1616
// via the push delivery method.
1717
func ExampleTracingHandlerFunc() {
18-
// Initialize sensor
19-
sensor := instana.NewSensor("pubsub-consumer")
18+
// Initialize collector
19+
c := instana.InitCollector(&instana.Options{
20+
Service: "pubsub-consumer",
21+
})
22+
defer instana.ShutdownCollector()
2023

2124
// Wrap your Pub/Sub message handler with pubsub.TracingHandlerFunc
22-
http.Handle("/", pubsub.TracingHandlerFunc(sensor, "/", func(w http.ResponseWriter, req *http.Request) {
25+
http.Handle("/", pubsub.TracingHandlerFunc(c, "/", func(w http.ResponseWriter, req *http.Request) {
2326
var delivery struct {
2427
Message struct {
2528
Data []byte `json:"data"`

instrumentation/cloud.google.com/go/pubsub/pubsub_test.go

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ func TestClient_Topic(t *testing.T) {
2727
defer teardown()
2828

2929
recorder := instana.NewTestRecorder()
30-
tracer := instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder)
30+
c := instana.InitCollector(&instana.Options{
31+
AgentClient: alwaysReadyClient{},
32+
Recorder: recorder,
33+
})
3134

32-
sensor := instana.NewSensorWithTracer(tracer)
33-
defer instana.ShutdownSensor()
35+
defer instana.ShutdownCollector()
3436

35-
pSpan := tracer.StartSpan("parent-span")
37+
pSpan := c.StartSpan("parent-span")
3638
ctx := context.Background()
3739
if pSpan != nil {
3840
ctx = instana.ContextWithSpan(ctx, pSpan)
@@ -45,23 +47,23 @@ func TestClient_Topic(t *testing.T) {
4547

4648
examples := map[string]func(*testing.T, *pubsub.Message) *pubsub.PublishResult{
4749
"ClientProject": func(t *testing.T, msg *pubsub.Message) *pubsub.PublishResult {
48-
client, err := pubsub.NewClient(ctx, "test-project", sensor, option.WithGRPCConn(conn))
50+
client, err := pubsub.NewClient(ctx, "test-project", c, option.WithGRPCConn(conn))
4951
require.NoError(t, err)
5052

5153
top := client.Topic("test-topic")
5254

5355
return top.Publish(ctx, msg)
5456
},
5557
"OtherProject": func(t *testing.T, msg *pubsub.Message) *pubsub.PublishResult {
56-
client, err := pubsub.NewClient(ctx, "other-project", sensor, option.WithGRPCConn(conn))
58+
client, err := pubsub.NewClient(ctx, "other-project", c, option.WithGRPCConn(conn))
5759
require.NoError(t, err)
5860

5961
top := client.TopicInProject("test-topic", "test-project")
6062

6163
return top.Publish(ctx, msg)
6264
},
6365
"CreateTopic": func(t *testing.T, msg *pubsub.Message) *pubsub.PublishResult {
64-
client, err := pubsub.NewClient(ctx, "test-project", sensor, option.WithGRPCConn(conn))
66+
client, err := pubsub.NewClient(ctx, "test-project", c, option.WithGRPCConn(conn))
6567
require.NoError(t, err)
6668

6769
top, err := client.CreateTopic(ctx, "new-test-topic")
@@ -70,7 +72,7 @@ func TestClient_Topic(t *testing.T) {
7072
return top.Publish(ctx, msg)
7173
},
7274
"CreateTopicWithConfig": func(t *testing.T, msg *pubsub.Message) *pubsub.PublishResult {
73-
client, err := pubsub.NewClient(ctx, "test-project", sensor, option.WithGRPCConn(conn))
75+
client, err := pubsub.NewClient(ctx, "test-project", c, option.WithGRPCConn(conn))
7476
require.NoError(t, err)
7577

7678
conf := &gpubsub.TopicConfig{
@@ -140,13 +142,11 @@ func TestClient_Topics(t *testing.T) {
140142
require.NoError(t, err)
141143
defer teardown()
142144

143-
sensor := instana.NewSensorWithTracer(
144-
instana.NewTracerWithEverything(
145-
instana.DefaultOptions(),
146-
instana.NewTestRecorder(),
147-
),
148-
)
149-
defer instana.ShutdownSensor()
145+
c := instana.InitCollector(&instana.Options{
146+
AgentClient: alwaysReadyClient{},
147+
Recorder: instana.NewTestRecorder(),
148+
})
149+
defer instana.ShutdownCollector()
150150

151151
topicNames := []string{"first-topic", "second-topic"}
152152

@@ -157,7 +157,7 @@ func TestClient_Topics(t *testing.T) {
157157
require.NoError(t, err)
158158
}
159159

160-
client, err := pubsub.NewClient(context.Background(), "test-project", sensor, option.WithGRPCConn(conn))
160+
client, err := pubsub.NewClient(context.Background(), "test-project", c, option.WithGRPCConn(conn))
161161
require.NoError(t, err)
162162

163163
var res []*pubsub.PublishResult
@@ -196,13 +196,11 @@ func TestClient_Subscription(t *testing.T) {
196196
require.NoError(t, err)
197197
defer teardown()
198198

199-
sensor := instana.NewSensorWithTracer(
200-
instana.NewTracerWithEverything(
201-
instana.DefaultOptions(),
202-
instana.NewTestRecorder(),
203-
),
204-
)
205-
defer instana.ShutdownSensor()
199+
c := instana.InitCollector(&instana.Options{
200+
AgentClient: alwaysReadyClient{},
201+
Recorder: instana.NewTestRecorder(),
202+
})
203+
defer instana.ShutdownCollector()
206204

207205
top, err := srv.GServer.CreateTopic(context.Background(), &pb.Topic{
208206
Name: "projects/test-project/topics/test-topic",
@@ -221,7 +219,7 @@ func TestClient_Subscription(t *testing.T) {
221219
})
222220
require.NoError(t, err)
223221

224-
client, err := pubsub.NewClient(context.Background(), "test-project", sensor, option.WithGRPCConn(conn))
222+
client, err := pubsub.NewClient(context.Background(), "test-project", c, option.WithGRPCConn(conn))
225223
require.NoError(t, err)
226224

227225
return client.Subscription("test-subscription")
@@ -237,14 +235,14 @@ func TestClient_Subscription(t *testing.T) {
237235
})
238236
require.NoError(t, err)
239237

240-
client, err := pubsub.NewClient(context.Background(), "other-project", sensor, option.WithGRPCConn(conn))
238+
client, err := pubsub.NewClient(context.Background(), "other-project", c, option.WithGRPCConn(conn))
241239
require.NoError(t, err)
242240

243241
return client.SubscriptionInProject("test-subscription", "test-project")
244242
},
245243

246244
"CreateSubscriptionWithConfig": func(t *testing.T, topicName string) *pubsub.Subscription {
247-
client, err := pubsub.NewClient(context.Background(), "test-project", sensor, option.WithGRPCConn(conn))
245+
client, err := pubsub.NewClient(context.Background(), "test-project", c, option.WithGRPCConn(conn))
248246
require.NoError(t, err)
249247

250248
sub, err := client.CreateSubscription(context.Background(), "test-subscription", gpubsub.SubscriptionConfig{
@@ -288,13 +286,11 @@ func TestClient_Subscriptions(t *testing.T) {
288286
require.NoError(t, err)
289287
defer teardown()
290288

291-
sensor := instana.NewSensorWithTracer(
292-
instana.NewTracerWithEverything(
293-
instana.DefaultOptions(),
294-
instana.NewTestRecorder(),
295-
),
296-
)
297-
defer instana.ShutdownSensor()
289+
c := instana.InitCollector(&instana.Options{
290+
AgentClient: alwaysReadyClient{},
291+
Recorder: instana.NewTestRecorder(),
292+
})
293+
defer instana.ShutdownCollector()
298294

299295
top, err := srv.GServer.CreateTopic(context.Background(), &pb.Topic{
300296
Name: "projects/test-project/topics/test-topic",
@@ -311,7 +307,7 @@ func TestClient_Subscriptions(t *testing.T) {
311307
require.NoError(t, err)
312308
}
313309

314-
client, err := pubsub.NewClient(context.Background(), "test-project", sensor, option.WithGRPCConn(conn))
310+
client, err := pubsub.NewClient(context.Background(), "test-project", c, option.WithGRPCConn(conn))
315311
require.NoError(t, err)
316312

317313
var subs []string

instrumentation/cloud.google.com/go/pubsub/push_test.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import (
2424

2525
func TestTracingHandler(t *testing.T) {
2626
recorder := instana.NewTestRecorder()
27-
sensor := instana.NewSensorWithTracer(
28-
instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder),
29-
)
30-
defer instana.ShutdownSensor()
27+
c := instana.InitCollector(&instana.Options{
28+
AgentClient: alwaysReadyClient{},
29+
Recorder: recorder,
30+
})
31+
defer instana.ShutdownCollector()
3132

3233
payload, err := ioutil.ReadFile("testdata/message.json")
3334
require.NoError(t, err)
@@ -36,7 +37,7 @@ func TestTracingHandler(t *testing.T) {
3637
numCalls int
3738
reqSpan opentracing.Span
3839
)
39-
h := pubsub.TracingHandlerFunc(sensor, "/", func(w http.ResponseWriter, req *http.Request) {
40+
h := pubsub.TracingHandlerFunc(c, "/", func(w http.ResponseWriter, req *http.Request) {
4041
numCalls++
4142

4243
var ok bool
@@ -88,16 +89,17 @@ func TestTracingHandler(t *testing.T) {
8889

8990
func TestTracingHandlerFunc_TracePropagation(t *testing.T) {
9091
recorder := instana.NewTestRecorder()
91-
sensor := instana.NewSensorWithTracer(
92-
instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder),
93-
)
94-
defer instana.ShutdownSensor()
92+
c := instana.InitCollector(&instana.Options{
93+
AgentClient: alwaysReadyClient{},
94+
Recorder: recorder,
95+
})
96+
defer instana.ShutdownCollector()
9597

9698
payload, err := ioutil.ReadFile("testdata/message_with_context.json")
9799
require.NoError(t, err)
98100

99101
var numCalls int
100-
h := pubsub.TracingHandlerFunc(sensor, "/", func(w http.ResponseWriter, req *http.Request) {
102+
h := pubsub.TracingHandlerFunc(c, "/", func(w http.ResponseWriter, req *http.Request) {
101103
numCalls++
102104

103105
_, ok := instana.SpanFromContext(req.Context())
@@ -146,13 +148,14 @@ func TestTracingHandlerFunc_TracePropagation(t *testing.T) {
146148

147149
func TestTracingHandlerFunc_NotPubSub(t *testing.T) {
148150
recorder := instana.NewTestRecorder()
149-
sensor := instana.NewSensorWithTracer(
150-
instana.NewTracerWithEverything(&instana.Options{AgentClient: alwaysReadyClient{}}, recorder),
151-
)
152-
defer instana.ShutdownSensor()
151+
c := instana.InitCollector(&instana.Options{
152+
AgentClient: alwaysReadyClient{},
153+
Recorder: recorder,
154+
})
155+
defer instana.ShutdownCollector()
153156

154157
var numCalls int
155-
h := pubsub.TracingHandlerFunc(sensor, "/", func(w http.ResponseWriter, req *http.Request) {
158+
h := pubsub.TracingHandlerFunc(c, "/", func(w http.ResponseWriter, req *http.Request) {
156159
numCalls++
157160

158161
_, ok := instana.SpanFromContext(req.Context())

0 commit comments

Comments
 (0)