diff --git a/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java b/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java index c77923b80de..a7089d03abb 100644 --- a/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java +++ b/exporters/prometheus/src/main/java/io/opentelemetry/exporter/prometheus/Serializer.java @@ -244,6 +244,10 @@ private static boolean isDeltaTemporality(MetricData metricData) { } private static void writeResource(Resource resource, Writer writer) throws IOException { + if (resource.getAttributes().isEmpty()) { + return; + } + writer.write("# TYPE target info\n"); writer.write("# HELP target Target metadata\n"); writer.write("target_info{"); @@ -253,6 +257,10 @@ private static void writeResource(Resource resource, Writer writer) throws IOExc private static void writeScopeInfo( InstrumentationScopeInfo instrumentationScopeInfo, Writer writer) throws IOException { + if (instrumentationScopeInfo.getAttributes().isEmpty()) { + return; + } + writer.write("# TYPE otel_scope_info info\n"); writer.write("# HELP otel_scope_info Scope metadata\n"); writer.write("otel_scope_info{"); diff --git a/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerTest.java b/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerTest.java index cbda79c4183..2c6245fd1d4 100644 --- a/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerTest.java +++ b/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/PrometheusHttpServerTest.java @@ -117,12 +117,6 @@ void fetchPrometheus(String endpoint) { "# TYPE target info\n" + "# HELP target Target metadata\n" + "target_info{kr=\"vr\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"grpc\",otel_scope_version=\"version\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"http\",otel_scope_version=\"version\"} 1\n" + "# TYPE grpc_name_total counter\n" + "# HELP grpc_name_total long_description\n" + "grpc_name_total{otel_scope_name=\"grpc\",otel_scope_version=\"version\",kp=\"vp\"} 5.0 0\n" @@ -152,12 +146,6 @@ void fetchOpenMetrics(String endpoint) { "# TYPE target info\n" + "# HELP target Target metadata\n" + "target_info{kr=\"vr\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"grpc\",otel_scope_version=\"version\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"http\",otel_scope_version=\"version\"} 1\n" + "# TYPE grpc_name counter\n" + "# HELP grpc_name long_description\n" + "grpc_name_total{otel_scope_name=\"grpc\",otel_scope_version=\"version\",kp=\"vp\"} 5.0 0.000\n" @@ -179,9 +167,6 @@ void fetchFiltered() { "# TYPE target info\n" + "# HELP target Target metadata\n" + "target_info{kr=\"vr\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"grpc\",otel_scope_version=\"version\"} 1\n" + "# TYPE grpc_name_total counter\n" + "# HELP grpc_name_total long_description\n" + "grpc_name_total{otel_scope_name=\"grpc\",otel_scope_version=\"version\",kp=\"vp\"} 5.0 0\n"); @@ -206,12 +191,6 @@ void fetchPrometheusCompressed() throws IOException { "# TYPE target info\n" + "# HELP target Target metadata\n" + "target_info{kr=\"vr\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"grpc\",otel_scope_version=\"version\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"http\",otel_scope_version=\"version\"} 1\n" + "# TYPE grpc_name_total counter\n" + "# HELP grpc_name_total long_description\n" + "grpc_name_total{otel_scope_name=\"grpc\",otel_scope_version=\"version\",kp=\"vp\"} 5.0 0\n" @@ -287,12 +266,6 @@ void fetch_DuplicateMetrics() { "# TYPE target info\n" + "# HELP target Target metadata\n" + "target_info{kr=\"vr\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"scope1\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"scope2\"} 1\n" + "# TYPE foo_unit_total counter\n" + "# HELP foo_unit_total description1\n" + "foo_unit_total{otel_scope_name=\"scope1\"} 1.0 0\n" diff --git a/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java b/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java index 210e399ac0f..b76c390eab7 100644 --- a/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java +++ b/exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/SerializerTest.java @@ -241,9 +241,6 @@ void outOfOrderedAttributes() { "# TYPE target info\n" + "# HELP target Target metadata\n" + "target_info{kr=\"vr\"} 1\n" - + "# TYPE otel_scope_info info\n" - + "# HELP otel_scope_info Scope metadata\n" - + "otel_scope_info{otel_scope_name=\"scope\",otel_scope_version=\"1.0.0\"} 1\n" + "# TYPE sum_seconds_total counter\n" + "# HELP sum_seconds_total description\n" + "sum_seconds_total{otel_scope_name=\"scope\",otel_scope_version=\"1.0.0\",b_key=\"val1\",a_key=\"val2\",b_key=\"val3\"} 5.0 1633950672000\n"); @@ -251,6 +248,29 @@ void outOfOrderedAttributes() { "Dropping out-of-order attribute a_key=val2, which occurred after b_key. This can occur when an alternative Attribute implementation is used."); } + @Test + void emptyResource() { + MetricData metricData = + ImmutableMetricData.createDoubleSum( + Resource.empty(), + InstrumentationScopeInfo.builder("scope").setVersion("1.0.0").build(), + "monotonic.cumulative.double.sum", + "description", + "s", + ImmutableSumData.create( + /* isMonotonic= */ true, + AggregationTemporality.CUMULATIVE, + Collections.singletonList( + ImmutableDoublePointData.create( + 1633947011000000000L, 1633950672000000000L, Attributes.empty(), 5)))); + + assertThat(serialize004(metricData)) + .isEqualTo( + "# TYPE monotonic_cumulative_double_sum_seconds_total counter\n" + + "# HELP monotonic_cumulative_double_sum_seconds_total description\n" + + "monotonic_cumulative_double_sum_seconds_total{otel_scope_name=\"scope\",otel_scope_version=\"1.0.0\"} 5.0 1633950672000\n"); + } + private static String serialize004(MetricData... metrics) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {