Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zmoog committed Dec 29, 2023
1 parent 62613ca commit fabacd6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 50 deletions.
4 changes: 2 additions & 2 deletions x-pack/metricbeat/module/azure/client_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ func compareMetricValues(metVal *float64, metricVal *float64) bool {
return false
}

// convertTimeGrainToDuration converts the Azure time grain options to the equivalent
// asDuration converts the Azure time grain options to the equivalent
// `time.Duration` value.
//
// For example, converts "PT1M" to `time.Minute`.
//
// See https://docs.microsoft.com/en-us/azure/azure-monitor/platform/metrics-supported#time-grain
// for more information.
func convertTimeGrainToDuration(timeGrain string) time.Duration {
func asDuration(timeGrain string) time.Duration {
var duration time.Duration
switch timeGrain {
case "PT1M":
Expand Down
59 changes: 11 additions & 48 deletions x-pack/metricbeat/module/azure/metric_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,56 +49,27 @@ func (m *MetricRegistry) NeedsUpdate(referenceTime time.Time, metric Metric) boo
// resource ID and metric names.
metricKey := m.buildMetricKey(metric)

//// Get the now time in UTC, only to be used for logging.
//// It's interesting to see when the registry evaluate each
//// metric in relation to the reference time.
//now := time.Now().UTC()

if lastCollection, exists := m.collectionsInfo[metricKey]; exists {
// Turn the time grain into a duration (for example, PT5M -> 5 minutes).
timeGrainDuration := convertTimeGrainToDuration(lastCollection.timeGrain)

//// Calculate the start time of the time grain in relation to
//// the reference time.
//timeGrainStartTime := referenceTime.Add(-timeGrainDuration)

//// Only to be used for logging.
////
//// The time elapsed since the last collection, and the time
//// distance between last collection and the start of time
//// grain.
//elapsed := referenceTime.Sub(lastCollection.timestamp)
//distance := lastCollection.timestamp.Sub(timeGrainStartTime)

// If the last collection time is after the start time of the time grain,
// it means that we already have a value for the given time grain.
//
// In this case, the metricset does not need to collect the metric
// values again.
//
// if time.Since(metricsByGrain.metricsValuesUpdated).Seconds() < float64(timeGrains[compositeKey.timeGrain]) {
//if lastCollection.timestamp.After(timeGrainStartTime.Add(m.jitter)) {
lastCollectionSeconds := time.Since(lastCollection.timestamp).Seconds()
timeGrainSeconds := timeGrainDuration.Seconds()

if time.Since(lastCollection.timestamp).Seconds() < timeGrainDuration.Seconds() {
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,
//"now", now,
//"time_grain_start_time", timeGrainStartTime,
"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, ","),
//"elapsed", elapsed.String(),
//"jitter", m.jitter.String(),
//"distance", distance.String(),
"last_collection_seconds", lastCollectionSeconds,
"time_grain_seconds", timeGrainSeconds,
)

return false
Expand All @@ -110,19 +81,14 @@ func (m *MetricRegistry) NeedsUpdate(referenceTime time.Time, metric Metric) boo
"MetricRegistry: Metric needs an update",
"needs_update", true,
"reference_time", referenceTime,
//"now", now,
//"time_grain_start_time", timeGrainStartTime,
"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, ","),
//"elapsed", elapsed.String(),
//"jitter", m.jitter.String(),
//"distance", distance.String(),
"last_collection_seconds", lastCollectionSeconds,
"time_grain_seconds", timeGrainSeconds,
)

return true
Expand All @@ -136,13 +102,10 @@ func (m *MetricRegistry) NeedsUpdate(referenceTime time.Time, metric Metric) boo
"MetricRegistry: Metric needs an update (no collection info in the metric registry)",
"needs_update", true,
"reference_time", referenceTime,
//"now", now,
"time_grain", metric.TimeGrain,
"resource_id", metric.ResourceId,
"namespace", metric.Namespace,
"aggregation", metric.Aggregations,
"names", strings.Join(metric.Names, ","),
//"jitter", m.jitter.String(),
)

return true
Expand Down

0 comments on commit fabacd6

Please sign in to comment.