Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 136 additions & 61 deletions cf-java-logging-support-opentelemetry-agent-extension/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.attributes;

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

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

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

@Override
public Resource apply(Resource resource, ConfigProperties configProperties) {
boolean isEnabled = configProperties.getBoolean(OTEL_JAVAAGENT_EXTENSION_SAP_CF_RESOURCE_ENABLED, true);
boolean isEnabled = RESOURCE.CLOUD_FOUNDRY.ENABLED.getValue(configProperties);
if (!isEnabled) {
LOG.config("CF resource attributes are disabled by configuration.");
return Resource.empty();
Expand All @@ -48,7 +45,7 @@ public Resource apply(Resource resource, ConfigProperties configProperties) {
return resource;
}

String format = configProperties.getString(OTEL_JAVAAGENT_EXTENTION_CF_RESOURCE_FORMAT, "SAP");
String format = RESOURCE.CLOUD_FOUNDRY.FORMAT.getValue(configProperties);
if (!format.equalsIgnoreCase("SAP")) {
return resource;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;

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

private static ConfigProperties getDefaultProperties() {
Map<String, String> defaults = new HashMap<>();
defaults.put("com.sap.otel.extension.cloud-logging.label", "cloud-logging");
defaults.put("com.sap.otel.extension.cloud-logging.tag", "Cloud Logging");
defaults.put(DEPRECATED.RUNTIME.CLOUD_FOUNDRY.SERVICE.CLOUD_LOGGING.LABEL_SAP.getKey(), "cloud-logging");
defaults.put(DEPRECATED.RUNTIME.CLOUD_FOUNDRY.SERVICE.CLOUD_LOGGING.TAG_SAP.getKey(), "Cloud Logging");
defaults.put("otel.javaagent.extension.sap.cf.binding.user-provided.label", "user-provided");
ComponentLoader componentLoader =
ComponentLoader.forClassLoader(DefaultConfigProperties.class.getClassLoader());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;

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

import java.util.List;
Expand All @@ -10,10 +12,6 @@

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

private static final String DEFAULT_USER_PROVIDED_LABEL = "user-provided";
private static final String DEFAULT_CLOUD_LOGGING_LABEL = "cloud-logging";
private static final String DEFAULT_CLOUD_LOGGING_TAG = "Cloud Logging";

private final List<CloudFoundryServiceInstance> services;

public CloudLoggingServicesProvider(ConfigProperties config) {
Expand All @@ -27,20 +25,15 @@ public CloudLoggingServicesProvider(ConfigProperties config) {
}

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

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.binding;

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

import java.util.List;
import java.util.function.Supplier;

public class DynatraceServiceProvider implements Supplier<CloudFoundryServiceInstance> {

private static final String DEFAULT_USER_PROVIDED_LABEL = "user-provided";
private static final String DEFAULT_DYNATRACE_LABEL = "dynatrace";
private static final String DEFAULT_DYNATRACE_TAG = "dynatrace";

private final CloudFoundryServiceInstance service;

public DynatraceServiceProvider(ConfigProperties config) {
Expand All @@ -24,16 +22,15 @@ public DynatraceServiceProvider(ConfigProperties config) {
}

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

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.sap.hcf.cf.logging.opentelemetry.agent.ext.config;

import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;

import java.time.Duration;
import java.util.List;
import java.util.function.BiFunction;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.Collections.emptyList;

public class ConfigProperty<T> {

private static final Logger LOG = Logger.getLogger(ConfigProperty.class.getName());

private final String key;
private final ConfigProperty<T> fallback;
private final T defaultValue;
private final BiFunction<ConfigProperties, String, T> extractor;
private final boolean deprecated;

private ConfigProperty(Builder<T> builder) {
this.key = builder.key;
this.fallback = builder.fallback;
this.defaultValue = builder.defaultValue;
this.extractor = builder.extractor;
this.deprecated = builder.deprecated;
}

public T getValue(ConfigProperties config) {
if (config == null) {
LOG.warning(
"No configuration provided, using default value \"" + defaultValue + "\" for key: \"" + key + "\"");
return defaultValue;
}
T directValue = extractor.apply(config, key);
if (directValue != null) {
return directValue;
}
if (fallback == null) {
return defaultValue;
}
T fallbackValue = fallback.getValue(config);
if (fallbackValue != null && !fallbackValue.equals(defaultValue)) {
if (fallback.deprecated && LOG.isLoggable(Level.WARNING)) {
LOG.warning(
"Using deprecated configuration key \"" + fallback.key + "\". Please migrate to key: \"" + key + "\"");
}
return fallbackValue;
}
return defaultValue;
}

public String getKey() {
return key;
}

T getDefaultValue() {
return defaultValue;
}

static Builder<String> stringValued(String key) {
return new Builder<>(ConfigProperties::getString).withKey(key);
}

static Builder<Boolean> booleanValued(String key) {
return new Builder<>(ConfigProperties::getBoolean).withKey(key);
}

static Builder<Duration> durationValued(String key) {
return new Builder<>(ConfigProperties::getDuration).withKey(key);
}

static Builder<List<String>> listValued(String key) {
return new Builder<>(ConfigProperties::getList).withKey(key).withDefaultValue(emptyList());
}

static class Builder<T> {
private final BiFunction<ConfigProperties, String, T> extractor;
private String key;
private T defaultValue;
private ConfigProperty<T> fallback;
private boolean deprecated;

Builder(BiFunction<ConfigProperties, String, T> extractor) {
this.extractor = extractor;
}

private Builder<T> withKey(String key) {
this.key = key;
return this;
}

Builder<T> withDefaultValue(T defaultValue) {
this.defaultValue = defaultValue;
return this;
}

Builder<T> withFallback(ConfigProperty<T> fallback) {
this.fallback = fallback;
return this;
}

Builder<T> setDeprecated(boolean deprecated) {
this.deprecated = deprecated;
return this;
}

ConfigProperty<T> build() {
return new ConfigProperty<>(this);
}
}
}
Loading