-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.go
239 lines (202 loc) · 7.99 KB
/
metric.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package metric
import (
"context"
"fmt"
"os"
"time"
"github.com/kinneko-de/restaurant-document-generate-svc/internal/app/operation/logger"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"github.com/kinneko-de/restaurant-document-generate-svc/build"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
api "go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
"github.com/go-logr/zerologr"
)
const ServiceNameEnv = "OTEL_SERVICE_NAME"
const OtelMetricEndpointEnv = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT"
const MetricNameDocumentPreviewRequested = "restaurant.documents.preview.requested"
const MetricDescriptionDocumentPreviewRequested = "Sum of requested document previews"
const MetricNameDocumentPreviewDelivered = "restaurant.documents.preview.delivered"
const MetricDescriptionDocumentPreviewDelivered = "Sum of document previews that was delivered fully to the client"
const MetricNameDocumentGenerateSuccessful = "restaurant.documents.generate.successful"
const MetricDescriptionDocumentGenerateSuccessful = "Sum of documents that were generated successfully"
const MetricNameDocumentGenerateFailed = "restaurant.documents.generate.failed"
const MetricDescriptionDocumentGenerateFailed = "Sum of documents that failed to generate due to an error"
const MetricNameDocumentGenerateDuration = "restaurant.documents.generate.duration" // "Duration of document generation" Unit: "ms" Histogram
const MetricDescriptionDocumentGenerateDuration = "The duration of the document generation"
const MetricAttributeDocumentType = "document_type"
var (
config otelConfig
version = "0.2.0"
ctx = context.Background()
provider *metric.MeterProvider
meter api.Meter
previewRequested api.Int64Counter
previewDelivered api.Int64Counter
documentGenerateSuccessful api.Int64Counter
documentGenerateFailed api.Int64Counter
documentGenerateDuration api.Float64Histogram
)
func InitializeMetrics() (*metric.MeterProvider, error) {
metricLogger := zerologr.New(&logger.Logger)
otel.SetLogger(metricLogger)
err := readConfig()
if err != nil {
return nil, err
}
provider, err := initializeOpenTelemetry()
return provider, err
}
func initializeOpenTelemetry() (*metric.MeterProvider, error) {
ressource, err := createRessource()
if err != nil {
return nil, err
}
readers, err := createReader()
if err != nil {
return nil, err
}
views := createViews()
provider := createProvider(ressource, readers, views)
metricError := createMetrics(provider)
return provider, metricError
}
func createRessource() (*resource.Resource, error) {
res, err := resource.Merge(resource.Default(),
resource.NewWithAttributes(semconv.SchemaURL,
semconv.ServiceNameKey.String(config.OtelServiceName),
semconv.ServiceVersionKey.String(build.Version),
))
if err != nil {
return nil, fmt.Errorf("failed to create ressource for metric reader: %w", err)
}
return res, nil
}
func createViews() []metric.View {
view := metric.NewView(
metric.Instrument{
Name: MetricNameDocumentGenerateDuration,
Kind: metric.InstrumentKindHistogram,
},
metric.Stream{
Aggregation: metric.AggregationExplicitBucketHistogram{
NoMinMax: true,
Boundaries: []float64{1000, 4000, 7000, 10000, 20000},
},
},
)
return []metric.View{view}
}
func createReader() ([]metric.Reader, error) {
otelGrpcExporter, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithInsecure(), otlpmetricgrpc.WithEndpoint(config.OtelMetricEndpoint))
if err != nil {
return nil, fmt.Errorf("failed to initialize metric reader to otel collector: %w", err)
}
otelReader := metric.NewPeriodicReader(otelGrpcExporter)
consoleExporter, err := stdoutmetric.New()
if err != nil {
return nil, fmt.Errorf("failed to initialize metric reader to console: %w", err)
}
consoleReader := metric.NewPeriodicReader(consoleExporter)
return []metric.Reader{otelReader, consoleReader}, nil
}
func createProvider(ressource *resource.Resource, readers []metric.Reader, views []metric.View) *metric.MeterProvider {
options := []metric.Option{
metric.WithResource(ressource),
metric.WithView(views...),
}
for _, reader := range readers {
options = append(options, metric.WithReader(reader))
}
provider = metric.NewMeterProvider(
options...,
)
otel.SetMeterProvider(provider)
return provider
}
// https://opentelemetry.io/docs/specs/otel/metrics/semantic_conventions/
func createMetrics(provider *metric.MeterProvider) error {
// I decided to use the service name here as scope because this service is a microservice. one sccope per service approach.
meter = provider.Meter(config.OtelServiceName, api.WithInstrumentationVersion(version))
var err error
errorTemplate := "failed to initialize metric '%v' %w"
previewRequested, err = meter.Int64Counter(
MetricNameDocumentPreviewRequested,
api.WithDescription(MetricDescriptionDocumentPreviewRequested))
if err != nil {
return fmt.Errorf(errorTemplate, MetricNameDocumentPreviewRequested, err)
}
previewDelivered, err = meter.Int64Counter(
MetricNameDocumentPreviewDelivered,
api.WithDescription(MetricDescriptionDocumentPreviewDelivered))
if err != nil {
return fmt.Errorf(errorTemplate, MetricNameDocumentPreviewDelivered, err)
}
documentGenerateSuccessful, err = meter.Int64Counter(
MetricNameDocumentGenerateSuccessful,
api.WithDescription(MetricDescriptionDocumentGenerateSuccessful))
if err != nil {
return fmt.Errorf(errorTemplate, MetricNameDocumentGenerateSuccessful, err)
}
documentGenerateFailed, err = meter.Int64Counter(
MetricNameDocumentGenerateFailed,
api.WithDescription(MetricDescriptionDocumentGenerateFailed))
if err != nil {
return fmt.Errorf(errorTemplate, MetricNameDocumentGenerateFailed, err)
}
documentGenerateDuration, err = meter.Float64Histogram(
MetricNameDocumentGenerateDuration,
api.WithDescription(MetricDescriptionDocumentGenerateDuration),
api.WithUnit("ms"))
if err != nil {
return fmt.Errorf(errorTemplate, MetricNameDocumentGenerateDuration, err)
}
return nil
}
func PreviewRequested() {
previewRequested.Add(ctx, 1)
}
func PreviewDelivered() {
previewDelivered.Add(ctx, 1)
}
func DocumentGenerated(documentType string, duration time.Duration, err error) {
if err != nil {
documentGenerateFailed.Add(ctx, 1, api.WithAttributes(attribute.Key(MetricAttributeDocumentType).String(documentType)))
} else {
documentGenerateSuccessful.Add(ctx, 1, api.WithAttributes(attribute.Key(MetricAttributeDocumentType).String(documentType)))
documentGenerateDuration.Record(ctx, float64(duration.Milliseconds()), api.WithAttributes(attribute.Key(MetricAttributeDocumentType).String(documentType)))
}
}
func ForceFlush() {
provider.ForceFlush(ctx)
}
func readConfig() error {
otelConfig, err := loadConfig()
if err != nil {
return err
}
config = otelConfig
return nil
}
type otelConfig struct {
OtelMetricEndpoint string // is used by the otel sdk to identify the endpoint to send metrics to. According to document it Will be set implicitly by the otel sdk. But it does not work. I set it explicitly.
OtelServiceName string // is used by the otel sdk to identify the service name. I found no way to set it explicitly by the otel sdk. According to the specification setting an attribute with name "service.name" should work, but it does not.
}
func loadConfig() (otelConfig, error) {
endpoint, found := os.LookupEnv(OtelMetricEndpointEnv)
if !found {
return otelConfig{}, fmt.Errorf("otel metric endpoint is not configured. Expected environment variable %v", OtelMetricEndpointEnv)
}
serviceName, found := os.LookupEnv(ServiceNameEnv)
if !found {
return otelConfig{}, fmt.Errorf("otel service name is not configured. Expected environment variable %v", ServiceNameEnv)
}
return otelConfig{
OtelMetricEndpoint: endpoint,
OtelServiceName: serviceName,
}, nil
}