-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.go
138 lines (117 loc) · 3.43 KB
/
env.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package svc
import (
"context"
"fmt"
"log"
"os"
"github.com/opentracing/opentracing-go"
"github.com/remind101/pkg/logger"
"github.com/remind101/pkg/metrics"
"github.com/remind101/pkg/reporter"
"github.com/remind101/pkg/reporter/rollbar"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/opentracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
// Env holds global dependencies that need to be initialized in main() and
// injected as dependencies into an application.
type Env struct {
Reporter reporter.Reporter
Logger logger.Logger
Context context.Context
Close func() // Should be called in a defer in main().
}
// InitAll will initialize all the common dependencies such as metrics, reporting,
// tracing, and logging.
func InitAll() Env {
traceCloser := InitTracer()
metricsCloser := InitMetrics()
l := InitLogger()
logger.DefaultLogger = l
r := InitReporter()
ctx := reporter.WithReporter(context.Background(), r)
ctx = logger.WithLogger(ctx, l)
go func() {
defer reporter.Monitor(ctx)
metrics.Runtime()
}()
return Env{
Logger: l,
Reporter: r,
Context: ctx,
Close: func() {
traceCloser()
metricsCloser()
},
}
}
// InitTracer configures a global datadog tracer.
//
// Env Vars:
// * DDTRACE_ADDR - The host:port of the local trace agent server.
// * EMPIRE_APPNAME - App name, used to construct the service name.
// * EMPIRE_PROCESS - Process name, used to construct the service name.
func InitTracer() func() {
var opts []tracer.StartOption
// create a Tracer configuration
opts = append(opts, tracer.WithService(fmt.Sprintf(
"%s.%s",
os.Getenv("EMPIRE_APPNAME"),
os.Getenv("EMPIRE_PROCESS"))))
if addr := os.Getenv("DDTRACE_ADDR"); addr != "" {
opts = append(opts, tracer.WithAgentAddr(addr))
}
// Initialize a Tracer and ensure a graceful shutdown
// using the `closer.Close()`
tracer := opentracer.New(opts...)
// set the Datadog tracer as a GlobalTracer
opentracing.SetGlobalTracer(tracer)
return func() {
// I can't find a way to flush in the new tracer API
}
}
// InitMetrics configures pkg/metrics
//
// Env Vars:
// * STATSD_ADDR - The host:port of the statsd server.
func InitMetrics() func() {
if addr := os.Getenv("STATSD_ADDR"); addr != "" {
metrics.SetEmpireDefaultTags()
metrics.Reporter, _ = metrics.NewDataDogMetricsReporter(addr)
}
return func() {
metrics.Close()
}
}
// InitLogger configures a leveled logger.
//
// Env Vars:
// * LOG_LEVEL - The log level
//
// If you want to replace the global default logger:
// logger.DefaultLogger = InitLogger()
func InitLogger() logger.Logger {
lvl := logger.ERROR
if ll := os.Getenv("LOG_LEVEL"); ll != "" {
lvl = logger.ParseLevel(ll)
}
return logger.New(log.New(os.Stdout, "", 0), lvl)
}
// InitReporter configures and returns a reporter.Reporter instance.
//
// Env Vars:
// * ROLLBAR_ACCESS_TOKEN - The Rollbar access token
// * ROLLBAR_ENVIRONMENT - The Rollbar environment (staging, production)
// * ROLLBAR_ENDPOINT - The Rollbar endpoint: https://api.rollbar.com/api/1/item/
func InitReporter() reporter.Reporter {
rep := reporter.MultiReporter{}
// Log Reporter, uses package level logger.
rep = append(rep, reporter.NewLogReporter())
// Rollbar reporter
if os.Getenv(rollbar.EnvAccessToken) != "" && os.Getenv(rollbar.EnvEnvironment) != "" {
rollbar.ConfigureFromEnvironment()
rep = append(rep, rollbar.Reporter)
} else {
fmt.Println("Rollbar is not configured, skipping Rollbar reporter")
}
return rep
}