-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the reference time rounding on Azure Metrics #37365
Merged
zmoog
merged 11 commits into
elastic:main
from
zmoog:zmoog/round-reference-time-on-azure-metrics
Jan 5, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e8000a6
Replace Truncate() with Round()
zmoog 44da101
Add missing license header
zmoog 142f7fb
Fix linter objections
zmoog a86072a
Tests: look for precise nearest second value
zmoog b3402d9
Switch from round to compare with jitter
zmoog b6e4007
Compare elapsed with timegrain duration
zmoog a9f7104
Cleanup
zmoog 1ad7693
Cleanup
zmoog 6b4f3ab
Clean up
zmoog 6277f0d
Fix typo
zmoog cbf7167
Add changelog entry
zmoog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// 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 azure | ||
|
||
import ( | ||
"strings" | ||
"time" | ||
|
||
"github.com/elastic/elastic-agent-libs/logp" | ||
) | ||
|
||
// NewMetricRegistry instantiates a new metric registry. | ||
func NewMetricRegistry(logger *logp.Logger) *MetricRegistry { | ||
return &MetricRegistry{ | ||
logger: logger, | ||
collectionsInfo: make(map[string]MetricCollectionInfo), | ||
jitter: 1 * time.Second, | ||
} | ||
} | ||
|
||
// MetricRegistry keeps track of the last time a metric was collected and | ||
// the time grain used. | ||
// | ||
// This is used to avoid collecting the same metric values over and over again | ||
// when the time grain is larger than the collection interval. | ||
type MetricRegistry struct { | ||
logger *logp.Logger | ||
collectionsInfo map[string]MetricCollectionInfo | ||
// The collection period can be jittered by a second. | ||
// We introduce a small jitter to avoid skipping collections | ||
// when the collection period is close (usually < 1s) to the | ||
// time grain start time. | ||
jitter time.Duration | ||
} | ||
|
||
// Update updates the metric registry with the latest timestamp and | ||
// time grain for the given metric. | ||
func (m *MetricRegistry) Update(metric Metric, info MetricCollectionInfo) { | ||
m.collectionsInfo[m.buildMetricKey(metric)] = info | ||
} | ||
|
||
// NeedsUpdate returns true if the metric needs to be collected again | ||
// for the given `referenceTime`. | ||
func (m *MetricRegistry) NeedsUpdate(referenceTime time.Time, metric Metric) bool { | ||
// Build a key to store the metric in the registry. | ||
// The key is a combination of the namespace, | ||
// resource ID and metric names. | ||
metricKey := m.buildMetricKey(metric) | ||
|
||
if lastCollection, exists := m.collectionsInfo[metricKey]; exists { | ||
// Turn the time grain into a duration (for example, PT5M -> 5 minutes). | ||
timeGrainDuration := asDuration(lastCollection.timeGrain) | ||
|
||
// Adjust the last collection time by adding a small jitter to avoid | ||
// skipping collections when the collection period is close (usually < 1s). | ||
timeSinceLastCollection := time.Since(lastCollection.timestamp) + m.jitter | ||
|
||
if timeSinceLastCollection < timeGrainDuration { | ||
m.logger.Debugw( | ||
"MetricRegistry: Metric does not need an update", | ||
"needs_update", false, | ||
"reference_time", referenceTime, | ||
"last_collection_time", lastCollection.timestamp, | ||
"time_since_last_collection_seconds", timeSinceLastCollection.Seconds(), | ||
"time_grain", lastCollection.timeGrain, | ||
"time_grain_duration_seconds", timeGrainDuration.Seconds(), | ||
"resource_id", metric.ResourceId, | ||
"namespace", metric.Namespace, | ||
"aggregation", metric.Aggregations, | ||
"names", strings.Join(metric.Names, ","), | ||
) | ||
|
||
return false | ||
} | ||
|
||
// The last collection time is before the start time of the time grain, | ||
// it means that the metricset needs to collect the metric values again. | ||
m.logger.Debugw( | ||
"MetricRegistry: Metric needs an update", | ||
"needs_update", true, | ||
"reference_time", referenceTime, | ||
"last_collection_time", lastCollection.timestamp, | ||
"time_since_last_collection_seconds", timeSinceLastCollection.Seconds(), | ||
"time_grain", lastCollection.timeGrain, | ||
"time_grain_duration_seconds", timeGrainDuration.Seconds(), | ||
"resource_id", metric.ResourceId, | ||
"namespace", metric.Namespace, | ||
"aggregation", metric.Aggregations, | ||
"names", strings.Join(metric.Names, ","), | ||
) | ||
|
||
return true | ||
} | ||
|
||
// If the metric is not in the registry, it means that it has never | ||
// been collected before. | ||
// | ||
// In this case, we need to collect the metric. | ||
m.logger.Debugw( | ||
"MetricRegistry: Metric needs an update (no collection info in the metric registry)", | ||
"needs_update", true, | ||
"reference_time", referenceTime, | ||
"resource_id", metric.ResourceId, | ||
"namespace", metric.Namespace, | ||
"aggregation", metric.Aggregations, | ||
"names", strings.Join(metric.Names, ","), | ||
) | ||
|
||
return true | ||
} | ||
|
||
// buildMetricKey builds a key for the metric registry. | ||
// | ||
// The key is a combination of the namespace, resource ID and metric names. | ||
func (m *MetricRegistry) buildMetricKey(metric Metric) string { | ||
keyComponents := []string{ | ||
metric.Namespace, | ||
metric.ResourceId, | ||
} | ||
keyComponents = append(keyComponents, metric.Names...) | ||
|
||
return strings.Join(keyComponents, ",") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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 azure | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/elastic-agent-libs/logp" | ||
) | ||
|
||
func TestNewMetricRegistry(t *testing.T) { | ||
logger := logp.NewLogger("test azure monitor") | ||
|
||
t.Run("Collect metrics with a regular 5 minutes period", func(t *testing.T) { | ||
metricRegistry := NewMetricRegistry(logger) | ||
|
||
// Create a lastCollectionAt parsing the string 2023-12-08T16:37:50.000Z into a time.Time | ||
lastCollectionAt, _ := time.Parse(time.RFC3339, "2023-12-08T16:37:50.000Z") | ||
|
||
// Create a referenceTime parsing 2023-12-08T16:42:50.000Z into a time.Time | ||
referenceTime, _ := time.Parse(time.RFC3339, "2023-12-08T16:42:50.000Z") | ||
|
||
metric := Metric{ | ||
ResourceId: "test", | ||
Namespace: "test", | ||
} | ||
metricCollectionInfo := MetricCollectionInfo{ | ||
timeGrain: "PT5M", | ||
timestamp: lastCollectionAt, | ||
} | ||
|
||
metricRegistry.Update(metric, metricCollectionInfo) | ||
|
||
needsUpdate := metricRegistry.NeedsUpdate(referenceTime, metric) | ||
|
||
assert.True(t, needsUpdate, "metric should need update") | ||
}) | ||
|
||
t.Run("Collect metrics using a period 3 seconds longer than previous", func(t *testing.T) { | ||
metricRegistry := NewMetricRegistry(logger) | ||
|
||
// Create a lastCollectionAt parsing the string 2023-12-08T16:37:50.000Z into a time.Time | ||
lastCollectionAt, _ := time.Parse(time.RFC3339, "2023-12-08T16:37:50.000Z") | ||
|
||
// Create a referenceTime parsing 2023-12-08T16:42:50.000Z into a time.Time | ||
referenceTime, _ := time.Parse(time.RFC3339, "2023-12-08T16:42:53.000Z") | ||
|
||
metric := Metric{ | ||
ResourceId: "test", | ||
Namespace: "test", | ||
} | ||
metricCollectionInfo := MetricCollectionInfo{ | ||
timeGrain: "PT5M", | ||
timestamp: lastCollectionAt, | ||
} | ||
|
||
metricRegistry.Update(metric, metricCollectionInfo) | ||
|
||
needsUpdate := metricRegistry.NeedsUpdate(referenceTime, metric) | ||
|
||
assert.True(t, needsUpdate, "metric should need update") | ||
}) | ||
|
||
t.Run("Collect metrics using a period (1 second) shorter than previous", func(t *testing.T) { | ||
metricRegistry := NewMetricRegistry(logger) | ||
|
||
// Create a referenceTime parsing 2023-12-08T16:42:50.000Z into a time.Time | ||
referenceTime, _ := time.Parse(time.RFC3339, "2023-12-08T10:58:33.000Z") | ||
|
||
// Create a lastCollectionAt parsing the string 2023-12-08T16:37:50.000Z into a time.Time | ||
lastCollectionAt, _ := time.Parse(time.RFC3339, "2023-12-08T10:53:34.000Z") | ||
|
||
metric := Metric{ | ||
ResourceId: "test", | ||
Namespace: "test", | ||
} | ||
metricCollectionInfo := MetricCollectionInfo{ | ||
timeGrain: "PT5M", | ||
timestamp: lastCollectionAt, | ||
} | ||
|
||
metricRegistry.Update(metric, metricCollectionInfo) | ||
|
||
needsUpdate := metricRegistry.NeedsUpdate(referenceTime, metric) | ||
|
||
assert.True(t, needsUpdate, "metric should not need update") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a clean copy-paste?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied and pasted the license header from another file; let me check if I picked a bad one 👀