/
client.go
146 lines (126 loc) · 3.36 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
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
139
140
141
142
143
144
145
146
package analytics
import (
"os"
"strings"
"sync"
"time"
"github.com/common-fate/analytics-go/acore"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
DevEndpoint = "https://t-dev.commonfate.io"
DefaultEndpoint = "https://t.commonfate.io"
)
type Client struct {
mu *sync.Mutex
deploymentID *string
coreclient acore.Client
log *zap.Logger
// OnFailure is a callback fired if events are failed to be dispatched.
OnFailure func(e Event)
}
func logLevelFromEnv() zapcore.Level {
l, err := zapcore.ParseLevel(os.Getenv("CF_ANALYTICS_LOG_LEVEL"))
if err != nil {
return zap.PanicLevel
}
return l
}
func newClient(coreclient acore.Client, log *zap.Logger) *Client {
return &Client{
mu: &sync.Mutex{},
coreclient: coreclient,
log: log,
}
}
var (
// Disabled disables analytics altogether.
Disabled = Config{
Endpoint: "",
Enabled: false,
Verbose: false,
}
// Development uses https://t-dev.commonfate.io as the endpoint.
Development = Config{
Endpoint: DevEndpoint,
Enabled: true,
Verbose: true,
}
// Default uses https://t.commonfate.io as the endpoint.
Default = Config{
Endpoint: DefaultEndpoint,
Enabled: true,
Verbose: false,
}
)
// endpointOrDefault overrides the endpoint or returns the default
// endpoint (https://t.commonfate.io) if the endpoint is empty.
func endpointOrDefault(endpoint string) string {
if endpoint == "" {
return DefaultEndpoint
}
return endpoint
}
func defaultLogger() *zap.Logger {
return zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), os.Stderr, logLevelFromEnv())).Named("cf-analytics")
}
// New creates an analytics client.
// Usage:
//
// analytics.New(analytics.Development)
func New(c Config) *Client {
log := defaultLogger()
// create a no-op client if analytics are disabled.
if !c.Enabled {
return newClient(&acore.NoopClient{}, log)
}
client, err := acore.NewWithConfig(acore.Config{
Endpoint: c.Endpoint,
Verbose: c.Verbose,
Interval: time.Millisecond * 50,
BatchSize: 3,
})
if err != nil {
log.Error("error setting client", zap.Error(err))
return newClient(&acore.NoopClient{}, log)
}
log.Debug("configured analytics client", zap.Any("config", c))
return newClient(client, log)
}
// NewFromEnv sets up the analytics client based on the following
// parameters:
//
// - URL is CF_ANALYTICS_URL, or falls back to the default URL if not provided
// - Disabled if CF_ANALYTICS_DISABLED is true
func Env() Config {
return Config{
Endpoint: endpointOrDefault(os.Getenv("CF_ANALYTICS_URL")),
Enabled: strings.ToLower(os.Getenv("CF_ANALYTICS_DISABLED")) != "true",
Verbose: strings.ToLower(os.Getenv("CF_ANALYTICS_LOG_LEVEL")) == "debug",
}
}
// Config is configuration for the analytics client.
type Config struct {
Endpoint string `json:"endpoint"`
Enabled bool `json:"enabled"`
Verbose bool `json:"verbose"`
}
// Close the client.
func (c *Client) Close() {
c.log.Debug("closing analytics client", zap.String("url", c.coreclient.EndpointURL()))
err := c.coreclient.Close()
if err != nil {
c.log.Error("error closing client", zap.Error(err))
}
}
// SetDeploymentID sets the deployment ID.
func (c *Client) SetDeploymentID(depID string) {
if depID == "" {
return
}
c.mu.Lock()
defer c.mu.Unlock()
c.deploymentID = &depID
c.log.Debug("set deployment", zap.Any("deployment.id", depID))
}