Skip to content

Commit 77d72d2

Browse files
Merge branch '1.12.x' into 1.13.x
2 parents 12db19f + d7daaef commit 77d72d2

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

implementations/micrometer-registry-otlp/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies {
1010
testImplementation 'io.rest-assured:rest-assured'
1111
testImplementation 'org.testcontainers:junit-jupiter'
1212
testImplementation 'org.awaitility:awaitility'
13+
testImplementation libs.mockitoCore5
1314
}
1415

1516
dockerTest {

implementations/micrometer-registry-otlp/src/main/java/io/micrometer/registry/otlp/OtlpMeterRegistry.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public class OtlpMeterRegistry extends PushMeterRegistry {
8989

9090
private final TimeUnit baseTimeUnit;
9191

92+
private final String userAgentHeader;
93+
9294
// Time when the last scheduled rollOver has started. Applicable only for delta
9395
// flavour.
9496
private volatile long lastMeterRolloverStartTime = -1;
@@ -104,15 +106,17 @@ public OtlpMeterRegistry(OtlpConfig config, Clock clock) {
104106
this(config, clock, new HttpUrlConnectionSender());
105107
}
106108

109+
// VisibleForTesting
107110
// not public until we decide what we want to expose in public API
108111
// HttpSender may not be a good idea if we will support a non-HTTP transport
109-
private OtlpMeterRegistry(OtlpConfig config, Clock clock, HttpSender httpSender) {
112+
OtlpMeterRegistry(OtlpConfig config, Clock clock, HttpSender httpSender) {
110113
super(config, clock);
111114
this.config = config;
112115
this.baseTimeUnit = config.baseTimeUnit();
113116
this.httpSender = httpSender;
114117
this.resource = Resource.newBuilder().addAllAttributes(getResourceAttributes()).build();
115118
this.aggregationTemporality = config.aggregationTemporality();
119+
this.userAgentHeader = getUserAgentHeader();
116120
config().namingConvention(NamingConvention.dot);
117121
start(DEFAULT_THREAD_FACTORY);
118122
}
@@ -162,6 +166,7 @@ protected void publish() {
162166
.build())
163167
.build();
164168
HttpSender.Request.Builder httpRequest = this.httpSender.post(this.config.url())
169+
.withHeader("User-Agent", this.userAgentHeader)
165170
.withContent("application/x-protobuf", request.toByteArray());
166171
this.config.headers().forEach(httpRequest::withHeader);
167172
HttpSender.Response response = httpRequest.send();
@@ -432,4 +437,11 @@ static double[] getSloWithPositiveInf(DistributionStatisticConfig distributionSt
432437
return sloWithPositiveInf;
433438
}
434439

440+
private String getUserAgentHeader() {
441+
if (this.getClass().getPackage().getImplementationVersion() == null) {
442+
return "Micrometer-OTLP-Exporter-Java";
443+
}
444+
return "Micrometer-OTLP-Exporter-Java/" + this.getClass().getPackage().getImplementationVersion();
445+
}
446+
435447
}

implementations/micrometer-registry-otlp/src/test/java/io/micrometer/registry/otlp/OtlpMeterRegistryTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package io.micrometer.registry.otlp;
1717

18+
import io.micrometer.core.Issue;
1819
import io.micrometer.core.instrument.*;
1920
import io.micrometer.core.instrument.Timer;
2021
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
22+
import io.micrometer.core.ipc.http.HttpSender;
2123
import io.opentelemetry.proto.metrics.v1.HistogramDataPoint;
2224
import io.opentelemetry.proto.metrics.v1.Metric;
2325
import io.opentelemetry.proto.metrics.v1.NumberDataPoint;
26+
import org.junit.jupiter.api.BeforeEach;
2427
import org.junit.jupiter.api.Test;
2528

2629
import java.io.IOException;
@@ -29,6 +32,8 @@
2932
import java.util.concurrent.TimeUnit;
3033

3134
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.mockito.ArgumentMatchers.assertArg;
36+
import static org.mockito.Mockito.*;
3237
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariables;
3338

3439
/**
@@ -45,12 +50,21 @@ abstract class OtlpMeterRegistryTest {
4550

4651
protected static final Tag meterTag = Tag.of("key", "value");
4752

48-
protected MockClock clock = new MockClock();
53+
protected MockClock clock;
4954

50-
protected OtlpMeterRegistry registry = new OtlpMeterRegistry(otlpConfig(), clock);
55+
protected OtlpMeterRegistry registry;
56+
57+
private HttpSender mockHttpSender;
5158

5259
abstract OtlpConfig otlpConfig();
5360

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+
5468
// If the service.name was not specified, SDKs MUST fallback to 'unknown_service'
5569
@Test
5670
void unknownServiceByDefault() {
@@ -123,6 +137,23 @@ void timeGauge() {
123137
+ " time_unix_nano: 1000000\n" + " as_double: 0.024\n" + " }\n" + "}\n");
124138
}
125139

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+
126157
@Test
127158
void distributionWithPercentileShouldWriteSummary() {
128159
Timer timer = Timer.builder("timer")

0 commit comments

Comments
 (0)