Skip to content

[Java] Add Gauge Metric Extraction to DataflowMetrics #34307

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.apache.beam.model.pipeline.v1.MetricsApi.BoundedTrie;
import org.apache.beam.model.pipeline.v1.RunnerApi;
import org.apache.beam.runners.core.metrics.BoundedTrieData;
Expand All @@ -49,6 +50,7 @@
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableSet;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.joda.time.Instant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -170,6 +172,9 @@ public void addMetricResult(
// counter metric
Long value = getCounterValue(committed);
counterResults.add(MetricResult.create(metricKey, !isStreamingJob, value));
} else if (committed.getGauge() != null && attempted.getGauge() != null) {
GaugeResult value = getGaugeValue(committed);
gaugeResults.add(MetricResult.create(metricKey, !isStreamingJob, value));
} else if (committed.getSet() != null && attempted.getSet() != null) {
// stringset metric
StringSetResult value = getStringSetValue(committed);
Expand Down Expand Up @@ -225,6 +230,40 @@ private DistributionResult getDistributionValue(MetricUpdate metricUpdate) {
return DistributionResult.create(sum, count, min, max);
}

private GaugeResult getGaugeValue(MetricUpdate metricUpdate) {
if (metricUpdate.getGauge() == null) {
return GaugeResult.empty();
}
Object gaugeValue = metricUpdate.getGauge();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been a while since I thought about gauge metrics. I know that @Naireen worked on them some time. I am not sure what the spec here is, since the protobuf is so generic. I am trying to find a design doc for a reference but maybe @Naireen can tell us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Naireen please take a look

if (gaugeValue instanceof Number) {
long value = ((Number) gaugeValue).longValue();
Instant timestamp = new Instant(System.currentTimeMillis());
String tsStr = metricUpdate.getName().getContext().get("timestamp");
if (tsStr != null) {
try {
timestamp =
Instant.parse(tsStr); // Expecting ISO-8601 format, e.g., "2025-03-15T10:00:00Z"
} catch (IllegalArgumentException e) {
LOG.warn("Failed to parse gauge timestamp '{}': {}", tsStr, e.getMessage());
}
}
return GaugeResult.create(value, timestamp);
} else if (gaugeValue instanceof Map) {
Map<?, ?> gaugeData = (Map<?, ?>) gaugeValue;
Object valueObj = gaugeData.get("value");
Object tsObj = gaugeData.get("timestamp");
if (valueObj instanceof Number && tsObj instanceof Number) {
long value = ((Number) valueObj).longValue();
Instant timestamp = new Instant(((Number) tsObj).longValue());
return GaugeResult.create(value, timestamp);
}
}
LOG.warn(
"Gauge value {} is not a number or recognized structure, returning empty result",
gaugeValue);
return GaugeResult.empty();
}

public Iterable<MetricResult<DistributionResult>> getDistributionResults() {
return distributionResults.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.beam.sdk.extensions.gcp.storage.NoopPathValidator;
import org.apache.beam.sdk.metrics.BoundedTrieResult;
import org.apache.beam.sdk.metrics.DistributionResult;
import org.apache.beam.sdk.metrics.GaugeResult;
import org.apache.beam.sdk.metrics.MetricQueryResults;
import org.apache.beam.sdk.metrics.MetricsFilter;
import org.apache.beam.sdk.metrics.StringSetResult;
Expand All @@ -60,6 +61,7 @@
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableList;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableSet;
import org.joda.time.Instant;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -612,4 +614,69 @@ public void testTemplateJobMetricsThrowsUsefulError() throws Exception {
UnsupportedOperationException.class,
() -> metrics.queryMetrics(MetricsFilter.builder().build()));
}

@Test
public void testSingleGaugeUpdates() throws IOException {
AppliedPTransform<?, ?, ?> myStep = mock(AppliedPTransform.class);
when(myStep.getFullName()).thenReturn("myStepName");
BiMap<AppliedPTransform<?, ?, ?>, String> transformStepNames = HashBiMap.create();
transformStepNames.put(myStep, "s1");

JobMetrics jobMetrics = new JobMetrics();
DataflowPipelineJob job = mock(DataflowPipelineJob.class);
DataflowPipelineOptions options = mock(DataflowPipelineOptions.class);
when(options.isStreaming()).thenReturn(false);
when(job.getDataflowOptions()).thenReturn(options);
when(job.getState()).thenReturn(State.RUNNING);
when(job.getJobId()).thenReturn(JOB_ID);
when(job.getTransformStepNames()).thenReturn(transformStepNames);

// Create gauge metric updates with timestamp in context
String timestampStr = "2025-03-15T10:00:00Z";
MetricUpdate committedUpdate = new MetricUpdate();
committedUpdate.setGauge(new BigDecimal(42L));
MetricStructuredName committedName = new MetricStructuredName();
committedName.setName("gaugeName");
committedName.setOrigin("user");
committedName.setContext(
ImmutableMap.of(
"step", "s1",
"namespace", "gaugeNamespace",
"timestamp", timestampStr));
committedUpdate.setName(committedName);

MetricUpdate tentativeUpdate = new MetricUpdate();
tentativeUpdate.setGauge(new BigDecimal(42L));
MetricStructuredName tentativeName = new MetricStructuredName();
tentativeName.setName("gaugeName");
tentativeName.setOrigin("user");
tentativeName.setContext(
ImmutableMap.of(
"step", "s1",
"namespace", "gaugeNamespace",
"timestamp", timestampStr,
"tentative", "true"));
tentativeUpdate.setName(tentativeName);

jobMetrics.setMetrics(ImmutableList.of(committedUpdate, tentativeUpdate));
DataflowClient dataflowClient = mock(DataflowClient.class);
when(dataflowClient.getJobMetrics(JOB_ID)).thenReturn(jobMetrics);

DataflowMetrics dataflowMetrics = new DataflowMetrics(job, dataflowClient);
MetricQueryResults result = dataflowMetrics.allMetrics();

Instant expectedTimestamp = Instant.parse(timestampStr);
GaugeResult expectedGaugeResult = GaugeResult.create(42L, expectedTimestamp);

assertThat(
result.getGauges(),
containsInAnyOrder(
attemptedMetricsResult(
"gaugeNamespace", "gaugeName", "myStepName", expectedGaugeResult)));
assertThat(
result.getGauges(),
containsInAnyOrder(
committedMetricsResult(
"gaugeNamespace", "gaugeName", "myStepName", expectedGaugeResult)));
}
}
Loading