-
Notifications
You must be signed in to change notification settings - Fork 4
/
fromenv.go
76 lines (60 loc) · 1.88 KB
/
fromenv.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
// Package fromenv provides utilities to create lile options from environment
// variables. fromenv will error with fatal if it cannot resolve or errors
package fromenv
import (
"log"
"os"
"github.com/lileio/pubsub/v2"
"github.com/lileio/pubsub/v2/providers/google"
opentracing "github.com/opentracing/opentracing-go"
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
"github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/reporter"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
"github.com/sirupsen/logrus"
)
var zipkinReporter reporter.Reporter
func Tracer(name string) opentracing.Tracer {
zipkinHost := os.Getenv("USE_ZIPKIN")
if zipkinHost == "" {
return opentracing.GlobalTracer()
}
addr := "http://zipkin/api/v1/spans"
if os.Getenv("ZIPKIN_ADDR") != "" {
addr = os.Getenv("ZIPKIN_ADDR")
}
// create our local service endpoint
endpoint, _ := zipkin.NewEndpoint(name, name)
logrus.Infof("Using Zipkin HTTP tracer: %s", addr)
zipkinReporter = zipkinhttp.NewReporter(addr)
// initialize our tracer
nativeTracer, err := zipkin.NewTracer(zipkinReporter, zipkin.WithLocalEndpoint(endpoint))
if err != nil {
log.Fatalf("unable to create tracer: %+v\n", err)
}
// use zipkin-go-opentracing to wrap our tracer
tracer := zipkinot.Wrap(nativeTracer)
// optionally set as Global OpenTracing tracer instance
opentracing.SetGlobalTracer(tracer)
return tracer
}
func PubSubProvider() pubsub.Provider {
gpid := os.Getenv("GOOGLE_PUBSUB_PROJECT_ID")
if gpid != "" {
gc, err := google.NewGoogleCloud(gpid)
if err != nil {
logrus.Fatalf("fronenv: Google Cloud pubsub err: %s", err)
return nil
}
logrus.Infof("Using Google Cloud pubsub: %s", gpid)
return gc
}
logrus.Warn("Using noop pubsub provider")
return pubsub.NoopProvider{}
}
func Shutdown() error {
if zipkinReporter != nil {
return zipkinReporter.Close()
}
return nil
}