Skip to content

Commit

Permalink
Added builder for TracingConfig to DittoTracingInitResource to cr…
Browse files Browse the repository at this point in the history
…eate a config directly at instantiation.

This fixed a bug, too, because the config path prefix "tracing" was missing in the previous revision.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Oct 18, 2022
1 parent 85098d3 commit eadeefd
Showing 1 changed file with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
*/
package org.eclipse.ditto.internal.utils.tracing;

import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import static org.eclipse.ditto.internal.utils.tracing.config.TracingConfig.TracingConfigValue.TRACING_ENABLED;
import static org.eclipse.ditto.internal.utils.tracing.config.TracingConfig.TracingConfigValue.TRACING_PROPAGATION_CHANNEL;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.concurrent.NotThreadSafe;

import org.eclipse.ditto.base.model.common.ConditionChecker;
import org.eclipse.ditto.internal.utils.tracing.config.DefaultTracingConfig;
import org.eclipse.ditto.internal.utils.tracing.config.TracingConfig;
import org.junit.rules.ExternalResource;
Expand All @@ -46,9 +47,10 @@ private DittoTracingInitResource(final TracingConfig tracingConfig) {
* @param tracingConfig the configuration properties for {@code DittoTracing}.
* @return the new instance.
* @throws NullPointerException if {@code tracingConfig} is {@code null}.
* @see TracingConfigBuilder#defaultValues()
*/
public static DittoTracingInitResource newInstance(final TracingConfig tracingConfig) {
return new DittoTracingInitResource(ConditionChecker.checkNotNull(tracingConfig, "tracingConfig"));
return new DittoTracingInitResource(checkNotNull(tracingConfig, "tracingConfig"));
}

/**
Expand All @@ -57,12 +59,7 @@ public static DittoTracingInitResource newInstance(final TracingConfig tracingCo
* @return the new instance.
*/
public static DittoTracingInitResource disableDittoTracing() {
return newInstance(
DefaultTracingConfig.of(ConfigFactory.parseMap(Map.of(
TRACING_ENABLED.getConfigPath(), "false",
TRACING_PROPAGATION_CHANNEL.getConfigPath(), TRACING_PROPAGATION_CHANNEL.getDefaultValue()
)))
);
return newInstance(TracingConfigBuilder.defaultValues().withTracingDisabled().build());
}

@Override
Expand All @@ -77,4 +74,49 @@ protected void after() {
super.after();
}

@NotThreadSafe
public static final class TracingConfigBuilder {

private final Map<String, Object> rawConfigMap;

private TracingConfigBuilder() {
rawConfigMap = new HashMap<>();
}

public static TracingConfigBuilder defaultValues() {
final var result = new TracingConfigBuilder();
result.setTracingEnabledValue(TRACING_ENABLED.getDefaultValue());
result.setTracingPropagationChannelValue(TRACING_PROPAGATION_CHANNEL.getDefaultValue());
return result;
}

public TracingConfigBuilder withTracingEnabled() {
setTracingEnabledValue(true);
return this;
}

private void setTracingEnabledValue(final Object tracingEnabled) {
rawConfigMap.put(TRACING_ENABLED.getConfigPath(), tracingEnabled);
}

public TracingConfigBuilder withTracingDisabled() {
setTracingEnabledValue(false);
return this;
}

public TracingConfigBuilder withTracingPropagationChannel(final CharSequence propagationChannelName) {
setTracingPropagationChannelValue(checkNotNull(propagationChannelName, "propagationChannelName"));
return this;
}

private void setTracingPropagationChannelValue(final Object propagationChannelName) {
rawConfigMap.put(TRACING_PROPAGATION_CHANNEL.getConfigPath(), propagationChannelName);
}

public TracingConfig build() {
return DefaultTracingConfig.of(ConfigFactory.parseMap(Map.of("tracing", rawConfigMap)));
}

}

}

0 comments on commit eadeefd

Please sign in to comment.