Skip to content

Commit

Permalink
try to bridge StructuredConfigProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Feb 21, 2025
1 parent 6dccc64 commit 10e52d2
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@
@ConfigurationProperties(prefix = "otel")
public final class OtelSpringProperties {

public static final class OtlpHeader {
private String key;
private String value;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
Expand Down Expand Up @@ -315,6 +336,8 @@ public void setHttp(Http http) {

private List<String> propagators = Collections.emptyList();

private List<OtlpHeader> otlpHeaders = Collections.emptyList();

private Java java = new Java();

private Experimental experimental = new Experimental();
Expand Down Expand Up @@ -343,6 +366,14 @@ public void setJava(Java java) {
this.java = java;
}

public List<OtlpHeader> getOtlpHeaders() {
return otlpHeaders;
}

public void setOtlpHeaders(List<OtlpHeader> otlpHeaders) {
this.otlpHeaders = otlpHeaders;
}

public Experimental getExperimental() {
return experimental;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public class SpringConfigYamlProperties implements StructuredConfigProperties {
private final ConfigurableEnvironment environment;
private final String prefix;
private final StructuredConfigProperties fallback;
private final OtelSpringProperties otelSdkProperties;

public SpringConfigYamlProperties(
ConfigurableEnvironment environment, String prefix, StructuredConfigProperties fallback,
OtelSpringProperties otelSdkProperties) {
this.environment = environment;
this.prefix = prefix;
this.fallback = fallback;
this.otelSdkProperties = otelSdkProperties;
}

@Nullable
@Override
public String getString(String name) {
return or(environment.getProperty(fullKey(name), String.class), fallback.getString(name));
}

@Nullable
@Override
public Boolean getBoolean(String name) {
return or(environment.getProperty(fullKey(name), Boolean.class), fallback.getBoolean(name));
}

@Nullable
@Override
public Integer getInt(String name) {
return or(environment.getProperty(fullKey(name), Integer.class), fallback.getInt(name));
}

@Nullable
@Override
public Long getLong(String name) {
return or(environment.getProperty(fullKey(name), Long.class), fallback.getLong(name));
}

@Nullable
@Override
public Double getDouble(String name) {
return or(environment.getProperty(fullKey(name), Double.class), fallback.getDouble(name));
}

private String fullKey(String name) {
return prefix + "." + name;
}

@Override
public Set<String> getPropertyKeys() {
return environment.getPropertySources().stream()
.flatMap(
source ->
source instanceof EnumerablePropertySource<?>
? Arrays.stream(((EnumerablePropertySource<?>) source).getPropertyNames())
: Stream.empty())
.filter(name -> name.startsWith(prefix + "."))
.collect(Collectors.toSet());
}

@Nullable
@Override
public List<StructuredConfigProperties> getStructuredList(String name) {
if ("otlp.headers".equals(fullKey(name))) {
// this means, we mirror the entire model tree
// therefore, it seems to be better te tweak [email protected]:joelittlejohn/jsonschema2pojo.git
// to add the required @ConfigurationProperties(prefix = "otel") annotations
// (add an org.jsonschema2pojo.Annotator) - and maybe a bit more
List<OtelSpringProperties.OtlpHeader> otlpHeaders = otelSdkProperties.getOtlpHeaders();
}

return null;
// return or(environment.getProperty(name, List.class), otelSdkProperties.getList(name));
}

@Nullable
private static <T> T or(@Nullable T first, @Nullable T second) {
return first != null ? first : second;
}
}

0 comments on commit 10e52d2

Please sign in to comment.