Skip to content

Commit 8e2629a

Browse files
Refactor Configuration Properties (#318)
Refactor Configuration Properties This commit centralizes all configuration properties supported by the extension. It also introduces new property names to align with other agent extensions used in SAP BTP. Signed-off-by: Karsten Schnitter <k.schnitter@sap.com>
1 parent 22ab5c7 commit 8e2629a

17 files changed

+878
-125
lines changed

cf-java-logging-support-opentelemetry-agent-extension/README.md

Lines changed: 69 additions & 34 deletions
Large diffs are not rendered by default.

cf-java-logging-support-opentelemetry-agent-extension/src/main/java/com/sap/hcf/cf/logging/opentelemetry/agent/ext/attributes/CloudFoundryResourceCustomizer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.attributes;
22

3+
import com.sap.hcf.cf.logging.opentelemetry.agent.ext.config.ExtensionConfigurations.RESOURCE;
34
import io.opentelemetry.api.common.AttributeKey;
45
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
56
import io.opentelemetry.sdk.resources.Resource;
@@ -13,10 +14,6 @@
1314

1415
public class CloudFoundryResourceCustomizer implements BiFunction<Resource, ConfigProperties, Resource> {
1516

16-
private static final String OTEL_JAVAAGENT_EXTENSION_SAP_CF_RESOURCE_ENABLED =
17-
"otel.javaagent.extension.sap.cf.resource.enabled";
18-
private static final String OTEL_JAVAAGENT_EXTENTION_CF_RESOURCE_FORMAT =
19-
"otel.javaagent.extension.sap.cf.resource.format";
2017
private static final Logger LOG = Logger.getLogger(CloudFoundryResourceCustomizer.class.getName());
2118
private static final Map<String, String> SAP_CF_RESOURCE_ATTRIBUTES = new HashMap<String, String>() {{
2219
put("cloudfoundry.app.id", "sap.cf.app_id");
@@ -32,7 +29,7 @@ public class CloudFoundryResourceCustomizer implements BiFunction<Resource, Conf
3229

3330
@Override
3431
public Resource apply(Resource resource, ConfigProperties configProperties) {
35-
boolean isEnabled = configProperties.getBoolean(OTEL_JAVAAGENT_EXTENSION_SAP_CF_RESOURCE_ENABLED, true);
32+
boolean isEnabled = RESOURCE.CLOUD_FOUNDRY.ENABLED.getValue(configProperties);
3633
if (!isEnabled) {
3734
LOG.config("CF resource attributes are disabled by configuration.");
3835
return Resource.empty();
@@ -48,7 +45,7 @@ public Resource apply(Resource resource, ConfigProperties configProperties) {
4845
return resource;
4946
}
5047

51-
String format = configProperties.getString(OTEL_JAVAAGENT_EXTENTION_CF_RESOURCE_FORMAT, "SAP");
48+
String format = RESOURCE.CLOUD_FOUNDRY.FORMAT.getValue(configProperties);
5249
if (!format.equalsIgnoreCase("SAP")) {
5350
return resource;
5451
}

cf-java-logging-support-opentelemetry-agent-extension/src/main/java/com/sap/hcf/cf/logging/opentelemetry/agent/ext/binding/CloudLoggingBindingPropertiesSupplier.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;
22

3+
import com.sap.hcf.cf.logging.opentelemetry.agent.ext.config.ExtensionConfigurations.DEPRECATED;
34
import io.opentelemetry.common.ComponentLoader;
45
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
56
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
@@ -34,8 +35,8 @@ public CloudLoggingBindingPropertiesSupplier() {
3435

3536
private static ConfigProperties getDefaultProperties() {
3637
Map<String, String> defaults = new HashMap<>();
37-
defaults.put("com.sap.otel.extension.cloud-logging.label", "cloud-logging");
38-
defaults.put("com.sap.otel.extension.cloud-logging.tag", "Cloud Logging");
38+
defaults.put(DEPRECATED.RUNTIME.CLOUD_FOUNDRY.SERVICE.CLOUD_LOGGING.LABEL_SAP.getKey(), "cloud-logging");
39+
defaults.put(DEPRECATED.RUNTIME.CLOUD_FOUNDRY.SERVICE.CLOUD_LOGGING.TAG_SAP.getKey(), "Cloud Logging");
3940
defaults.put("otel.javaagent.extension.sap.cf.binding.user-provided.label", "user-provided");
4041
ComponentLoader componentLoader =
4142
ComponentLoader.forClassLoader(DefaultConfigProperties.class.getClassLoader());

cf-java-logging-support-opentelemetry-agent-extension/src/main/java/com/sap/hcf/cf/logging/opentelemetry/agent/ext/binding/CloudLoggingServicesProvider.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;
22

3+
import com.sap.hcf.cf.logging.opentelemetry.agent.ext.config.ExtensionConfigurations.DEPRECATED;
4+
import com.sap.hcf.cf.logging.opentelemetry.agent.ext.config.ExtensionConfigurations.RUNTIME;
35
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
46

57
import java.util.List;
@@ -10,10 +12,6 @@
1012

1113
public class CloudLoggingServicesProvider implements Supplier<Stream<CloudFoundryServiceInstance>> {
1214

13-
private static final String DEFAULT_USER_PROVIDED_LABEL = "user-provided";
14-
private static final String DEFAULT_CLOUD_LOGGING_LABEL = "cloud-logging";
15-
private static final String DEFAULT_CLOUD_LOGGING_TAG = "Cloud Logging";
16-
1715
private final List<CloudFoundryServiceInstance> services;
1816

1917
public CloudLoggingServicesProvider(ConfigProperties config) {
@@ -27,20 +25,15 @@ public CloudLoggingServicesProvider(ConfigProperties config) {
2725
}
2826

2927
private String getUserProvidedLabel(ConfigProperties config) {
30-
return config.getString("otel.javaagent.extension.sap.cf.binding.user-provided.label",
31-
DEFAULT_USER_PROVIDED_LABEL);
28+
return DEPRECATED.RUNTIME.CLOUD_FOUNDRY.SERVICE.USER_PROVIDED.LABEL_OTEL.getValue(config);
3229
}
3330

3431
private String getCloudLoggingLabel(ConfigProperties config) {
35-
String fromOwnProperties =
36-
System.getProperty("com.sap.otel.extension.cloud-logging.label", DEFAULT_CLOUD_LOGGING_LABEL);
37-
return config.getString("otel.javaagent.extension.sap.cf.binding.cloud-logging.label", fromOwnProperties);
32+
return RUNTIME.CLOUD_FOUNDRY.SERVICE.CLOUD_LOGGING.LABEL.getValue(config);
3833
}
3934

4035
private String getCloudLoggingTag(ConfigProperties config) {
41-
String fromOwnProperties =
42-
System.getProperty("com.sap.otel.extension.cloud-logging.tag", DEFAULT_CLOUD_LOGGING_TAG);
43-
return config.getString("otel.javaagent.extension.sap.cf.binding.cloud-logging.tag", fromOwnProperties);
36+
return RUNTIME.CLOUD_FOUNDRY.SERVICE.CLOUD_LOGGING.TAG.getValue(config);
4437
}
4538

4639
@Override

cf-java-logging-support-opentelemetry-agent-extension/src/main/java/com/sap/hcf/cf/logging/opentelemetry/agent/ext/binding/DynatraceServiceProvider.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;
22

3+
import com.sap.hcf.cf.logging.opentelemetry.agent.ext.config.ExtensionConfigurations.DEPRECATED;
4+
import com.sap.hcf.cf.logging.opentelemetry.agent.ext.config.ExtensionConfigurations.RUNTIME;
35
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
46

57
import java.util.List;
68
import java.util.function.Supplier;
79

810
public class DynatraceServiceProvider implements Supplier<CloudFoundryServiceInstance> {
911

10-
private static final String DEFAULT_USER_PROVIDED_LABEL = "user-provided";
11-
private static final String DEFAULT_DYNATRACE_LABEL = "dynatrace";
12-
private static final String DEFAULT_DYNATRACE_TAG = "dynatrace";
13-
1412
private final CloudFoundryServiceInstance service;
1513

1614
public DynatraceServiceProvider(ConfigProperties config) {
@@ -24,16 +22,15 @@ public DynatraceServiceProvider(ConfigProperties config) {
2422
}
2523

2624
private String getUserProvidedLabel(ConfigProperties config) {
27-
return config.getString("otel.javaagent.extension.sap.cf.binding.user-provided.label",
28-
DEFAULT_USER_PROVIDED_LABEL);
25+
return DEPRECATED.RUNTIME.CLOUD_FOUNDRY.SERVICE.USER_PROVIDED.LABEL_OTEL.getValue(config);
2926
}
3027

3128
private String getDynatraceLabel(ConfigProperties config) {
32-
return config.getString("otel.javaagent.extension.sap.cf.binding.dynatrace.label", DEFAULT_DYNATRACE_LABEL);
29+
return RUNTIME.CLOUD_FOUNDRY.SERVICE.DYNATRACE.LABEL.getValue(config);
3330
}
3431

3532
private String getDynatraceTag(ConfigProperties config) {
36-
return config.getString("otel.javaagent.extension.sap.cf.binding.dynatrace.tag", DEFAULT_DYNATRACE_TAG);
33+
return RUNTIME.CLOUD_FOUNDRY.SERVICE.DYNATRACE.TAG.getValue(config);
3734
}
3835

3936
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.config;
2+
3+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
4+
5+
import java.time.Duration;
6+
import java.util.List;
7+
import java.util.function.BiFunction;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
10+
11+
import static java.util.Collections.emptyList;
12+
13+
public class ConfigProperty<T> {
14+
15+
private static final Logger LOG = Logger.getLogger(ConfigProperty.class.getName());
16+
17+
private final String key;
18+
private final ConfigProperty<T> fallback;
19+
private final T defaultValue;
20+
private final BiFunction<ConfigProperties, String, T> extractor;
21+
private final boolean deprecated;
22+
23+
private ConfigProperty(Builder<T> builder) {
24+
this.key = builder.key;
25+
this.fallback = builder.fallback;
26+
this.defaultValue = builder.defaultValue;
27+
this.extractor = builder.extractor;
28+
this.deprecated = builder.deprecated;
29+
}
30+
31+
public T getValue(ConfigProperties config) {
32+
if (config == null) {
33+
LOG.warning(
34+
"No configuration provided, using default value \"" + defaultValue + "\" for key: \"" + key + "\"");
35+
return defaultValue;
36+
}
37+
T directValue = extractor.apply(config, key);
38+
if (directValue != null) {
39+
return directValue;
40+
}
41+
if (fallback == null) {
42+
return defaultValue;
43+
}
44+
T fallbackValue = fallback.getValue(config);
45+
if (fallbackValue != null && !fallbackValue.equals(defaultValue)) {
46+
if (fallback.deprecated && LOG.isLoggable(Level.WARNING)) {
47+
LOG.warning(
48+
"Using deprecated configuration key \"" + fallback.key + "\". Please migrate to key: \"" + key + "\"");
49+
}
50+
return fallbackValue;
51+
}
52+
return defaultValue;
53+
}
54+
55+
public String getKey() {
56+
return key;
57+
}
58+
59+
T getDefaultValue() {
60+
return defaultValue;
61+
}
62+
63+
static Builder<String> stringValued(String key) {
64+
return new Builder<>(ConfigProperties::getString).withKey(key);
65+
}
66+
67+
static Builder<Boolean> booleanValued(String key) {
68+
return new Builder<>(ConfigProperties::getBoolean).withKey(key);
69+
}
70+
71+
static Builder<Duration> durationValued(String key) {
72+
return new Builder<>(ConfigProperties::getDuration).withKey(key);
73+
}
74+
75+
static Builder<List<String>> listValued(String key) {
76+
return new Builder<>(ConfigProperties::getList).withKey(key).withDefaultValue(emptyList());
77+
}
78+
79+
static class Builder<T> {
80+
private final BiFunction<ConfigProperties, String, T> extractor;
81+
private String key;
82+
private T defaultValue;
83+
private ConfigProperty<T> fallback;
84+
private boolean deprecated;
85+
86+
Builder(BiFunction<ConfigProperties, String, T> extractor) {
87+
this.extractor = extractor;
88+
}
89+
90+
private Builder<T> withKey(String key) {
91+
this.key = key;
92+
return this;
93+
}
94+
95+
Builder<T> withDefaultValue(T defaultValue) {
96+
this.defaultValue = defaultValue;
97+
return this;
98+
}
99+
100+
Builder<T> withFallback(ConfigProperty<T> fallback) {
101+
this.fallback = fallback;
102+
return this;
103+
}
104+
105+
Builder<T> setDeprecated(boolean deprecated) {
106+
this.deprecated = deprecated;
107+
return this;
108+
}
109+
110+
ConfigProperty<T> build() {
111+
return new ConfigProperty<>(this);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)