Skip to content

Commit

Permalink
Prometheus exporter: omit empty otel_scope_info and otel_target_info …
Browse files Browse the repository at this point in the history
…metrics (#5887)
  • Loading branch information
laurit committed Oct 11, 2023
1 parent cc8774f commit 3c52cdf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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{");
Expand All @@ -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{");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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");
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,36 @@ 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");
logCapturer.assertContains(
"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 {
Expand Down

0 comments on commit 3c52cdf

Please sign in to comment.