Skip to content

Commit 5e1dc2b

Browse files
committed
add test
1 parent b8121e5 commit 5e1dc2b

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

exporters/prometheus/src/test/java/io/opentelemetry/exporter/prometheus/Otel2PrometheusConverterTest.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.List;
3939
import java.util.concurrent.atomic.AtomicInteger;
4040
import java.util.function.Predicate;
41-
import java.util.regex.Matcher;
4241
import java.util.regex.Pattern;
4342
import java.util.stream.Collectors;
4443
import java.util.stream.Stream;
@@ -52,7 +51,9 @@ class Otel2PrometheusConverterTest {
5251

5352
private static final Pattern PATTERN =
5453
Pattern.compile(
55-
"# HELP (?<help>.*)\n# TYPE (?<type>.*)\n(?<metricName>.*)\\{otel_scope_name=\"scope\"}(.|\\n)*");
54+
"(.|\\n)*# HELP (?<help>.*)\n# TYPE (?<type>.*)\n(?<metricName>.*)\\{"
55+
+ "otel_scope_foo=\"bar\",otel_scope_name=\"scope\","
56+
+ "otel_scope_schema_url=\"schemaUrl\",otel_scope_version=\"version\"}(.|\\n)*");
5657

5758
private final Otel2PrometheusConverter converter =
5859
new Otel2PrometheusConverter(true, /* allowedResourceAttributesFilter= */ null);
@@ -67,16 +68,17 @@ void metricMetadata(
6768
ExpositionFormats.init().getPrometheusTextFormatWriter().write(out, snapshots);
6869
String expositionFormat = new String(out.toByteArray(), StandardCharsets.UTF_8);
6970

70-
// Uncomment to debug exposition format output
71-
// System.out.println(expositionFormat);
72-
73-
Matcher matcher = PATTERN.matcher(expositionFormat);
74-
assertThat(matcher.matches()).isTrue();
75-
assertThat(matcher.group("help")).isEqualTo(expectedHelp);
76-
assertThat(matcher.group("type")).isEqualTo(expectedType);
77-
// Note: Summaries and histograms produce output which matches METRIC_NAME_PATTERN multiple
78-
// times. The pattern ends up matching against the first.
79-
assertThat(matcher.group("metricName")).isEqualTo(expectedMetricName);
71+
assertThat(expositionFormat)
72+
.matchesSatisfying(
73+
PATTERN,
74+
matcher -> {
75+
assertThat(matcher.group("help")).isEqualTo(expectedHelp);
76+
assertThat(matcher.group("type")).isEqualTo(expectedType);
77+
// Note: Summaries and histograms produce output which matches METRIC_NAME_PATTERN
78+
// multiple
79+
// times. The pattern ends up matching against the first.
80+
assertThat(matcher.group("metricName")).isEqualTo(expectedMetricName);
81+
});
8082
}
8183

8284
@ParameterizedTest
@@ -160,7 +162,9 @@ private static Stream<Arguments> resourceAttributesAdditionArgs() {
160162
: "my_metric_units",
161163

162164
// "cluster" attribute is added (due to reg expr specified) and only it
163-
"cluster=\"mycluster\",foo1=\"bar1\",foo2=\"bar2\",otel_scope_name=\"scope\""));
165+
"cluster=\"mycluster\",foo1=\"bar1\",foo2=\"bar2\",otel_scope_foo=\"bar\","
166+
+ "otel_scope_name=\"scope\",otel_scope_schema_url=\"schemaUrl\","
167+
+ "otel_scope_version=\"version\""));
164168
}
165169

166170
// Resource attributes which also exists in the metric labels are not added twice
@@ -179,7 +183,8 @@ private static Stream<Arguments> resourceAttributesAdditionArgs() {
179183

180184
// "cluster" attribute is present only once and the value is taken
181185
// from the metric attributes and not the resource attributes
182-
"cluster=\"mycluster2\",foo2=\"bar2\",otel_scope_name=\"scope\""));
186+
"cluster=\"mycluster2\",foo2=\"bar2\",otel_scope_foo=\"bar\",otel_scope_name=\"scope\","
187+
+ "otel_scope_schema_url=\"schemaUrl\",otel_scope_version=\"version\""));
183188

