Skip to content

Commit

Permalink
Merge pull request #12175 from senthuran16/OTEL_RESOURCE_ATTRIBUTES_m…
Browse files Browse the repository at this point in the history
…aster

Add Implementation to provide OTEL Resource Attributes
  • Loading branch information
dushaniw authored Oct 31, 2023
2 parents 1e485ff + 5a21822 commit d644af0
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
package org.wso2.carbon.apimgt.tracing.telemetry;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.jaeger.JaegerGrpcSpanExporter;
import io.opentelemetry.extension.trace.propagation.JaegerPropagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
Expand Down Expand Up @@ -68,11 +65,9 @@ public void init(String serviceName) {
log.debug("Jaeger exporter: " + jaegerExporter + " is configured at http://" + hostname + ":" + port);
}

Resource serviceNameResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName));

sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(jaegerExporter).build())
.setResource(Resource.getDefault().merge(serviceNameResource))
.setResource(TelemetryUtil.getTracerProviderResource(serviceName))
.build();

openTelemetry = OpenTelemetrySdk.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
package org.wso2.carbon.apimgt.tracing.telemetry;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -71,11 +68,9 @@ public void init(String serviceName) {
log.debug("OTLP exporter: " + otlpGrpcSpanExporterBuilder + " is configured at " + endPointURL);
}

Resource serviceNameResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName));

sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(otlpGrpcSpanExporterBuilder.build()).build())
.setResource(Resource.getDefault().merge(serviceNameResource))
.setResource(TelemetryUtil.getTracerProviderResource(serviceName))
.build();

openTelemetry = OpenTelemetrySdk.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class TelemetryConstants {
*/
static final String OTLP_CONFIG_URL = "OpenTelemetry.RemoteTracer.Url";
static final String OPENTELEMETRY_PROPERTIES_PREFIX = "OpenTelemetry.RemoteTracer.Properties.";
static final String OTEL_RESOURCE_ATTRIBUTE_CONFIG_KEYS_PREFIX = "OpenTelemetry.ResourceAttributes.";
static final String OTEL_RESOURCE_ATTRIBUTES_ENVIRONMENT_VARIABLE_NAME = "OTEL_RESOURCE_ATTRIBUTES";

private TelemetryConstants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,28 @@
package org.wso2.carbon.apimgt.tracing.telemetry;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
import org.wso2.carbon.apimgt.tracing.internal.ServiceReferenceHolder;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.wso2.carbon.apimgt.tracing.telemetry.TelemetryConstants.OTEL_RESOURCE_ATTRIBUTES_ENVIRONMENT_VARIABLE_NAME;
import static org.wso2.carbon.apimgt.tracing.telemetry.TelemetryConstants.OTEL_RESOURCE_ATTRIBUTE_CONFIG_KEYS_PREFIX;

/**
* Span utility class.
Expand Down Expand Up @@ -229,6 +240,51 @@ public static boolean telemetryEnabled() {
return false;
}

/**
* Gets the tracer provider resource with the provided default service name.
*
* @param defaultServiceName Default service name.
* @return Tracer provider resource.
*/
public static Resource getTracerProviderResource(String defaultServiceName) {
APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfiguration();
Map<String, String> otelResourceAttributes = new HashMap<>();

// Get resource attributes from configuration
Set<String> otelResourceAttributeConfigKeys = configuration.getConfigKeySet().stream()
.filter(entry -> entry.startsWith(OTEL_RESOURCE_ATTRIBUTE_CONFIG_KEYS_PREFIX))
.collect(Collectors.toSet());
for (String configKey : otelResourceAttributeConfigKeys) {
String otelResourceAttributeKey = configKey.substring(OTEL_RESOURCE_ATTRIBUTE_CONFIG_KEYS_PREFIX.length());
otelResourceAttributes.put(otelResourceAttributeKey, configuration.getFirstProperty(configKey));
}

/* Get resource attributes from environment variables. If a resource attribute's value has been already
provided via configuration, the value provided via environment variable will overwrite that. */
String environmentVariableValue = System.getenv(OTEL_RESOURCE_ATTRIBUTES_ENVIRONMENT_VARIABLE_NAME);
if (environmentVariableValue != null) {
String[] resourceAttributes = StringUtils.split(environmentVariableValue, ",");
for (String keyValuePair : resourceAttributes) {
String[] keyValue = StringUtils.split(keyValuePair, "=");
otelResourceAttributes.put(keyValue[0], keyValue[1]);
}
}

AttributesBuilder attributesBuilder = Attributes.builder();
for (Map.Entry<String, String> otelResourceAttribute : otelResourceAttributes.entrySet()) {
attributesBuilder.put(otelResourceAttribute.getKey(), otelResourceAttribute.getValue());
}
Attributes attributes = attributesBuilder.build();

Resource tracerProviderResource = Resource.getDefault();
Resource serviceNameResource = Resource.create(
Attributes.of(ResourceAttributes.SERVICE_NAME, defaultServiceName));
tracerProviderResource = tracerProviderResource.merge(serviceNameResource);
tracerProviderResource = tracerProviderResource.merge(Resource.create(attributes));

return tracerProviderResource;
}

private TelemetryUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
package org.wso2.carbon.apimgt.tracing.telemetry;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
Expand Down Expand Up @@ -65,11 +62,9 @@ public void init(String serviceName) {
log.debug("Zipkin exporter: " + zipkinExporter + " is configured at http://" + hostname + ":" + port);
}

Resource serviceNameResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, serviceName));

sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(zipkinExporter).build())
.setResource(Resource.getDefault().merge(serviceNameResource))
.setResource(TelemetryUtil.getTracerProviderResource(serviceName))
.build();

openTelemetry = OpenTelemetrySdk.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,11 @@
<LogTracer>
<Enabled>{{apim.open_telemetry.log_tracer.enable}}</Enabled>
</LogTracer>
<ResourceAttributes>
{% for attribute in apim.open_telemetry.resource_attributes %}
<{{attribute.name}}>{{attribute.value}}</{{attribute.name}}>
{% endfor %}
</ResourceAttributes>
</OpenTelemetry>

<OpenTracer>
Expand Down

0 comments on commit d644af0

Please sign in to comment.