/
client.go
53 lines (42 loc) · 1.16 KB
/
client.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
package main
import (
"fmt"
"net/http"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/theplant/appkit/log"
"github.com/theplant/appkit/tracing"
)
func main() {
log := log.Default()
// Hacky way to configure the global tracer
closer, _, err := tracing.Tracer(log)
if err != nil {
panic(err)
}
defer closer.Close()
span := opentracing.StartSpan("client")
defer span.Finish()
httpClient := &http.Client{}
httpReq, _ := http.NewRequest("GET", "http://localhost:9900/", nil)
// Transmit the span's TraceContext as HTTP headers on our
// outbound request.
opentracing.GlobalTracer().Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(httpReq.Header))
ext.HTTPMethod.Set(span, httpReq.Method)
ext.HTTPUrl.Set(span, httpReq.URL.String())
ext.SpanKind.Set(span, ext.SpanKindRPCClientEnum)
resp, err := httpClient.Do(httpReq)
if err != nil {
ext.Error.Set(span, true)
span.LogKV("error", err)
} else {
if resp.StatusCode >= 400 {
ext.Error.Set(span, true)
}
ext.HTTPStatusCode.Set(span, uint16(resp.StatusCode))
}
fmt.Println("response", resp, err)
}