-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathobservability_client_example.go
More file actions
150 lines (124 loc) · 3.87 KB
/
observability_client_example.go
File metadata and controls
150 lines (124 loc) · 3.87 KB
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
147
148
149
150
package statsig
import (
"context"
"errors"
"sync"
)
type Metric struct {
Name string `json:"name"`
Type string `json:"type"`
Value float64 `json:"value"`
Tags map[string]interface{} `json:"tags"`
}
type observabilityClientExample struct {
incrementMetrics []Metric
gaugeMetrics []Metric
distributionMetrics []Metric
mu sync.RWMutex
}
func NewObservabilityClientExample() *observabilityClientExample {
return &observabilityClientExample{
incrementMetrics: make([]Metric, 0),
gaugeMetrics: make([]Metric, 0),
distributionMetrics: make([]Metric, 0),
}
}
func (o *observabilityClientExample) Init(ctx context.Context) error {
return nil
}
func (o *observabilityClientExample) Increment(metricName string, value int, tags map[string]interface{}) error {
o.mu.Lock()
defer o.mu.Unlock()
o.incrementMetrics = append(o.incrementMetrics, Metric{
Name: metricName,
Type: "increment",
Value: float64(value),
Tags: tags,
})
return nil
}
func (o *observabilityClientExample) Gauge(metricName string, value float64, tags map[string]interface{}) error {
o.mu.Lock()
defer o.mu.Unlock()
o.gaugeMetrics = append(o.gaugeMetrics, Metric{
Name: metricName,
Type: "gauge",
Value: value,
Tags: tags,
})
return nil
}
func (o *observabilityClientExample) Distribution(metricName string, value float64, tags map[string]interface{}) error {
o.mu.Lock()
defer o.mu.Unlock()
o.distributionMetrics = append(o.distributionMetrics, Metric{
Name: metricName,
Type: "distribution",
Value: value,
Tags: tags,
})
return nil
}
func (o *observabilityClientExample) ShouldEnableHighCardinalityForThisTag(tag string) bool {
return true
}
func (o *observabilityClientExample) Shutdown(ctx context.Context) error {
return nil
}
func (o *observabilityClientExample) GetMetrics(metricType string) []Metric {
o.mu.RLock()
defer o.mu.RUnlock()
if metricType == "" {
totalLength := len(o.incrementMetrics) + len(o.gaugeMetrics) + len(o.distributionMetrics)
metrics := make([]Metric, 0, totalLength)
metrics = append(metrics, o.incrementMetrics...)
metrics = append(metrics, o.gaugeMetrics...)
metrics = append(metrics, o.distributionMetrics...)
return metrics
}
switch metricType {
case "increment":
metrics := make([]Metric, len(o.incrementMetrics))
copy(metrics, o.incrementMetrics)
return metrics
case "gauge":
metrics := make([]Metric, len(o.gaugeMetrics))
copy(metrics, o.gaugeMetrics)
return metrics
case "distribution":
metrics := make([]Metric, len(o.distributionMetrics))
copy(metrics, o.distributionMetrics)
return metrics
default:
return []Metric{}
}
}
func (o *observabilityClientExample) ClearMetrics() {
o.mu.Lock()
defer o.mu.Unlock()
o.incrementMetrics = make([]Metric, 0)
o.gaugeMetrics = make([]Metric, 0)
o.distributionMetrics = make([]Metric, 0)
}
type brokenObservabilityClientExample struct{}
func NewBrokenObservabilityClientExample() *brokenObservabilityClientExample {
return &brokenObservabilityClientExample{}
}
func (o *brokenObservabilityClientExample) Init(ctx context.Context) error {
return errors.New("init failed")
}
func (o *brokenObservabilityClientExample) Increment(metricName string, value int, tags map[string]interface{}) error {
return errors.New("increment failed")
}
func (o *brokenObservabilityClientExample) Gauge(metricName string, value float64, tags map[string]interface{}) error {
return errors.New("gauge failed")
}
func (o *brokenObservabilityClientExample) Distribution(metricName string, value float64, tags map[string]interface{}) error {
return errors.New("distribution failed")
}
func (o *brokenObservabilityClientExample) ShouldEnableHighCardinalityForThisTag(tag string) bool {
return false
}
func (o *brokenObservabilityClientExample) Shutdown(ctx context.Context) error {
return errors.New("shutdown failed")
}