55
66package io .opentelemetry .exporter .prometheus ;
77
8+ import static io .opentelemetry .api .common .AttributeKey .booleanArrayKey ;
9+ import static io .opentelemetry .api .common .AttributeKey .booleanKey ;
10+ import static io .opentelemetry .api .common .AttributeKey .doubleArrayKey ;
11+ import static io .opentelemetry .api .common .AttributeKey .doubleKey ;
12+ import static io .opentelemetry .api .common .AttributeKey .longArrayKey ;
13+ import static io .opentelemetry .api .common .AttributeKey .longKey ;
14+ import static io .opentelemetry .api .common .AttributeKey .stringArrayKey ;
815import static io .opentelemetry .api .common .AttributeKey .stringKey ;
916import static org .assertj .core .api .Assertions .assertThat ;
1017import static org .assertj .core .api .Assertions .assertThatCode ;
1118
1219import com .fasterxml .jackson .core .JsonProcessingException ;
1320import com .fasterxml .jackson .databind .ObjectMapper ;
14- import io .opentelemetry .api .common .AttributeKey ;
1521import io .opentelemetry .api .common .AttributeType ;
1622import io .opentelemetry .api .common .Attributes ;
1723import io .opentelemetry .sdk .common .InstrumentationScopeInfo ;
3238import io .opentelemetry .sdk .metrics .internal .data .ImmutableSummaryPointData ;
3339import io .opentelemetry .sdk .resources .Resource ;
3440import io .prometheus .metrics .expositionformats .ExpositionFormats ;
41+ import io .prometheus .metrics .model .snapshots .Labels ;
3542import io .prometheus .metrics .model .snapshots .MetricSnapshots ;
3643import java .io .ByteArrayOutputStream ;
3744import java .io .IOException ;
@@ -57,6 +64,7 @@ class Otel2PrometheusConverterTest {
5764 private static final Pattern PATTERN =
5865 Pattern .compile (
5966 "# HELP (?<help>.*)\n # TYPE (?<type>.*)\n (?<metricName>.*)\\ {otel_scope_name=\" scope\" }(.|\\ n)*" );
67+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
6068
6169 private final Otel2PrometheusConverter converter =
6270 new Otel2PrometheusConverter (true , /* allowedResourceAttributesFilter= */ null );
@@ -141,66 +149,28 @@ void prometheusNameCollisionTest_Issue6277() {
141149 assertThatCode (() -> converter .convert (metricData )).doesNotThrowAnyException ();
142150 }
143151
144- @ Test
145- void labelValueSerialization_Primitives () {
146- Attributes attributes =
147- Attributes .builder ()
148- .put (AttributeKey .stringKey ("stringKey" ), "stringValue" )
149- .put (AttributeKey .booleanKey ("booleanKey" ), true )
150- .put (AttributeKey .longKey ("longKey" ), Long .MAX_VALUE )
151- .put (AttributeKey .doubleKey ("doubleKey" ), 0.12345 )
152- .build ();
153- MetricData metricData =
154- createSampleMetricData ("sample" , "1" , MetricDataType .LONG_SUM , attributes , null );
155-
156- MetricSnapshots snapshots = converter .convert (Collections .singletonList (metricData ));
157-
158- assertThat (snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("stringKey" ))
159- .isEqualTo ("stringValue" );
160- assertThat (snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("booleanKey" ))
161- .isEqualTo ("true" );
162- assertThat (snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("longKey" ))
163- .isEqualTo ("9223372036854775807" );
164- assertThat (snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("doubleKey" ))
165- .isEqualTo ("0.12345" );
166- }
167-
168- @ Test
169- void labelValueSerialization_NonPrimitives () throws JsonProcessingException {
170- List <String > stringArrayValue =
171- Arrays .asList ("stringValue1" , "\" +\\ \\ \\ +\b +\f +\n +\r +\t +" + (char ) 0 );
172- List <Boolean > booleanArrayValue = Arrays .asList (true , false );
173- List <Long > longArrayValue = Arrays .asList (Long .MIN_VALUE , Long .MAX_VALUE );
174- List <Double > doubleArrayValue = Arrays .asList (Double .MIN_VALUE , Double .MAX_VALUE );
175- Attributes attributes =
176- Attributes .builder ()
177- .put (AttributeKey .stringArrayKey ("stringKey" ), stringArrayValue )
178- .put (AttributeKey .booleanArrayKey ("booleanKey" ), booleanArrayValue )
179- .put (AttributeKey .longArrayKey ("longKey" ), longArrayValue )
180- .put (AttributeKey .doubleArrayKey ("doubleKey" ), doubleArrayValue )
181- .build ();
152+ @ ParameterizedTest
153+ @ MethodSource ("labelValueSerializationArgs" )
154+ void labelValueSerialization (Attributes attributes ) {
182155 MetricData metricData =
183156 createSampleMetricData ("sample" , "1" , MetricDataType .LONG_SUM , attributes , null );
184157
185158 MetricSnapshots snapshots = converter .convert (Collections .singletonList (metricData ));
186159
187- ObjectMapper objectMapper = new ObjectMapper ();
188- assertThat (
189- objectMapper .readTree (
190- snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("stringKey" )))
191- .isEqualTo (objectMapper .valueToTree (stringArrayValue ));
192- assertThat (
193- objectMapper .readTree (
194- snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("booleanKey" )))
195- .isEqualTo (objectMapper .valueToTree (booleanArrayValue ));
196- assertThat (
197- objectMapper .readTree (
198- snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("longKey" )))
199- .isEqualTo (objectMapper .valueToTree (longArrayValue ));
200- assertThat (
201- objectMapper .readTree (
202- snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ().get ("doubleKey" )))
203- .isEqualTo (objectMapper .valueToTree (doubleArrayValue ));
160+ Labels labels = snapshots .get (0 ).getDataPoints ().get (0 ).getLabels ();
161+ attributes .forEach (
162+ (key , value ) -> {
163+ String labelValue = labels .get (key .getKey ());
164+ try {
165+ String expectedValue =
166+ key .getType () == AttributeType .STRING
167+ ? (String ) value
168+ : OBJECT_MAPPER .writeValueAsString (value );
169+ assertThat (labelValue ).isEqualTo (expectedValue );
170+ } catch (JsonProcessingException e ) {
171+ throw new RuntimeException (e );
172+ }
173+ });
204174 }
205175
206176 @ Test
@@ -375,6 +345,24 @@ private static Stream<Arguments> metricMetadataArgs() {
375345 "_metric_name_bytes_count" ));
376346 }
377347
348+ private static Stream <Arguments > labelValueSerializationArgs () {
349+ return Stream .of (
350+ Arguments .of (Attributes .of (stringKey ("key" ), "stringValue" )),
351+ Arguments .of (Attributes .of (booleanKey ("key" ), true )),
352+ Arguments .of (Attributes .of (longKey ("key" ), Long .MAX_VALUE )),
353+ Arguments .of (Attributes .of (doubleKey ("key" ), 0.12345 )),
354+ Arguments .of (
355+ Attributes .of (
356+ stringArrayKey ("key" ),
357+ Arrays .asList ("stringValue1" , "\" +\\ \\ \\ +\b +\f +\n +\r +\t +" + (char ) 0 ))),
358+ Arguments .of (Attributes .of (booleanArrayKey ("key" ), Arrays .asList (true , false ))),
359+ Arguments .of (
360+ Attributes .of (longArrayKey ("key" ), Arrays .asList (Long .MIN_VALUE , Long .MAX_VALUE ))),
361+ Arguments .of (
362+ Attributes .of (
363+ doubleArrayKey ("key" ), Arrays .asList (Double .MIN_VALUE , Double .MAX_VALUE ))));
364+ }
365+
378366 static MetricData createSampleMetricData (
379367 String metricName , Resource resource , Attributes attributes ) {
380368 return createSampleMetricData (
0 commit comments