-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathexample_http_client_test.go
44 lines (34 loc) · 1.34 KB
/
example_http_client_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
// (c) Copyright IBM Corp. 2023
//go:build go1.18
// +build go1.18
package instabeego_test
import (
"context"
"log"
"net/http"
"github.com/beego/beego/v2/client/httplib"
instana "github.com/instana/go-sensor"
"github.com/instana/go-sensor/instrumentation/instabeego"
"github.com/opentracing/opentracing-go/ext"
)
// This example shows how to instrument beego httplib module (HTTP client) with Instana tracing
func Example_httpClientInstrumentation() {
t := instana.InitCollector(&instana.Options{
Service: "my-http-client",
EnableAutoProfile: true,
})
// Every call should start with an entry span (https://docs.instana.io/quick_start/custom_tracing/#always-start-new-traces-with-entry-spans)
// Normally this would be your HTTP/GRPC/message queue request span, but here we need to create it explicitly, since an HTTP client call is
// an exit span. And all exit spans must have a parent entry span.
sp := t.StartSpan("client-call")
sp.SetTag(string(ext.SpanKind), "entry")
defer sp.Finish()
// Inject the parent span into request context
ctx := instana.ContextWithSpan(context.Background(), sp)
req := httplib.NewBeegoRequestWithCtx(ctx, "https://www.instana.com", http.MethodGet)
instabeego.InstrumentRequest(t, req)
_, err := req.Response()
if err != nil {
log.Fatalf("failed to GET https://www.instana.com: %s", err)
}
}