Skip to content

Commit

Permalink
Consolidate logic on two config paths to solve problem of missing con…
Browse files Browse the repository at this point in the history
…fig on container re-use path
  • Loading branch information
holly-cummins committed Jan 17, 2025
1 parent 86538a6 commit ebc4d57
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,22 @@ public String getOtlpProtocol() {
}

public int getOtlpPort() {
int port = getOtlpPortInternal();
int port = getPrivateOtlpPort();
return getMappedPort(port);
}

private int getOtlpPortInternal() {
private int getPrivateOtlpPort() {
return getPrivateOtlpPort(getOtlpProtocol());
}

public static int getPrivateOtlpPort(String otlpProtocol) {
// use ignore-case here; grpc == gRPC
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equalsIgnoreCase(getOtlpProtocol())) {
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equalsIgnoreCase(otlpProtocol)) {
return ContainerConstants.OTEL_GRPC_EXPORTER_PORT;
} else if (ContainerConstants.OTEL_HTTP_PROTOCOL.equals(getOtlpProtocol())) {
} else if (ContainerConstants.OTEL_HTTP_PROTOCOL.equals(otlpProtocol)) {
return ContainerConstants.OTEL_HTTP_EXPORTER_PORT;
} else {
throw new IllegalArgumentException("Unsupported OTEL protocol: " + getOtlpProtocol());
throw new IllegalArgumentException("Unsupported OTEL protocol: " + otlpProtocol);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
public class LgtmResource extends ContainerResource<LgtmContainer, LgtmConfig> {

private ExtensionsCatalog catalog;
private LgtmConfig config;

@Override
public LgtmConfig config(ModulesConfiguration configuration) {
return configuration.lgtm();
LgtmConfig config = configuration.lgtm();
this.config = config;
return config;
}

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

// FIXME consolidate config methods.
private int getPrivateOtlpPort() {
if (config != null) {
return LgtmContainer.getPrivateOtlpPort(config.otlpProtocol());
} else {
return -1;
}
}

private Map<String, String> config(int privatePort, String host) {
return config(privatePort, host, container.getMappedPort(privatePort));
}

@Override
public Map<String, String> config(int privatePort, String host, int publicPort) {

Map<String, String> containerConfigs = new HashMap<>();

switch (privatePort) {
case ContainerConstants.GRAFANA_PORT:
return Map.of("grafana.endpoint", String.format("http://%s:%s", host, publicPort));
case ContainerConstants.OTEL_GRPC_EXPORTER_PORT:
containerConfigs.put("grafana.endpoint", String.format("http://%s:%s", host, publicPort));
break;
case ContainerConstants.OTEL_HTTP_EXPORTER_PORT:
return Map.of("otel-collector.url", String.format("%s:%s", host, publicPort));
if (catalog != null && catalog.hasMicrometerOtlp()) {

containerConfigs.put("quarkus.micrometer.export.otlp.url",
String.format("http://%s:%s/v1/metrics", host,
publicPort));
}
// No break, fall through
case ContainerConstants.OTEL_GRPC_EXPORTER_PORT:
containerConfigs.put("otel-collector.url", String.format("%s:%s", host, publicPort));
break;
}

// The OTLP port is probably one of the ports we already compared against, but at compile-time we don't know which one,
// so instead of doing this check as a fallthrough on the switch, do a normal if-check
if (catalog != null && catalog.hasOpenTelemetry()) {
final int privateOtlpPort = getPrivateOtlpPort();
if (privateOtlpPort == privatePort) {
containerConfigs.put("quarkus.otel.exporter.otlp.endpoint",
String.format("http://%s:%s", host, publicPort));
String otlpProtocol = config.otlpProtocol(); // If we got to this stage, config must be not null
containerConfigs.put("quarkus.otel.exporter.otlp.protocol", otlpProtocol);
}

}
return Map.of();
return containerConfigs;
}

@Override
Expand All @@ -53,23 +92,13 @@ protected LgtmContainer defaultContainer() {
@Override
public Map<String, String> doStart() {
String host = container.getHost();
int otlpPort = container.getOtlpPort();

//Set non Quarkus properties for convenience and testing.
Map<String, String> containerConfigs = new HashMap<>();
containerConfigs.put("grafana.endpoint", String.format("http://%s:%s", host, container.getGrafanaPort()));
containerConfigs.put("otel-collector.url", String.format("%s:%s", host, otlpPort));

// set relevant properties for Quarkus extensions directly
if (catalog != null && catalog.hasOpenTelemetry()) {
containerConfigs.put("quarkus.otel.exporter.otlp.endpoint", String.format("http://%s:%s", host, otlpPort));
containerConfigs.put("quarkus.otel.exporter.otlp.protocol", container.getOtlpProtocol());
}
if (catalog != null && catalog.hasMicrometerOtlp()) {
// always use http -- as that's what Micrometer supports
containerConfigs.put("quarkus.micrometer.export.otlp.url",
String.format("http://%s:%s/v1/metrics", host,
container.getMappedPort(ContainerConstants.OTEL_HTTP_EXPORTER_PORT)));
containerConfigs.putAll(config(ContainerConstants.GRAFANA_PORT, host));
containerConfigs.putAll(config(ContainerConstants.OTEL_HTTP_EXPORTER_PORT, host));
// Iff GRPC is the hotel protocol, overwrite the otel-collector.url we just wrote with the correct grpc one, and set up the otlp endpoints
if (ContainerConstants.OTEL_GRPC_PROTOCOL.equals(container.getOtlpProtocol())) {
containerConfigs.putAll(config(ContainerConstants.OTEL_GRPC_EXPORTER_PORT, host));
}
return containerConfigs;
}
Expand Down

0 comments on commit ebc4d57

Please sign in to comment.