184189
// Empty attributes
185190
arguments.add(
@@ -194,7 +199,8 @@ private static Stream<Arguments> resourceAttributesAdditionArgs() {
194199
stringKey("host"), "localhost", stringKey("cluster"), "mycluster"))),
195200
/* allowedResourceAttributesFilter= */ Predicates.startsWith("clu"),
196201
"my_metric_units",
197-
"cluster=\"mycluster\",otel_scope_name=\"scope\""));
202+
"cluster=\"mycluster\",otel_scope_foo=\"bar\",otel_scope_name=\"scope\","
203+
+ "otel_scope_schema_url=\"schemaUrl\",otel_scope_version=\"version\""));
198204

199205
return arguments.stream();
200206
}
@@ -314,11 +320,17 @@ static MetricData createSampleMetricData(
314320
Attributes attributesToUse = attributes == null ? Attributes.empty() : attributes;
315321
Resource resourceToUse = resource == null ? Resource.getDefault() : resource;
316322

323+
InstrumentationScopeInfo scope =
324+
InstrumentationScopeInfo.builder("scope")
325+
.setVersion("version")
326+
.setSchemaUrl("schemaUrl")
327+
.setAttributes(Attributes.of(stringKey("foo"), "bar"))
328+
.build();
317329
switch (metricDataType) {
318330
case SUMMARY:
319331
return ImmutableMetricData.createDoubleSummary(
320332
resourceToUse,
321-
InstrumentationScopeInfo.create("scope"),
333+
scope,
322334
metricName,
323335
"description",
324336
metricUnit,
@@ -329,7 +341,7 @@ static MetricData createSampleMetricData(
329341
case LONG_SUM:
330342
return ImmutableMetricData.createLongSum(
331343
resourceToUse,
332-
InstrumentationScopeInfo.create("scope"),
344+
scope,
333345
metricName,
334346
"description",
335347
metricUnit,
@@ -341,7 +353,7 @@ static MetricData createSampleMetricData(
341353
case DOUBLE_SUM:
342354
return ImmutableMetricData.createDoubleSum(
343355
resourceToUse,
344-
InstrumentationScopeInfo.create("scope"),
356+
scope,
345357
metricName,
346358
"description",
347359
metricUnit,
@@ -353,7 +365,7 @@ static MetricData createSampleMetricData(
353365
case LONG_GAUGE:
354366
return ImmutableMetricData.createLongGauge(
355367
resourceToUse,
356-
InstrumentationScopeInfo.create("scope"),
368+
scope,
357369
metricName,
358370
"description",
359371
metricUnit,
@@ -363,7 +375,7 @@ static MetricData createSampleMetricData(
363375
case DOUBLE_GAUGE:
364376
return ImmutableMetricData.createDoubleGauge(
365377
resourceToUse,
366-
InstrumentationScopeInfo.create("scope"),
378+
scope,
367379
metricName,
368380
"description",
369381
metricUnit,
@@ -373,7 +385,7 @@ static MetricData createSampleMetricData(
373385
case HISTOGRAM:
374386
return ImmutableMetricData.createDoubleHistogram(
375387
resourceToUse,
376-
InstrumentationScopeInfo.create("scope"),
388+
scope,
377389
metricName,
378390
"description",
379391
metricUnit,
@@ -394,7 +406,7 @@ static MetricData createSampleMetricData(
394406
case EXPONENTIAL_HISTOGRAM:
395407
return ImmutableMetricData.createExponentialHistogram(
396408
resourceToUse,
397-
InstrumentationScopeInfo.create("scope"),
409+
scope,
398410
metricName,
399411
"description",
400412
metricUnit,

0 commit comments

Comments
 (0)