forked from krakend/krakend-opencensus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackdriver.go
42 lines (35 loc) · 1.24 KB
/
stackdriver.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
package stackdriver
import (
"context"
"errors"
"time"
"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource"
opencensus "github.com/maysaraodeh/krakend-opencensus/v2"
)
func init() {
opencensus.RegisterExporterFactories(func(ctx context.Context, cfg opencensus.Config) (interface{}, error) {
return Exporter(ctx, cfg)
})
}
var defaultMetricPrefix = "krakend"
func Exporter(ctx context.Context, cfg opencensus.Config) (*stackdriver.Exporter, error) {
if cfg.Exporters.Stackdriver == nil {
return nil, errors.New("stackdriver exporter disabled")
}
if cfg.Exporters.Stackdriver.MetricPrefix == "" {
cfg.Exporters.Stackdriver.MetricPrefix = defaultMetricPrefix
}
labels := &stackdriver.Labels{}
for k, v := range cfg.Exporters.Stackdriver.DefaultLabels {
labels.Set(k, v, "")
}
return stackdriver.NewExporter(stackdriver.Options{
ProjectID: cfg.Exporters.Stackdriver.ProjectID,
MetricPrefix: cfg.Exporters.Stackdriver.MetricPrefix,
BundleDelayThreshold: time.Duration(cfg.ReportingPeriod) * time.Second,
BundleCountThreshold: cfg.SampleRate,
DefaultMonitoringLabels: labels,
MonitoredResource: monitoredresource.Autodetect(),
})
}