Skip to content

Commit 174290c

Browse files
authored
Merge pull request #45689 from holly-cummins/better-support-lgtm-container-reuse
Fix incomplete config on re-used LGTM containers
2 parents c2fe311 + 8450e15 commit 174290c

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

extensions/observability-devservices/testcontainers/src/main/java/io/quarkus/observability/testcontainers/LgtmContainer.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,22 @@ public String getOtlpProtocol() {
120120
}
121121

122122
public int getOtlpPort() {
123-
int port = getOtlpPortInternal();
123+
int port = getPrivateOtlpPort();
124124
return getMappedPort(port);
125125
}
126126

127-
private int getOtlpPortInternal() {
127+
private int getPrivateOtlpPort() {
128+
return getPrivateOtlpPort(getOtlpProtocol());
129+
}
130+
131+
public static int getPrivateOtlpPort(String otlpProtocol) {
128132
// use ignore-case here; grpc == gRPC
129-
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equalsIgnoreCase(getOtlpProtocol())) {
133+
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equalsIgnoreCase(otlpProtocol)) {
130134
return ContainerConstants.OTEL_GRPC_EXPORTER_PORT;
131-
} else if (ContainerConstants.OTEL_HTTP_PROTOCOL.equals(getOtlpProtocol())) {
135+
} else if (ContainerConstants.OTEL_HTTP_PROTOCOL.equals(otlpProtocol)) {
132136
return ContainerConstants.OTEL_HTTP_EXPORTER_PORT;
133137
} else {
134-
throw new IllegalArgumentException("Unsupported OTEL protocol: " + getOtlpProtocol());
138+
throw new IllegalArgumentException("Unsupported OTEL protocol: " + otlpProtocol);
135139
}
136140
}
137141

extensions/observability-devservices/testlibs/devresource-common/src/main/java/io/quarkus/observability/devresource/DevResourceLifecycleManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ default Container<T> container(T config, ModulesConfiguration root) {
7575
}
7676

7777
/**
78-
* Deduct current config from params.
79-
* If port are too dynamic / configured, it's hard to deduct,
78+
* Deduce current config from params.
79+
* If port are too dynamic / configured, it's hard to deduce,
8080
* since configuration is not part of the devservice state.
8181
* e.g. different ports then usual - Grafana UI is 3000, if you do not use 3000,
8282
* it's hard or impossible to know which port belongs to certain property.

extensions/observability-devservices/testlibs/devresource-lgtm/src/main/java/io/quarkus/observability/devresource/lgtm/LgtmResource.java

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
public class LgtmResource extends ContainerResource<LgtmContainer, LgtmConfig> {
1616

1717
private ExtensionsCatalog catalog;
18+
private LgtmConfig config;
1819

1920
@Override
2021
public LgtmConfig config(ModulesConfiguration configuration) {
21-
return configuration.lgtm();
22+
LgtmConfig config = configuration.lgtm();
23+
this.config = config;
24+
return config;
2225
}
2326

2427
@Override
@@ -32,17 +35,53 @@ public Container<LgtmConfig> container(LgtmConfig config, ModulesConfiguration r
3235
return set(new LgtmContainer(config));
3336
}
3437

35-
// FIXME consolidate config methods.
38+
private int getPrivateOtlpPort() {
39+
if (config != null) {
40+
return LgtmContainer.getPrivateOtlpPort(config.otlpProtocol());
41+
} else {
42+
return -1;
43+
}
44+
}
45+
46+
private Map<String, String> config(int privatePort, String host) {
47+
return config(privatePort, host, container.getMappedPort(privatePort));
48+
}
49+
3650
@Override
3751
public Map<String, String> config(int privatePort, String host, int publicPort) {
52+
53+
Map<String, String> containerConfigs = new HashMap<>();
54+
3855
switch (privatePort) {
3956
case ContainerConstants.GRAFANA_PORT:
40-
return Map.of("grafana.endpoint", String.format("http://%s:%s", host, publicPort));
41-
case ContainerConstants.OTEL_GRPC_EXPORTER_PORT:
57+
containerConfigs.put("grafana.endpoint", String.format("http://%s:%s", host, publicPort));
58+
break;
4259
case ContainerConstants.OTEL_HTTP_EXPORTER_PORT:
43-
return Map.of("otel-collector.url", String.format("%s:%s", host, publicPort));
60+
if (catalog != null && catalog.hasMicrometerOtlp()) {
61+
62+
containerConfigs.put("quarkus.micrometer.export.otlp.url",
63+
String.format("http://%s:%s/v1/metrics", host,
64+
publicPort));
65+
}
66+
// No break, fall through
67+
case ContainerConstants.OTEL_GRPC_EXPORTER_PORT:
68+
containerConfigs.put("otel-collector.url", String.format("%s:%s", host, publicPort));
69+
break;
70+
}
71+
72+
// The OTLP port is probably one of the ports we already compared against, but at compile-time we don't know which one,
73+
// so instead of doing this check as a fallthrough on the switch, do a normal if-check
74+
if (catalog != null && catalog.hasOpenTelemetry()) {
75+
final int privateOtlpPort = getPrivateOtlpPort();
76+
if (privateOtlpPort == privatePort) {
77+
containerConfigs.put("quarkus.otel.exporter.otlp.endpoint",
78+
String.format("http://%s:%s", host, publicPort));
79+
String otlpProtocol = config.otlpProtocol(); // If we got to this stage, config must be not null
80+
containerConfigs.put("quarkus.otel.exporter.otlp.protocol", otlpProtocol);
81+
}
82+
4483
}
45-
return Map.of();
84+
return containerConfigs;
4685
}
4786

4887
@Override
@@ -53,23 +92,13 @@ protected LgtmContainer defaultContainer() {
5392
@Override
5493
public Map<String, String> doStart() {
5594
String host = container.getHost();
56-
int otlpPort = container.getOtlpPort();
57-
58-
//Set non Quarkus properties for convenience and testing.
5995
Map<String, String> containerConfigs = new HashMap<>();
60-
containerConfigs.put("grafana.endpoint", String.format("http://%s:%s", host, container.getGrafanaPort()));
61-
containerConfigs.put("otel-collector.url", String.format("%s:%s", host, otlpPort));
6296

63-
// set relevant properties for Quarkus extensions directly
64-
if (catalog != null && catalog.hasOpenTelemetry()) {
65-
containerConfigs.put("quarkus.otel.exporter.otlp.endpoint", String.format("http://%s:%s", host, otlpPort));
66-
containerConfigs.put("quarkus.otel.exporter.otlp.protocol", container.getOtlpProtocol());
67-
}
68-
if (catalog != null && catalog.hasMicrometerOtlp()) {
69-
// always use http -- as that's what Micrometer supports
70-
containerConfigs.put("quarkus.micrometer.export.otlp.url",
71-
String.format("http://%s:%s/v1/metrics", host,
72-
container.getMappedPort(ContainerConstants.OTEL_HTTP_EXPORTER_PORT)));
97+
containerConfigs.putAll(config(ContainerConstants.GRAFANA_PORT, host));
98+
containerConfigs.putAll(config(ContainerConstants.OTEL_HTTP_EXPORTER_PORT, host));
99+
// Iff GRPC is the OTLP protocol, overwrite the otel-collector.url we just wrote with the correct grpc one, and set up the otlp endpoints
100+
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equals(container.getOtlpProtocol())) {
101+
containerConfigs.putAll(config(ContainerConstants.OTEL_GRPC_EXPORTER_PORT, host));
73102
}
74103
return containerConfigs;
75104
}

0 commit comments

Comments
 (0)