-
Notifications
You must be signed in to change notification settings - Fork 73
/
exporters.go
92 lines (73 loc) · 2.79 KB
/
exporters.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
package config
import (
"fmt"
"time"
)
type (
serverConfig struct {
Telemetry serverTelemetryConfig `yaml:",omitempty" mapstructure:"telemetry"`
}
serverTelemetryConfig struct {
Exporter string `yaml:",omitempty" mapstructure:"exporter"`
ApplicationExporter string `yaml:",omitempty" mapstructure:"applicationExporter"`
DataStore string `yaml:",omitempty" mapstructure:"dataStore"`
}
telemetry struct {
ApplicationProfiler ApplicationProfiler `yaml:",omitempty" mapstructure:"profiler"`
Exporters map[string]TelemetryExporterOption `yaml:",omitempty" mapstructure:"exporters"`
}
TelemetryExporterOption struct {
ServiceName string `yaml:",omitempty" mapstructure:"serviceName"`
Sampling float64 `yaml:",omitempty" mapstructure:"sampling"`
MetricsReaderInterval time.Duration `yaml:",omitempty" mapstructure:"metricsReaderInterval"`
Exporter ExporterConfig `yaml:",omitempty" mapstructure:"exporter"`
}
ExporterConfig struct {
Type string `yaml:",omitempty" mapstructure:"type"`
CollectorConfiguration OTELCollectorConfig `yaml:"collector,omitempty" mapstructure:"collector"`
}
OTELCollectorConfig struct {
Endpoint string `yaml:",omitempty" mapstructure:"endpoint"`
}
ApplicationProfiler struct {
Name string `yaml:",omitempty" mapstructure:"name"`
Enabled bool `yaml:",omitempty" mapstructure:"enabled"`
Endpoint string `yaml:",omitempty" mapstructure:"endpoint"`
SamplingRate int `yaml:",omitempty" mapstructure:"samplingRate"`
Environment string `yaml:",omitempty" mapstructure:"environment"`
}
)
func (c *AppConfig) ApplicationProfiler() *ApplicationProfiler {
c.mu.Lock()
defer c.mu.Unlock()
return &c.config.Telemetry.ApplicationProfiler
}
func (c *AppConfig) Exporter() (*TelemetryExporterOption, error) {
c.mu.Lock()
defer c.mu.Unlock()
return c.getExporter(c.config.Server.Telemetry.Exporter)
}
func (c *AppConfig) ApplicationExporter() (*TelemetryExporterOption, error) {
c.mu.Lock()
defer c.mu.Unlock()
return c.getExporter(c.config.Server.Telemetry.ApplicationExporter)
}
func (c *AppConfig) getExporter(name string) (*TelemetryExporterOption, error) {
// Exporters are optional: if no name was provided we consider that the user don't want to have them enabled
if name == "" {
return nil, nil
}
exporterConfig, found := c.config.Telemetry.Exporters[name]
if !found {
availableOptions := mapKeys(c.config.Telemetry.Exporters)
return nil, fmt.Errorf(`invalid exporter option: "%s". Available options: %v`, name, availableOptions)
}
return &exporterConfig, nil
}
func mapKeys[T any](m map[string]T) []string {
keys := make([]string, 0)
for key := range m {
keys = append(keys, key)
}
return keys
}