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