Skip to content

Commit 8450e15

Browse files
committed
Consolidate logic on two config paths to solve problem of missing config on container re-use path
1 parent 86538a6 commit 8450e15

File tree

2 files changed

+59
-26
lines changed
  • extensions/observability-devservices
    • testcontainers/src/main/java/io/quarkus/observability/testcontainers
    • testlibs/devresource-lgtm/src/main/java/io/quarkus/observability/devresource/lgtm

2 files changed

+59
-26
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
@@ -100,18 +100,22 @@ public String getOtlpProtocol() {
100100
}
101101

102102
public int getOtlpPort() {
103-
int port = getOtlpPortInternal();
103+
int port = getPrivateOtlpPort();
104104
return getMappedPort(port);
105105
}
106106

107-
private int getOtlpPortInternal() {
107+
private int getPrivateOtlpPort() {
108+
return getPrivateOtlpPort(getOtlpProtocol());
109+
}
110+
111+
public static int getPrivateOtlpPort(String otlpProtocol) {
108112
// use ignore-case here; grpc == gRPC
109-
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equalsIgnoreCase(getOtlpProtocol())) {
113+
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equalsIgnoreCase(otlpProtocol)) {
110114
return ContainerConstants.OTEL_GRPC_EXPORTER_PORT;
111-
} else if (ContainerConstants.OTEL_HTTP_PROTOCOL.equals(getOtlpProtocol())) {
115+
} else if (ContainerConstants.OTEL_HTTP_PROTOCOL.equals(otlpProtocol)) {
112116
return ContainerConstants.OTEL_HTTP_EXPORTER_PORT;
113117
} else {
114-
throw new IllegalArgumentException("Unsupported OTEL protocol: " + getOtlpProtocol());
118+
throw new IllegalArgumentException("Unsupported OTEL protocol: " + otlpProtocol);
115119
}
116120
}
117121

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)