-
Notifications
You must be signed in to change notification settings - Fork 8
/
metric_service.go
190 lines (173 loc) · 5.69 KB
/
metric_service.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package services
import (
"errors"
"log"
"strconv"
"strings"
"time"
"github.com/caraml-dev/mlp/api/pkg/instrumentation/metrics"
"github.com/caraml-dev/xp/common/api/schema"
_segmenters "github.com/caraml-dev/xp/common/segmenters"
"github.com/caraml-dev/xp/treatment-service/config"
"github.com/caraml-dev/xp/treatment-service/instrumentation"
"github.com/caraml-dev/xp/treatment-service/models"
)
type MetricService interface {
LogFetchTreatmentMetrics(
begin time.Time,
projectId models.ProjectId,
treatment schema.SelectedTreatment,
requestFilter map[string][]*_segmenters.SegmenterValue,
statusCode int,
)
LogLatencyHistogram(begin time.Time, labels map[string]string, loggingMetric metrics.MetricName)
LogRequestCount(labels map[string]string, loggingMetric metrics.MetricName)
// GetProjectNameLabel retrieves only project name as labels
GetProjectNameLabel(projectId models.ProjectId) map[string]string
GetMetricLabels() []string
// GetLabels retrieves labels with flag to filter for segmenters
GetLabels(projectId models.ProjectId, treatment schema.SelectedTreatment, statusCode int,
requestFilter map[string][]*_segmenters.SegmenterValue, withSegmenters bool) map[string]string
SetMetricsCollector(collector metrics.Collector)
}
type metricService struct {
Kind config.MetricSinkKind
LocalStorage *models.LocalStorage
MetricLabels []string
}
func NewMetricService(cfg config.Monitoring, localStorage *models.LocalStorage) (MetricService, error) {
switch cfg.Kind {
case config.NoopMetricSink, config.RPCMetricSink:
case config.PrometheusMetricSink:
// Init metrics collector
histogramMap := instrumentation.GetHistogramMap()
counterMap := instrumentation.GetCounterMap(cfg.MetricLabels)
err := metrics.InitPrometheusMetricsCollector(instrumentation.GaugeMap, histogramMap, counterMap)
if err != nil {
return nil, errors.New("failed to initialize Prometheus-based MetricService")
}
}
svc := &metricService{
Kind: cfg.Kind,
LocalStorage: localStorage,
MetricLabels: cfg.MetricLabels,
}
return svc, nil
}
func (ms *metricService) SetMetricsCollector(collector metrics.Collector) {
metrics.SetGlobMetricsCollector(collector)
}
func (ms *metricService) LogLatencyHistogram(begin time.Time, labels map[string]string, loggingMetric metrics.MetricName) {
var err error
switch ms.Kind {
case config.NoopMetricSink:
case config.PrometheusMetricSink, config.RPCMetricSink:
switch loggingMetric {
case instrumentation.FetchTreatmentRequestDurationMs:
err = metrics.Glob().MeasureDurationMsSince(
instrumentation.FetchTreatmentRequestDurationMs, begin, labels,
)
case instrumentation.ExperimentLookupDurationMs:
err = metrics.Glob().MeasureDurationMsSince(
instrumentation.ExperimentLookupDurationMs, begin, labels,
)
}
if err != nil {
log.Printf("error while logging %s metrics (latency): %s", loggingMetric, err)
}
}
}
func (ms *metricService) LogRequestCount(labels map[string]string, loggingMetric metrics.MetricName) {
var err error
switch ms.Kind {
case config.NoopMetricSink:
case config.PrometheusMetricSink, config.RPCMetricSink:
switch loggingMetric {
case instrumentation.FetchTreatmentRequestCount:
err = metrics.Glob().Inc(
instrumentation.FetchTreatmentRequestCount, labels,
)
case instrumentation.NoMatchingExperimentRequestCount:
err = metrics.Glob().Inc(
instrumentation.NoMatchingExperimentRequestCount, labels,
)
}
if err != nil {
log.Printf("error while logging metrics (request_count): %s", err)
}
}
}
func (ms *metricService) GetProjectNameLabel(projectId models.ProjectId) map[string]string {
settings := ms.LocalStorage.FindProjectSettingsWithId(projectId)
return map[string]string{
"project_name": settings.Username,
}
}
func (ms *metricService) GetMetricLabels() []string {
return ms.MetricLabels
}
func (ms *metricService) GetLabels(
projectId models.ProjectId,
treatment schema.SelectedTreatment,
statusCode int,
requestFilter map[string][]*_segmenters.SegmenterValue,
withSegmenters bool,
) map[string]string {
settings := ms.LocalStorage.FindProjectSettingsWithId(projectId)
labels := map[string]string{
"project_name": settings.Username,
"experiment_name": treatment.ExperimentName,
"treatment_name": treatment.Treatment.Name,
"response_code": strconv.Itoa(statusCode),
}
if withSegmenters {
// Set default value for required labels
for _, label := range ms.MetricLabels {
labels[label] = ""
if filterValues, ok := requestFilter[label]; ok {
// Do the convert and set labels[label]
strLabels := []string{}
for _, v := range filterValues {
switch v.Value.(type) {
case *_segmenters.SegmenterValue_Integer:
strLabels = append(strLabels, strconv.Itoa(int(v.GetInteger())))
case *_segmenters.SegmenterValue_String_:
strLabels = append(strLabels, v.GetString_())
}
}
labels[label] = strings.Join(strLabels, ",")
}
}
}
return labels
}
func (ms *metricService) LogFetchTreatmentMetrics(
begin time.Time,
projectId models.ProjectId,
treatment schema.SelectedTreatment,
requestFilter map[string][]*_segmenters.SegmenterValue,
statusCode int,
) {
labels := ms.GetLabels(
projectId,
treatment,
statusCode,
requestFilter,
false,
)
ms.LogLatencyHistogram(begin, labels, instrumentation.FetchTreatmentRequestDurationMs)
labels = ms.GetLabels(
projectId,
treatment,
statusCode,
requestFilter,
true,
)
if treatment.ExperimentName != "" || treatment.Treatment.Name != "" {
ms.LogRequestCount(labels, instrumentation.FetchTreatmentRequestCount)
} else {
delete(labels, "experiment_name")
delete(labels, "treatment_name")
ms.LogRequestCount(labels, instrumentation.NoMatchingExperimentRequestCount)
}
}