-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathmetric.go
634 lines (551 loc) · 19.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// Copyright 2020, Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metric
import (
"context"
"fmt"
"log"
"strings"
"time"
"go.opentelemetry.io/otel/api/global"
apimetric "go.opentelemetry.io/otel/api/metric"
export "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
"go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
"go.opentelemetry.io/otel/sdk/metric/controller/push"
"go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/resource"
monitoring "cloud.google.com/go/monitoring/apiv3/v2"
googlepb "github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/api/option"
googlemetricpb "google.golang.org/genproto/googleapis/api/metric"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
)
const (
version = "0.2.1"
cloudMonitoringMetricDescriptorNameFormat = "custom.googleapis.com/opentelemetry/%s"
)
// TODO: Remove when Count aggregation is used in the implementation
var _ = countToTypeValueAndTimestamp
// key is used to judge the uniqueness of the record descriptor.
type key struct {
name string
libraryname string
}
func keyOf(descriptor *apimetric.Descriptor) key {
return key{
name: descriptor.Name(),
libraryname: descriptor.InstrumentationName(),
}
}
// metricExporter is the implementation of OpenTelemetry metric exporter for
// Google Cloud Monitoring.
type metricExporter struct {
o *options
// mdCache is the cache to hold MetricDescriptor to avoid creating duplicate MD.
// TODO: [ymotongpoo] this map should be goroutine safe with mutex.
mdCache map[key]*googlemetricpb.MetricDescriptor
client *monitoring.MetricClient
// startTime is the cache of start time shared among all CUMULATIVE values.
// c.f. https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricKind
// TODO: Remove this when OTel SDK provides start time for each record specifically for stateful batcher.
startTime time.Time
}
// Below are maps with monitored resources fields as keys
// and OpenTelemetry resources fields as values
var k8sContainerMap = map[string]string{
"location": CloudKeyZone,
"cluster_name": K8SKeyClusterName,
"namespace_name": K8SKeyNamespaceName,
"pod_name": K8SKeyPodName,
"container_name": ContainerKeyName,
}
var k8sNodeMap = map[string]string{
"location": CloudKeyZone,
"cluster_name": K8SKeyClusterName,
"node_name": HostKeyName,
}
var k8sClusterMap = map[string]string{
"location": CloudKeyZone,
"cluster_name": K8SKeyClusterName,
}
var k8sPodMap = map[string]string{
"location": CloudKeyZone,
"cluster_name": K8SKeyClusterName,
"namespace_name": K8SKeyNamespaceName,
"pod_name": K8SKeyPodName,
}
var gceResourceMap = map[string]string{
"instance_id": HostKeyID,
"zone": CloudKeyZone,
}
var awsResourceMap = map[string]string{
"instance_id": HostKeyID,
"region": CloudKeyRegion,
"aws_account": CloudKeyAccountID,
}
// newMetricExporter returns an exporter that uploads OTel metric data to Google Cloud Monitoring.
func newMetricExporter(o *options) (*metricExporter, error) {
if strings.TrimSpace(o.ProjectID) == "" {
return nil, errBlankProjectID
}
clientOpts := append(o.MonitoringClientOptions, option.WithUserAgent(userAgent))
ctx := o.Context
if ctx == nil {
ctx = context.Background()
}
client, err := monitoring.NewMetricClient(ctx, clientOpts...)
if err != nil {
return nil, err
}
cache := map[key]*googlemetricpb.MetricDescriptor{}
e := &metricExporter{
o: o,
mdCache: cache,
client: client,
startTime: time.Now(),
}
return e, nil
}
// InstallNewPipeline instantiates a NewExportPipeline and registers it globally.
func InstallNewPipeline(opts []Option, popts ...push.Option) (*push.Controller, error) {
pusher, err := NewExportPipeline(opts, popts...)
if err != nil {
return nil, err
}
global.SetMeterProvider(pusher.MeterProvider())
return pusher, err
}
// NewExportPipeline sets up a complete export pipeline with the recommended setup,
// chaining a NewRawExporter into the recommended selectors and integrators.
func NewExportPipeline(opts []Option, popts ...push.Option) (*push.Controller, error) {
selector := NewWithCloudMonitoringDistribution()
exporter, err := NewRawExporter(opts...)
if err != nil {
return nil, err
}
period := exporter.metricExporter.o.ReportingInterval
checkpointer := basic.New(selector, exporter)
pusher := push.New(
checkpointer,
exporter,
append([]push.Option{
push.WithPeriod(period),
}, popts...)...,
)
pusher.Start()
return pusher, nil
}
// ExportMetrics exports OpenTelemetry Metrics to Google Cloud Monitoring.
func (me *metricExporter) ExportMetrics(ctx context.Context, cps export.CheckpointSet) error {
if err := me.exportMetricDescriptor(ctx, cps); err != nil {
return err
}
if err := me.exportTimeSeries(ctx, cps); err != nil {
return err
}
return nil
}
// exportMetricDescriptor create MetricDescriptor from the record
// if the descriptor is not registered in Cloud Monitoring yet.
func (me *metricExporter) exportMetricDescriptor(ctx context.Context, cps export.CheckpointSet) error {
mds := make(map[key]*googlemetricpb.MetricDescriptor)
aggError := cps.ForEach(export.CumulativeExporter, func(r export.Record) error {
k := keyOf(r.Descriptor())
if _, ok := me.mdCache[k]; ok {
return nil
}
if _, localok := mds[k]; !localok {
md := me.recordToMdpb(&r)
mds[k] = md
}
return nil
})
if aggError != nil {
return aggError
}
if len(mds) == 0 {
return nil
}
// TODO: This process is synchronous and blocks longer time if records in cps
// have many different descriptors. In the cps.ForEach above, it should spawn
// goroutines to send CreateMetricDescriptorRequest asynchronously in the case
// the descriptor does not exist in global cache (me.mdCache).
// See details in #26.
for kmd, md := range mds {
err := me.createMetricDescriptorIfNeeded(ctx, md)
if err != nil {
return err
}
me.mdCache[kmd] = md
}
return nil
}
func (me *metricExporter) createMetricDescriptorIfNeeded(ctx context.Context, md *googlemetricpb.MetricDescriptor) error {
mdReq := &monitoringpb.GetMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s/metricDescriptors/%s", me.o.ProjectID, md.Type),
}
_, err := me.client.GetMetricDescriptor(ctx, mdReq)
if err == nil {
// If the metric descriptor already exists, skip the CreateMetricDescriptor call.
// Metric descriptors cannot be updated without deleting them first, so there
// isn't anything we can do here:
// https://cloud.google.com/monitoring/custom-metrics/creating-metrics#md-modify
return nil
}
req := &monitoringpb.CreateMetricDescriptorRequest{
Name: fmt.Sprintf("projects/%s", me.o.ProjectID),
MetricDescriptor: md,
}
_, err = me.client.CreateMetricDescriptor(ctx, req)
return err
}
// exportTimeSeriees create TimeSeries from the records in cps.
// res should be the common resource among all TimeSeries, such as instance id, application name and so on.
func (me *metricExporter) exportTimeSeries(ctx context.Context, cps export.CheckpointSet) error {
tss := []*monitoringpb.TimeSeries{}
aggError := cps.ForEach(export.CumulativeExporter, func(r export.Record) error {
ts, err := me.recordToTspb(&r)
if err != nil {
return err
}
tss = append(tss, ts)
return nil
})
if aggError != nil {
if me.o.onError != nil {
me.o.onError(aggError)
} else {
log.Printf("Error during exporting TimeSeries: %v", aggError)
}
}
req := &monitoringpb.CreateTimeSeriesRequest{
Name: fmt.Sprintf("projects/%s", me.o.ProjectID),
TimeSeries: tss,
}
return me.client.CreateTimeSeries(ctx, req)
}
// recordToTspb converts record to TimeSeries proto type with common resource.
// ref. https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries
func (me *metricExporter) recordToTspb(r *export.Record) (*monitoringpb.TimeSeries, error) {
m := me.recordToMpb(r)
mr := me.resourceToMonitoredResourcepb(r.Resource())
tv, t, err := me.recordToTypedValueAndTimestamp(r)
if err != nil {
return nil, err
}
p := &monitoringpb.Point{
Value: tv,
Interval: t,
}
return &monitoringpb.TimeSeries{
Metric: m,
Resource: mr,
Points: []*monitoringpb.Point{p},
}, nil
}
// descToMetricType converts descriptor to MetricType proto type.
// Basically this returns default value ("custom.googleapis.com/opentelemetry/[metric type]")
func (me *metricExporter) descToMetricType(desc *apimetric.Descriptor) string {
if formatter := me.o.MetricDescriptorTypeFormatter; formatter != nil {
return formatter(desc)
}
return fmt.Sprintf(cloudMonitoringMetricDescriptorNameFormat, desc.Name())
}
// recordToMdpb extracts data and converts them to googlemetricpb.MetricDescriptor.
func (me *metricExporter) recordToMdpb(record *export.Record) *googlemetricpb.MetricDescriptor {
desc := record.Descriptor()
name := desc.Name()
unit := record.Descriptor().Unit()
kind, typ := recordToMdpbKindType(record)
// Detailed explanations on MetricDescriptor proto is not documented on
// generated Go packages. Refer to the original proto file.
// https://github.com/googleapis/googleapis/blob/50af053/google/api/metric.proto#L33
return &googlemetricpb.MetricDescriptor{
Name: name,
Type: me.descToMetricType(desc),
MetricKind: kind,
ValueType: typ,
Unit: string(unit),
Description: desc.Description(),
}
}
// refer to the monitored resources fields
// https://cloud.google.com/monitoring/api/resources
func subdivideGCPTypes(labelMap map[string]string) (string, map[string]string) {
_, hasLocation := labelMap[CloudKeyZone]
_, hasClusterName := labelMap[K8SKeyClusterName]
_, hasNamespaceName := labelMap[K8SKeyNamespaceName]
_, hasPodName := labelMap[K8SKeyPodName]
_, hasContainerName := labelMap[ContainerKeyName]
if hasLocation && hasClusterName && hasNamespaceName && hasPodName && hasContainerName {
return K8SContainer, k8sContainerMap
}
_, hasNodeName := labelMap[HostKeyName]
if hasLocation && hasClusterName && hasNodeName {
return K8SNode, k8sNodeMap
}
if hasLocation && hasClusterName && hasNamespaceName && hasPodName {
return K8SPod, k8sPodMap
}
if hasLocation && hasClusterName {
return K8SCluster, k8sClusterMap
}
return GCEInstance, gceResourceMap
}
// resourceToMonitoredResourcepb converts resource in OTel to MonitoredResource
// proto type for Cloud Monitoring.
//
// https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.monitoredResourceDescriptors
func (me *metricExporter) resourceToMonitoredResourcepb(res *resource.Resource) *monitoredrespb.MonitoredResource {
monitoredRes := &monitoredrespb.MonitoredResource{
Type: "global",
Labels: map[string]string{
"project_id": me.o.ProjectID,
},
}
// Return "global" Monitored resources if the input resource is null or empty
// "global" only accepts "project_id" for label.
// https://cloud.google.com/monitoring/api/resources#tag_global
if res == nil || res.Len() == 0 {
return monitoredRes
}
resLabelMap := generateResLabelMap(res)
resTypeStr := "global"
match := map[string]string{}
if resType, found := resLabelMap[CloudKeyProvider]; found {
switch resType {
case CloudProviderGCP:
resTypeStr, match = subdivideGCPTypes(resLabelMap)
case CloudProviderAWS:
resTypeStr = AWSEC2Instance
match = awsResourceMap
}
outputMap, isMissing := transformResource(match, resLabelMap)
if isMissing {
resTypeStr = "global"
} else {
monitoredRes.Labels = outputMap
}
}
monitoredRes.Type = resTypeStr
monitoredRes.Labels["project_id"] = me.o.ProjectID
return monitoredRes
}
func generateResLabelMap(res *resource.Resource) map[string]string {
resLabelMap := make(map[string]string)
for _, label := range res.Attributes() {
resLabelMap[string(label.Key)] = label.Value.AsString()
}
return resLabelMap
}
// returns transformed label map and false if all labels in match are found
// in input except optional project_id. It returns true if at least one label
// other than project_id is missing.
func transformResource(match, input map[string]string) (map[string]string, bool) {
output := make(map[string]string, len(input))
for dst, src := range match {
if v, ok := input[src]; ok {
output[dst] = v
} else {
return map[string]string{}, true
}
}
return output, false
}
// recordToMdpbKindType return the mapping from OTel's record descriptor to
// Cloud Monitoring's MetricKind and ValueType.
func recordToMdpbKindType(r *export.Record) (googlemetricpb.MetricDescriptor_MetricKind, googlemetricpb.MetricDescriptor_ValueType) {
mkind := r.Descriptor().MetricKind()
nkind := r.Descriptor().NumberKind()
var kind googlemetricpb.MetricDescriptor_MetricKind
switch mkind {
// TODO: Decide how UpDownCounterKind and UpDownSumObserverKind should be handled.
// CUMULATIVE might not be correct as it assumes the metric always goes up.
case apimetric.CounterKind, apimetric.UpDownCounterKind, apimetric.SumObserverKind, apimetric.UpDownSumObserverKind:
kind = googlemetricpb.MetricDescriptor_CUMULATIVE
case apimetric.ValueObserverKind, apimetric.ValueRecorderKind:
kind = googlemetricpb.MetricDescriptor_GAUGE
default:
kind = googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED
}
var typ googlemetricpb.MetricDescriptor_ValueType
switch nkind {
case apimetric.Int64NumberKind:
typ = googlemetricpb.MetricDescriptor_INT64
case apimetric.Float64NumberKind:
typ = googlemetricpb.MetricDescriptor_DOUBLE
default:
typ = googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED
}
// TODO: Add handling for MeasureKind if necessary.
return kind, typ
}
// recordToMpb converts data from records to Metric proto type for Cloud Monitoring.
func (me *metricExporter) recordToMpb(r *export.Record) *googlemetricpb.Metric {
desc := r.Descriptor()
k := keyOf(desc)
md, ok := me.mdCache[k]
if !ok {
md = me.recordToMdpb(r)
}
labels := make(map[string]string)
iter := r.Labels().Iter()
for iter.Next() {
kv := iter.Label()
labels[normalizeLabelKey(string(kv.Key))] = kv.Value.AsString()
}
return &googlemetricpb.Metric{
Type: md.Type,
Labels: labels,
}
}
// recordToTypedValueAndTimestamp converts measurement value stored in r to
// correspoinding counterpart in monitoringpb, and extracts timestamp of the record
// and convert it to appropriate TimeInterval.
//
// TODO: Apply appropriate TimeInterval based upon MetricKind. See details in the doc.
// https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries#Point
// See detils in #25.
func (me *metricExporter) recordToTypedValueAndTimestamp(r *export.Record) (*monitoringpb.TypedValue, *monitoringpb.TimeInterval, error) {
agg := r.Aggregation()
nkind := r.Descriptor().NumberKind()
mkind := r.Descriptor().MetricKind()
now := time.Now().Unix()
// TODO: Ignoring the case for Min, Max and Distribution to simply
// the first implementation.
//
// NOTE: Currently the selector used in the integrator is our own implementation,
// because none of those in go.opentelemetry.io/otel/sdk/metric/selector/simple
// gives interfaces to fetch LastValue.
//
// Views API should provide better interface that does not require the complicated codition handling
// done in this function.
// https://github.com/open-telemetry/opentelemetry-specification/issues/466
// In OpenCensus, view interface provided the bundle of name, measure, labels and aggregation in one place,
// and it should return the appropriate value based on the aggregation type specified there.
switch mkind {
case apimetric.ValueObserverKind, apimetric.ValueRecorderKind:
if lv, ok := agg.(*lastvalue.Aggregator); ok {
return lastValueToTypedValueAndTimestamp(lv, nkind)
}
return nil, nil, errUnsupportedAggregation{agg: agg}
case apimetric.CounterKind, apimetric.UpDownCounterKind, apimetric.SumObserverKind, apimetric.UpDownSumObserverKind:
// CUMULATIVE measurement should have the same start time and increasing end time.
// c.f. https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors#MetricKind
if sum, ok := agg.(*sum.Aggregator); ok {
return sumToTypedValueAndTimestamp(sum, nkind, me.startTime.Unix(), now)
}
return nil, nil, errUnsupportedAggregation{agg: agg}
}
return nil, nil, errUnexpectedMetricKind{kind: mkind}
}
func countToTypeValueAndTimestamp(count *minmaxsumcount.Aggregator, kind apimetric.NumberKind, start, end int64) (*monitoringpb.TypedValue, *monitoringpb.TimeInterval, error) {
value, err := count.Count()
if err != nil {
return nil, nil, err
}
t := &monitoringpb.TimeInterval{
StartTime: &googlepb.Timestamp{
Seconds: start,
},
EndTime: &googlepb.Timestamp{
Seconds: end,
},
}
switch kind {
case apimetric.Int64NumberKind:
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: value,
},
}, t, nil
case apimetric.Float64NumberKind:
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_DoubleValue{
DoubleValue: float64(value),
},
}, t, nil
}
return nil, nil, errUnexpectedNumberKind{kind: kind}
}
func lastValueToTypedValueAndTimestamp(lv *lastvalue.Aggregator, kind apimetric.NumberKind) (*monitoringpb.TypedValue, *monitoringpb.TimeInterval, error) {
value, timestamp, err := lv.LastValue()
if err != nil {
return nil, nil, err
}
// TODO: Consider the expression of TimeInterval (#25)
t := &monitoringpb.TimeInterval{
StartTime: &googlepb.Timestamp{
Seconds: timestamp.Unix(),
},
EndTime: &googlepb.Timestamp{
Seconds: timestamp.Unix(),
},
}
tv, err := aggToTypedValue(kind, value)
if err != nil {
return nil, nil, err
}
return tv, t, nil
}
func sumToTypedValueAndTimestamp(sum *sum.Aggregator, kind apimetric.NumberKind, start, end int64) (*monitoringpb.TypedValue, *monitoringpb.TimeInterval, error) {
value, err := sum.Sum()
if err != nil {
return nil, nil, err
}
t := &monitoringpb.TimeInterval{
StartTime: &googlepb.Timestamp{
Seconds: start,
},
EndTime: &googlepb.Timestamp{
Seconds: end,
},
}
tv, err := aggToTypedValue(kind, value)
if err != nil {
return nil, nil, err
}
return tv, t, err
}
func aggToTypedValue(kind apimetric.NumberKind, value apimetric.Number) (*monitoringpb.TypedValue, error) {
switch kind {
case apimetric.Int64NumberKind:
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: value.AsInt64(),
},
}, nil
case apimetric.Float64NumberKind:
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_DoubleValue{
DoubleValue: value.AsFloat64(),
},
}, nil
}
return nil, errUnexpectedNumberKind{kind: kind}
}
// https://github.com/googleapis/googleapis/blob/c4c562f89acce603fb189679836712d08c7f8584/google/api/metric.proto#L149
//
// > The label key name must follow:
// >
// > * Only upper and lower-case letters, digits and underscores (_) are
// > allowed.
// > * Label name must start with a letter or digit.
// > * The maximum length of a label name is 100 characters.
func normalizeLabelKey(s string) string {
return strings.ReplaceAll(s, ".", "_")
}