Skip to content

Commit

Permalink
Change format of metric ID to use the metric ID code insteads
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Feb 6, 2025
1 parent 1f6e9a0 commit ed35d9d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,11 @@ private void updateIngestedUsageCounterFor(Event event, String tag, Measurement
if (event.getBillingProvider() != null) {
counter.tag("billing_provider", event.getBillingProvider().value());
}
try {
counter
.tag("metric_id", MetricId.fromString(measurement.getMetricId()).toUpperCaseFormatted())
.withRegistry(meterRegistry)
.withTags("product", tag)
.increment(measurement.getValue());
} catch (Exception e) {
log.error(
"Error to increment counter '{}'. Event was '{}' and measurement '{}'",
INGESTED_USAGE_METRIC,
event,
measurement);
}
counter
.tag("metric_id", MetricId.tryGetValueFromString(measurement.getMetricId()))
.withRegistry(meterRegistry)
.withTags("product", tag)
.increment(measurement.getValue());
}

private static class ServiceInstancesResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package org.candlepin.subscriptions.tally;

import com.redhat.swatch.configuration.registry.MetricId;
import com.redhat.swatch.configuration.registry.SubscriptionDefinition;
import com.redhat.swatch.configuration.registry.Variant;
import io.micrometer.core.annotation.Timed;
Expand Down Expand Up @@ -142,7 +143,7 @@ protected void recordTallyCount(List<TallySnapshot> savedSnapshots) {
"product",
snap.getProductId(),
"metric_id",
entry.getKey().getMetricId(),
MetricId.tryGetValueFromString(entry.getKey().getMetricId()),
"billing_provider",
snap.getBillingProvider().getValue());
c.increment(entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ private Optional<Meter> getIngestedUsageMetric(
INGESTED_USAGE_METRIC.equals(m.getId().getName())
&& productTag.equals(m.getId().getTag("product"))
&& MetricId.fromString(metricId)
.toUpperCaseFormatted()
.getValue()
.equals(m.getId().getTag("metric_id"))
&& billingProvider.equals(m.getId().getTag("billing_provider")))
.findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void produceSnapshotsForOrg() {
.tags("product", "RHEL for x86", "billing_provider", BillingProvider.RED_HAT.getValue())
.withRegistry(registry);

for (var s : Set.of("CORES", "SOCKETS")) {
for (var s : Set.of("Cores", "Sockets")) {
var c = counter.withTag("metric_id", s);
assertEquals(10.0, c.count());
}
Expand Down Expand Up @@ -129,7 +129,7 @@ void produceHourlySnapshotsForOrg() {
.tags("product", "rosa", "billing_provider", BillingProvider.RED_HAT.getValue())
.withRegistry(registry);

for (var s : Set.of("CORES", "SOCKETS")) {
for (var s : Set.of("Cores", "Sockets")) {
var c = counter.withTag("metric_id", s);
assertEquals(10.0, c.count());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.redhat.swatch.billable.usage.kafka.streams;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.swatch.configuration.registry.MetricId;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.quarkus.arc.profile.UnlessBuildProfile;
Expand Down Expand Up @@ -116,7 +117,11 @@ private void traceAggregate(

counter
.withRegistry(meterRegistry)
.withTags("product", key.key().getProductId(), "metric_id", key.key().getMetricId())
.withTags(
"product",
key.key().getProductId(),
"metric_id",
MetricId.tryGetValueFromString(key.key().getMetricId()))
.increment(aggregate.getTotalValue().doubleValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private void updateUsageMeter(BillableUsage usage, double contractCoverage, doub
new ArrayList<>(
List.of(
"product", usage.getProductId(),
"metric_id", usage.getMetricId(),
"metric_id", MetricId.tryGetValueFromString(usage.getMetricId()),
"billing_provider", usage.getBillingProvider().value(),
"status", usage.getStatus().value()));
double coverage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.redhat.swatch.billable.usage.kafka.streams.BillableUsageAggregationStreamProperties;
import com.redhat.swatch.billable.usage.kafka.streams.StreamTopologyProducer;
import com.redhat.swatch.configuration.registry.MetricId;
import com.redhat.swatch.configuration.util.MetricIdUtils;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -230,7 +231,9 @@ private Optional<Meter> getIngestedUsageAggregatedMetric(
m ->
USAGE_TOTAL_AGGREGATED_METRIC.equals(m.getId().getName())
&& productTag.equals(m.getId().getTag("product"))
&& metricId.equals(m.getId().getTag("metric_id"))
&& MetricId.fromString(metricId)
.getValue()
.equals(m.getId().getTag("metric_id"))
&& billingProvider.equals(m.getId().getTag("billing_provider")))
.findFirst();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,9 @@ private Optional<Meter> getUsageMetric(
m ->
metric.equals(m.getId().getName())
&& productTag.equals(m.getId().getTag("product"))
&& metricId.equals(m.getId().getTag("metric_id"))
&& MetricId.fromString(metricId)
.getValue()
.equals(m.getId().getTag("metric_id"))
&& billingProvider.equals(m.getId().getTag("billing_provider")))
.findFirst();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,11 @@ private void incrementMeteredTotal(BillableUsageAggregate usage) {

swatchProducerMeteredTotal
.withRegistry(meterRegistry)
.withTags("product", aggregateKey.getProductId(), "metric_id", aggregateKey.getMetricId())
.withTags(
"product",
aggregateKey.getProductId(),
"metric_id",
MetricId.fromString(aggregateKey.getMetricId()).getValue())
.increment(usage.getTotalValue().doubleValue());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
// constructor is private so that the factory method is the only way to get a MetricId
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Slf4j
public class MetricId implements Serializable {

private final String value;
Expand Down Expand Up @@ -62,6 +64,19 @@ public static MetricId fromString(String value) {
String.format("MetricId: %s not found in configuration", value)));
}

/**
* Get the metric id value from a String. In case of it does not exist, it returns the string
* instead of throwing an exception.
*/
public static String tryGetValueFromString(String metricId) {
try {
return MetricId.fromString(metricId).getValue();
} catch (IllegalArgumentException e) {
log.warn("Failed to get the MetricId.value from {}", metricId);
return metricId;
}
}

public static Set<MetricId> getAll() {
return SubscriptionDefinitionRegistry.getInstance().getSubscriptions().stream()
.map(SubscriptionDefinition::getMetrics)
Expand Down

0 comments on commit ed35d9d

Please sign in to comment.