15
15
*/
16
16
package io .micrometer .registry .otlp ;
17
17
18
- import io .micrometer .core .instrument .* ;
18
+ import io .micrometer .core .Issue ;
19
19
import io .micrometer .core .instrument .Timer ;
20
+ import io .micrometer .core .instrument .*;
20
21
import io .micrometer .core .instrument .distribution .DistributionStatisticConfig ;
21
- import io .micrometer .core .instrument .Timer ;
22
+ import io .micrometer .core .instrument .util .NamedThreadFactory ;
23
+ import io .micrometer .core .ipc .http .HttpSender ;
22
24
import io .opentelemetry .proto .metrics .v1 .ExponentialHistogramDataPoint ;
23
25
import io .opentelemetry .proto .metrics .v1 .HistogramDataPoint ;
24
26
import io .opentelemetry .proto .metrics .v1 .Metric ;
25
27
import io .opentelemetry .proto .metrics .v1 .NumberDataPoint ;
28
+ import org .junit .jupiter .api .BeforeEach ;
26
29
import org .junit .jupiter .api .Test ;
27
30
28
31
import java .io .IOException ;
31
34
import java .util .concurrent .TimeUnit ;
32
35
33
36
import static org .assertj .core .api .Assertions .assertThat ;
37
+ import static org .mockito .ArgumentMatchers .assertArg ;
38
+ import static org .mockito .Mockito .*;
34
39
import static uk .org .webcompere .systemstubs .SystemStubs .withEnvironmentVariables ;
35
40
36
41
/**
@@ -47,16 +52,27 @@ abstract class OtlpMeterRegistryTest {
47
52
48
53
protected static final Tag meterTag = Tag .of ("key" , "value" );
49
54
50
- protected MockClock clock = new MockClock () ;
55
+ protected MockClock clock ;
51
56
52
- OtlpMeterRegistry registry = new OtlpMeterRegistry ( otlpConfig (), clock ) ;
57
+ private HttpSender mockHttpSender ;
53
58
54
- OtlpMeterRegistry registryWithExponentialHistogram = new OtlpMeterRegistry (exponentialHistogramOtlpConfig (), clock );
59
+ OtlpMeterRegistry registry ;
60
+
61
+ OtlpMeterRegistry registryWithExponentialHistogram ;
55
62
56
63
abstract OtlpConfig otlpConfig ();
57
64
58
65
abstract OtlpConfig exponentialHistogramOtlpConfig ();
59
66
67
+ @ BeforeEach
68
+ void setUp () {
69
+ this .clock = new MockClock ();
70
+ this .mockHttpSender = mock (HttpSender .class );
71
+ this .registry = new OtlpMeterRegistry (otlpConfig (), this .clock ,
72
+ new NamedThreadFactory ("otlp-metrics-publisher" ), this .mockHttpSender );
73
+ this .registryWithExponentialHistogram = new OtlpMeterRegistry (exponentialHistogramOtlpConfig (), clock );
74
+ }
75
+
60
76
// If the service.name was not specified, SDKs MUST fallback to 'unknown_service'
61
77
@ Test
62
78
void unknownServiceByDefault () {
@@ -129,6 +145,23 @@ void timeGauge() {
129
145
+ " time_unix_nano: 1000000\n " + " as_double: 0.024\n " + " }\n " + "}\n " );
130
146
}
131
147
148
+ @ Issue ("#5577" )
149
+ @ Test
150
+ void httpHeaders () throws Throwable {
151
+ HttpSender .Request .Builder builder = HttpSender .Request .build (otlpConfig ().url (), this .mockHttpSender );
152
+ when (mockHttpSender .post (otlpConfig ().url ())).thenReturn (builder );
153
+
154
+ when (mockHttpSender .send (isA (HttpSender .Request .class ))).thenReturn (new HttpSender .Response (200 , "" ));
155
+
156
+ writeToMetric (TimeGauge .builder ("gauge.time" , this , TimeUnit .MICROSECONDS , o -> 24 ).register (registry ));
157
+ registry .publish ();
158
+
159
+ verify (this .mockHttpSender ).send (assertArg (request -> {
160
+ assertThat (request .getRequestHeaders ().get ("User-Agent" )).startsWith ("Micrometer-OTLP-Exporter-Java" );
161
+ assertThat (request .getRequestHeaders ()).containsEntry ("Content-Type" , "application/x-protobuf" );
162
+ }));
163
+ }
164
+
132
165
@ Test
133
166
void distributionWithPercentileShouldWriteSummary () {
134
167
Timer .Builder timer = Timer .builder ("timer" )
0 commit comments