Skip to content

Commit

Permalink
Merge branch 'main' into feat/java-17-support
Browse files Browse the repository at this point in the history
  • Loading branch information
manikmagar authored Oct 10, 2024
2 parents 637730c + e1b78c6 commit 990d177
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 188 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<description>Mule Extension for generating OpenTelemetry traces from Mule Applications</description>
<url>https://github.com/avioconsulting/mule-opentelemetry-module</url>


<developers>
<developer>
<name>Adam DesJardin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ default Optional<String> prevContextScopedPath(String path) {
* @return String
*/
default String contextScopedLocation() {
return getEventContextId() + "/" + getLocation();
return contextScopedLocationFor(getEventContextId(), getLocation());
}

static String contextScopedLocationFor(String eventContextId, String location) {
return eventContextId + "/" + location;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.avioconsulting.mule.opentelemetry.internal;

import com.avioconsulting.mule.opentelemetry.internal.connection.OpenTelemetryConnection;
import com.avioconsulting.mule.opentelemetry.api.traces.ComponentEventContext;
import com.avioconsulting.mule.opentelemetry.internal.config.OpenTelemetryExtensionConfiguration;
import com.avioconsulting.mule.opentelemetry.internal.util.OpenTelemetryUtil;
import org.mule.runtime.api.component.location.ComponentLocation;
import org.mule.runtime.extension.api.annotation.Alias;
import org.mule.runtime.extension.api.annotation.param.Connection;
import org.mule.runtime.extension.api.annotation.param.Config;
import org.mule.runtime.extension.api.annotation.param.Optional;
import org.mule.runtime.extension.api.annotation.param.display.DisplayName;
import org.mule.runtime.extension.api.annotation.param.display.Summary;
Expand All @@ -14,7 +16,6 @@
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.function.Supplier;

public class OpenTelemetryOperations {

Expand All @@ -24,8 +25,8 @@ public class OpenTelemetryOperations {
* Deprecated: Use Get Current Trace Context instead. When OTEL_TRACE_CONTEXT
* does not pre-exist, there is no way for users to get current transaction id.
*
* @param openTelemetryConnection
* {@link OpenTelemetryConnection} Instance
* @param config
* {@link OpenTelemetryExtensionConfiguration} Instance
* @param traceTransactionId
* provided by user
* @param correlationInfo
Expand All @@ -35,18 +36,18 @@ public class OpenTelemetryOperations {
@DisplayName("Get Trace Context")
@Alias("get-trace-context")
@Deprecated(message = "Use Get Current Trace Context instead. When OTEL_TRACE_CONTEXT does not pre-exist, there is no way for users to get current transaction id.", since = "2.3.0", toRemoveIn = "3.0.0")
public Map<String, String> getTraceContext(@Connection Supplier<OpenTelemetryConnection> openTelemetryConnection,
public Map<String, String> getTraceContext(@Config OpenTelemetryExtensionConfiguration config,
@DisplayName("Trace Transaction Id") @Optional(defaultValue = "#[vars.OTEL_TRACE_CONTEXT.TRACE_TRANSACTION_ID]") ParameterResolver<String> traceTransactionId,
CorrelationInfo correlationInfo) {
LOGGER.warn("get-trace-context has been deprecated. Use get-current-trace-context instead");
return openTelemetryConnection.get().getTraceContext(traceTransactionId.resolve());
return config.getOpenTelemetryConnection().getTraceContext(traceTransactionId.resolve());
}

/**
* Get the trace context for current trace transaction.
*
* @param openTelemetryConnection
* {@link OpenTelemetryConnection} Instance
* @param config
* {@link OpenTelemetryExtensionConfiguration} Instance
* @param correlationInfo
* {@link CorrelationInfo} (injected by runtime) to extract the
* current event id
Expand All @@ -56,22 +57,25 @@ public Map<String, String> getTraceContext(@Connection Supplier<OpenTelemetryCon
@Alias("get-current-trace-context")
@Summary("Gets the current trace context")
public Map<String, String> getCurrentTraceContext(
@Connection Supplier<OpenTelemetryConnection> openTelemetryConnection,
CorrelationInfo correlationInfo) {
@Config OpenTelemetryExtensionConfiguration config,
CorrelationInfo correlationInfo, ComponentLocation location) {
String eventTransactionId = OpenTelemetryUtil.getEventTransactionId(correlationInfo.getEventId());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Getting current context for event Id: {}, correlationId: {}, trace transactionId: {}",
correlationInfo.getEventId(), correlationInfo.getCorrelationId(), eventTransactionId);
LOGGER.debug(
"Getting current context for event Id: {}, correlationId: {}, trace transactionId: {} at location {} in container {}",
correlationInfo.getEventId(), correlationInfo.getCorrelationId(), eventTransactionId,
location.getLocation(), location.getRootContainerName());
}
return openTelemetryConnection.get().getTraceContext(eventTransactionId);
return config.getOpenTelemetryConnection().getTraceContext(eventTransactionId, ComponentEventContext
.contextScopedLocationFor(correlationInfo.getEventId(), location.getRootContainerName()));
}

/**
* Deprecated: Use addTransactionTags instead. When OTEL_TRACE_CONTEXT does not
* pre-exist,there is no way for users to get current transaction id.
*
* @param openTelemetryConnection
* {@link OpenTelemetryConnection} provided by the SDK
* @param config
* {@link OpenTelemetryExtensionConfiguration} provided by the SDK
* @param tags
* {@link Map} of {@link String} Keys and {@link String} Values
* containing the tags. Behavior of null values in the map is
Expand All @@ -81,12 +85,12 @@ public Map<String, String> getCurrentTraceContext(
*/
@DisplayName("Add Custom Tags")
@Deprecated(message = "Use addTransactionTags instead. When OTEL_TRACE_CONTEXT does not pre-exist, there is no way for users to get current transaction id.", since = "2.3.0", toRemoveIn = "3.0.0")
public void addCustomTags(@Connection Supplier<OpenTelemetryConnection> openTelemetryConnection,
public void addCustomTags(@Config OpenTelemetryExtensionConfiguration config,
@DisplayName("Trace Transaction Id") @Optional(defaultValue = "#[vars.OTEL_TRACE_CONTEXT.TRACE_TRANSACTION_ID]") ParameterResolver<String> traceTransactionId,
Map<String, String> tags,
CorrelationInfo correlationInfo) {
LOGGER.warn("add-custom-tags has been deprecated. Use add-transaction-tags instead.");
openTelemetryConnection.get().getTransactionStore().addTransactionTags(traceTransactionId.resolve(),
config.getOpenTelemetryConnection().getTransactionStore().addTransactionTags(traceTransactionId.resolve(),
"custom",
tags);
}
Expand All @@ -99,8 +103,8 @@ public void addCustomTags(@Connection Supplier<OpenTelemetryConnection> openTele
* If the transaction's root span previously contained a mapping for the key,
* the old value is replaced by the new value.
*
* @param openTelemetryConnection
* {@link OpenTelemetryConnection} provided by the SDK
* @param config
* {@link OpenTelemetryExtensionConfiguration} provided by the SDK
* @param tags
* {@link Map} of {@link String} Keys and {@link String} Values
* containing the tags. Behavior of null values in the map is
Expand All @@ -109,15 +113,15 @@ public void addCustomTags(@Connection Supplier<OpenTelemetryConnection> openTele
* {@link CorrelationInfo} from the runtime
*/
@DisplayName("Add Transaction Tags")
public void addTransactionTags(@Connection Supplier<OpenTelemetryConnection> openTelemetryConnection,
public void addTransactionTags(@Config OpenTelemetryExtensionConfiguration config,
Map<String, String> tags,
CorrelationInfo correlationInfo) {
String eventTransactionId = OpenTelemetryUtil.getEventTransactionId(correlationInfo.getEventId());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Add Transaction Tags for event Id: {}, correlationId: {}, trace transactionId: {}",
correlationInfo.getEventId(), correlationInfo.getCorrelationId(), eventTransactionId);
}
openTelemetryConnection.get().getTransactionStore().addTransactionTags(eventTransactionId,
config.getOpenTelemetryConnection().getTransactionStore().addTransactionTags(eventTransactionId,
"custom",
tags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.avioconsulting.mule.opentelemetry.api.providers.OpenTelemetryMetricsConfigSupplier;
import com.avioconsulting.mule.opentelemetry.internal.OpenTelemetryOperations;
import com.avioconsulting.mule.opentelemetry.internal.connection.OpenTelemetryConnection;
import com.avioconsulting.mule.opentelemetry.internal.connection.OpenTelemetryConnectionProvider;
import com.avioconsulting.mule.opentelemetry.internal.notifications.listeners.AsyncMessageNotificationListener;
import com.avioconsulting.mule.opentelemetry.internal.notifications.listeners.MetricEventNotificationListener;
import com.avioconsulting.mule.opentelemetry.internal.notifications.listeners.MuleMessageProcessorNotificationListener;
Expand All @@ -27,7 +26,6 @@
import org.mule.runtime.extension.api.annotation.Configuration;
import org.mule.runtime.extension.api.annotation.Expression;
import org.mule.runtime.extension.api.annotation.Operations;
import org.mule.runtime.extension.api.annotation.connectivity.ConnectionProviders;
import org.mule.runtime.extension.api.annotation.param.Optional;
import org.mule.runtime.extension.api.annotation.param.Parameter;
import org.mule.runtime.extension.api.annotation.param.ParameterGroup;
Expand All @@ -42,7 +40,6 @@
import javax.inject.Inject;

@Operations(OpenTelemetryOperations.class)
@ConnectionProviders(OpenTelemetryConnectionProvider.class)
@Configuration
public class OpenTelemetryExtensionConfiguration
implements Startable, Stoppable, OpenTelemetryConfiguration, OpenTelemetryMetricsConfigSupplier {
Expand All @@ -60,6 +57,7 @@ public class OpenTelemetryExtensionConfiguration
@Inject
private ExpressionManager expressionManager;
private AppIdentifier appIdentifier;
private OpenTelemetryConnection openTelemetryConnection;

public HttpService getHttpService() {
return httpService;
Expand Down Expand Up @@ -179,6 +177,10 @@ public ExpressionManager getExpressionManager() {
return expressionManager;
}

public OpenTelemetryConnection getOpenTelemetryConnection() {
return openTelemetryConnection;
}

@Override
public String getConfigName() {
return configName;
Expand All @@ -194,7 +196,7 @@ public String getConfigName() {
public void start() throws MuleException {
logger.info("Initiating otel config - '{}'", getConfigName());
appIdentifier = AppIdentifier.fromEnvironment(expressionManager);
OpenTelemetryConnection openTelemetryConnection = OpenTelemetryConnection
openTelemetryConnection = OpenTelemetryConnection
.getInstance(new OpenTelemetryConfigWrapper(this));
muleNotificationProcessor.init(openTelemetryConnection,
getTraceLevelConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import com.avioconsulting.mule.opentelemetry.internal.util.OpenTelemetryUtil;
import com.avioconsulting.mule.opentelemetry.internal.util.PropertiesUtil;
import com.avioconsulting.mule.opentelemetry.internal.util.ServiceProviderUtil;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.events.GlobalEventEmitterProvider;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.trace.*;
import io.opentelemetry.context.Context;
Expand Down Expand Up @@ -72,7 +70,7 @@ public class OpenTelemetryConnection implements TraceContextHandler {

private static final String INSTRUMENTATION_NAME = "mule-opentelemetry-module-DEV";
private final TransactionStore transactionStore;
private static OpenTelemetryConnection openTelemetryConnection;
private OpenTelemetryConnection openTelemetryConnection;
private final OpenTelemetry openTelemetry;
private final Tracer tracer;
private boolean turnOffTracing = false;
Expand Down Expand Up @@ -110,7 +108,6 @@ private OpenTelemetryConnection(OpenTelemetryConfigWrapper openTelemetryConfigWr
expressionManager = openTelemetryConfigWrapper.getOpenTelemetryConfiguration().getExpressionManager();
}
builder.setServiceClassLoader(AutoConfiguredOpenTelemetrySdkBuilder.class.getClassLoader());
builder.setResultAsGlobal();
if (!turnOffMetrics)
metricsProvider.initialise(appIdentifier);
openTelemetry = builder.build().getOpenTelemetrySdk();
Expand All @@ -126,6 +123,7 @@ private OpenTelemetryConnection(OpenTelemetryConfigWrapper openTelemetryConfigWr
tracer = openTelemetry.getTracer(instrumentationName, instrumentationVersion);
transactionStore = InMemoryTransactionStore.getInstance();
PropertiesUtil.init();
openTelemetryConnection = this;
}

private void installOpenTelemetryLogger() {
Expand Down Expand Up @@ -159,41 +157,17 @@ public OpenTelemetryMetricsProviderCollection getMetricsProviders() {
return metricsProviders;
}

/**
* {@link Supplier} to use with
* {@link ConnectionProvider} where lazy
* initialization is required.
*
* @return a non-null {@code Supplier<OpenTelemetryConnection>}
*/
public static Supplier<OpenTelemetryConnection> supplier() {
return () -> openTelemetryConnection;
}

/**
* This is for tests to reset the static instance in-between the tests.
* Reset Global OpenTelemetry instances.
*/
public static void resetForTest() {
if (openTelemetryConnection != null && openTelemetryConnection.metricsProvider != null) {
openTelemetryConnection.metricsProvider.stop();
openTelemetryConnection.getMetricsProviders().stop();
}
GlobalOpenTelemetry.resetForTest();
GlobalEventEmitterProvider.resetForTest();
openTelemetryConnection = null;

}

public static synchronized OpenTelemetryConnection getInstance(
OpenTelemetryConfigWrapper openTelemetryConfigWrapper) {
if (openTelemetryConnection == null) {
openTelemetryConnection = new OpenTelemetryConnection(openTelemetryConfigWrapper);
}
return openTelemetryConnection;
}

public static synchronized OpenTelemetryConnection getInstance() {
return openTelemetryConnection;
return new OpenTelemetryConnection(openTelemetryConfigWrapper);
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.avioconsulting.mule.opentelemetry.api.traces.TraceComponent;
import com.avioconsulting.mule.opentelemetry.internal.processor.MuleNotificationProcessor;
import com.avioconsulting.mule.opentelemetry.internal.util.ComponentsUtil;
import io.opentelemetry.api.trace.SpanKind;
import org.mule.runtime.api.component.Component;
import org.mule.runtime.api.component.location.ComponentLocation;
import org.mule.runtime.api.component.location.ConfigurationComponentLocator;
Expand All @@ -18,12 +17,10 @@
import org.slf4j.LoggerFactory;

import javax.annotation.concurrent.ThreadSafe;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import static com.avioconsulting.mule.opentelemetry.api.sdk.SemanticAttributes.MULE_APP_SCOPE_SUBFLOW_NAME;
import static com.avioconsulting.mule.opentelemetry.api.store.TransactionStore.*;
import static com.avioconsulting.mule.opentelemetry.internal.util.ComponentsUtil.*;
import static com.avioconsulting.mule.opentelemetry.internal.util.OpenTelemetryUtil.getEventTransactionId;
Expand Down Expand Up @@ -109,29 +106,11 @@ public void before(
getLocationParent(location.getLocation()));
final String transactionId = getEventTransactionId(event);
if (isFlowRef(location)) {
String targetFlowName = traceComponent.getTags().get("mule.app.processor.flowRef.name");
if (muleNotificationProcessor.getOpenTelemetryConnection().getExpressionManager()
.isExpression(targetFlowName)) {
targetFlowName = muleNotificationProcessor.getOpenTelemetryConnection().getExpressionManager()
.evaluate(targetFlowName, event.asBindingContext()).getValue().toString();
traceComponent.getTags().put("mule.app.processor.flowRef.name", targetFlowName);
}
Optional<ComponentLocation> subFlowLocation = findLocation(
targetFlowName,
configurationComponentLocator)
.filter(ComponentsUtil::isSubFlow);
Optional<ComponentLocation> subFlowLocation = resolveFlowName(
muleNotificationProcessor.getOpenTelemetryConnection().getExpressionManager(),
traceComponent, event.asBindingContext(), configurationComponentLocator);
if (subFlowLocation.isPresent()) {
ComponentLocation subFlowComp = subFlowLocation.get();
TraceComponent subflowTrace = TraceComponent.of(subFlowComp)
.withTransactionId(traceComponent.getTransactionId())
.withSpanName(subFlowComp.getLocation())
.withSpanKind(SpanKind.INTERNAL)
.withTags(Collections.singletonMap(MULE_APP_SCOPE_SUBFLOW_NAME.getKey(),
subFlowComp.getLocation()))
.withStatsCode(traceComponent.getStatusCode())
.withStartTime(traceComponent.getStartTime())
.withContext(traceComponent.getContext())
.withEventContextId(traceComponent.getEventContextId());
TraceComponent subflowTrace = getTraceComponent(subFlowLocation.get(), traceComponent);
muleNotificationProcessor.getOpenTelemetryConnection().addProcessorSpan(subflowTrace,
location.getLocation());
event.addVariable(TRACE_CONTEXT_MAP_KEY,
Expand Down
Loading

0 comments on commit 990d177

Please sign in to comment.