-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracer.go
68 lines (58 loc) · 1.41 KB
/
tracer.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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
trace "cloud.google.com/go/trace/apiv1"
gcloudtracer "github.com/lovoo/gcloud-opentracing"
basictracer "github.com/opentracing/basictracer-go"
opentracing "github.com/opentracing/opentracing-go"
"google.golang.org/api/option"
)
type Tracer struct {
recorder *gcloudtracer.Recorder
}
func (t *Tracer) Initialize() error {
filename := os.Getenv("GOOGLE_CREDENTIALS_FILE")
if filename == "" {
filename = "credentials.json"
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil
}
client, err := trace.NewClient(context.Background(), option.WithCredentialsFile(filename))
if err != nil {
log.Fatalf("error creating a tracing client: %v", err)
}
creds, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
var credsObj struct {
ProjectID string `json:"project_id"`
}
err = json.Unmarshal(creds, &credsObj)
if err != nil {
return err
}
recorder, err := gcloudtracer.NewRecorder(context.Background(), credsObj.ProjectID, client)
if err != nil {
log.Fatalf("error creating a recorder: %v", err)
}
t.recorder = recorder
opentracing.SetGlobalTracer(basictracer.NewWithOptions(basictracer.Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool {
return true
},
}))
return nil
}
func (t *Tracer) Close() error {
if t.recorder == nil {
return nil
}
return t.recorder.Close()
}