-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathtimeseries.go
59 lines (45 loc) · 1.99 KB
/
timeseries.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package metrics
import (
"context"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/gcp"
)
// timeSeriesGrouped groups TimeSeries responses into common Elasticsearch friendly events. This is to avoid sending
// events with a single metric that shares info (like timestamp) with another event with a single metric too
func (m *MetricSet) timeSeriesGrouped(ctx context.Context, gcpService gcp.MetadataService, tsas []timeSeriesWithAligner, e *incomingFieldExtractor) map[string][]KeyValuePoint {
eventGroups := make(map[string][]KeyValuePoint)
metadataService := gcpService
for _, tsa := range tsas {
aligner := tsa.aligner
for _, ts := range tsa.timeSeries {
keyValues := e.extractTimeSeriesMetricValues(ts, aligner)
sdCollectorInputData := gcp.NewStackdriverCollectorInputData(ts, m.config.ProjectID, m.config.Zone, m.config.Region, m.config.Regions)
if gcpService == nil {
metadataService = gcp.NewStackdriverMetadataServiceForTimeSeries(ts)
}
for i := range keyValues {
sdCollectorInputData.Timestamp = &keyValues[i].Timestamp
id, err := metadataService.ID(ctx, sdCollectorInputData)
if err != nil {
m.Logger().Errorf("error trying to retrieve ID from metric event '%v'", err)
continue
}
metadataCollectorData, err := metadataService.Metadata(ctx, sdCollectorInputData.TimeSeries)
if err != nil {
m.Logger().Error("error trying to retrieve labels from metric event")
continue
}
if _, ok := eventGroups[id]; !ok {
eventGroups[id] = make([]KeyValuePoint, 0)
}
keyValues[i].ECS = metadataCollectorData.ECS
keyValues[i].Labels = metadataCollectorData.Labels
// Group the data into common events
eventGroups[id] = append(eventGroups[id], keyValues[i])
}
}
}
return eventGroups
}