Skip to content
Merged
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 @@ -27,15 +27,23 @@ public MetricDataFactory(
this.currentTimeMillis = currentTimeMillis;
}

@Nullable
public MetricData create(CounterSnapshot snapshot) {
if (snapshot.getDataPoints().isEmpty()) {
return null;
}
return new PrometheusMetricData<>(
snapshot.getMetadata(),
new PrometheusCounter(snapshot, currentTimeMillis),
instrumentationScopeInfo,
resource);
}

@Nullable
public MetricData create(GaugeSnapshot snapshot) {
if (snapshot.getDataPoints().isEmpty()) {
return null;
}
return new PrometheusMetricData<>(
snapshot.getMetadata(),
new PrometheusGauge(snapshot, currentTimeMillis),
Expand Down Expand Up @@ -64,31 +72,47 @@ public MetricData create(HistogramSnapshot snapshot) {
return null;
}

@Nullable
public MetricData create(SummarySnapshot snapshot) {
if (snapshot.getDataPoints().isEmpty()) {
return null;
}
return new PrometheusMetricData<>(
snapshot.getMetadata(),
new PrometheusSummary(snapshot, currentTimeMillis),
instrumentationScopeInfo,
resource);
}

@Nullable
public MetricData create(InfoSnapshot snapshot) {
if (snapshot.getDataPoints().isEmpty()) {
return null;
}
return new PrometheusMetricData<>(
snapshot.getMetadata(),
new PrometheusInfo(snapshot, currentTimeMillis),
instrumentationScopeInfo,
resource);
}

@Nullable
public MetricData create(StateSetSnapshot snapshot) {
if (snapshot.getDataPoints().isEmpty()) {
return null;
}
return new PrometheusMetricData<>(
snapshot.getMetadata(),
new PrometheusStateSet(snapshot, currentTimeMillis),
instrumentationScopeInfo,
resource);
}

@Nullable
public MetricData create(UnknownSnapshot snapshot) {
if (snapshot.getDataPoints().isEmpty()) {
return null;
}
return new PrometheusMetricData<>(
snapshot.getMetadata(),
new PrometheusUnknown(snapshot, currentTimeMillis),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ExportTest {

private static final Attributes ATTRIBUTES =
Attributes.of(AttributeKey.stringKey("label"), "val", AttributeKey.stringKey("key"), "value");
@RegisterExtension OpenTelemetryExtension testing = OpenTelemetryExtension.create();
@RegisterExtension static OpenTelemetryExtension testing = OpenTelemetryExtension.create();

private final PrometheusRegistry registry = new PrometheusRegistry();

Expand Down Expand Up @@ -304,6 +304,26 @@ void unknown() {
.hasAttributes(Attributes.of(AttributeKey.stringKey("label"), "val"))));
}

@Test
void metricsWithoutDataPointsAreNotExported() {
// Register metrics with labels but don't create any data points
// This simulates the jvm_memory_pool_allocated_bytes scenario where a metric
// is registered with label names, but no data points are created until GC happens
Counter.builder().name("counter_no_data").labelNames("pool").register(registry);
Gauge.builder().name("gauge_no_data").labelNames("pool").register(registry);
Summary.builder().name("summary_no_data").labelNames("pool").register(registry);
Histogram.builder().name("histogram_no_data").labelNames("pool").register(registry);
StateSet.builder()
.name("stateset_no_data")
.states("state")
.labelNames("pool")
.register(registry);
Info.builder().name("info_no_data").labelNames("pool").register(registry);

List<MetricData> metrics = testing.getMetrics();
assertThat(metrics).isEmpty();
}

private MetricAssert metricAssert() {
List<MetricData> metrics = testing.getMetrics();
assertThat(metrics).hasSize(1);
Expand Down