Skip to content

Commit

Permalink
Add OtelUtil to support OpenTelemetry creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ligangty committed Sep 18, 2023
1 parent c00edd8 commit fe0fc70
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,8 @@
*/
package org.commonjava.o11yphant.otel;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
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.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.commonjava.o11yphant.otel.impl.OtelContextPropagator;
import org.commonjava.o11yphant.otel.impl.OtelSpanProvider;
Expand All @@ -40,12 +26,6 @@
import org.commonjava.o11yphant.trace.spi.O11yphantTracePlugin;
import org.commonjava.o11yphant.trace.spi.SpanProvider;
import org.commonjava.o11yphant.trace.thread.ThreadTracingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OtelTracePlugin
implements O11yphantTracePlugin
Expand All @@ -61,69 +41,7 @@ public OtelTracePlugin( TracerConfiguration traceConfiguration, OtelConfiguratio
{
if ( traceConfiguration.isEnabled() )
{
final Logger logger = LoggerFactory.getLogger( this.getClass() );
logger.debug( "Trace enabled with Otel trace plugin." );
SpanExporter[] spanExporters = exporters;
//FIXME: This needs to be more exposed to configuration options, especially for endpoint and exporter formats.
if ( exporters == null || exporters.length < 1 )
{
final String grpcEndpoint = otelConfig.getGrpcEndpointUri();
logger.info( "Trace grpc endpoint is configured as: {}", grpcEndpoint );
List<SpanExporter> exp = new ArrayList<>();
if ( traceConfiguration.isConsoleTransport() )
{
exp.add( LoggingSpanExporter.create() );
}

OtlpGrpcSpanExporterBuilder grpcExporterBuilder = OtlpGrpcSpanExporter.builder();
grpcExporterBuilder.setEndpoint( grpcEndpoint );
Map<String, String> exporterHeaders = otelConfig.getGrpcHeaders();
if ( exporterHeaders != null )
{
exporterHeaders.forEach( grpcExporterBuilder::addHeader );
}

grpcExporterBuilder.build();
exp.add( grpcExporterBuilder.build() );

spanExporters = exp.toArray( new SpanExporter[] {} );
}

SpanProcessor processor = BatchSpanProcessor.builder( SpanExporter.composite( spanExporters ) ).build();

SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder().addSpanProcessor( processor );

Map<String, String> otelResources = otelConfig.getResources();
if ( otelResources != null && !otelResources.isEmpty() )
{
logger.debug( "Additional Trace Attributes for OTEL: {}", otelResources );
AttributesBuilder builder = Attributes.builder();
otelResources.forEach( builder::put );
Resource resource = Resource.getDefault().merge( Resource.create( builder.build() ) );
tracerProviderBuilder.setResource( resource );
}

SdkTracerProvider tracerProvider = tracerProviderBuilder.build();

OpenTelemetry otel;

try
{
otel = OpenTelemetrySdk.builder()
.setTracerProvider( tracerProvider )
.setPropagators(
ContextPropagators.create( W3CTraceContextPropagator.getInstance() ) )
.build();
logger.debug("The OpenTelemetry instance has not been setup successfully");
}
catch ( IllegalStateException e )
{
logger.warn(
"The OpenTelemetry instance has not been setup successfully, will use a global one. Error: {}",
e.getMessage() );
otel = GlobalOpenTelemetry.get();
}

OpenTelemetry otel = OtelUtil.getOpenTelemetry( traceConfiguration, otelConfig, exporters );
Tracer tracer =
otel.getTracer( otelConfig.getInstrumentationName(), otelConfig.getInstrumentationVersion() );

Expand All @@ -150,4 +68,5 @@ public ThreadTracingContext getThreadTracingContext()
{
return threadTracingContext;
}

}
111 changes: 111 additions & 0 deletions trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* Copyright (C) 2020 Red Hat, Inc. ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.o11yphant.otel;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
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.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.commonjava.o11yphant.trace.TracerConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OtelUtil
{
private final static Logger logger = LoggerFactory.getLogger( OtelUtil.class );

public static OpenTelemetry getOpenTelemetry( TracerConfiguration traceConfiguration, OtelConfiguration otelConfig,
SpanExporter... exporters )
{
logger.debug( "Trace enabled with Otel trace plugin." );
SpanExporter[] spanExporters = exporters;
//FIXME: This needs to be more exposed to configuration options, especially for endpoint and exporter formats.
if ( exporters == null || exporters.length < 1 )
{
final String grpcEndpoint = otelConfig.getGrpcEndpointUri();
logger.info( "Trace grpc endpoint is configured as: {}", grpcEndpoint );
List<SpanExporter> exp = new ArrayList<>();
if ( traceConfiguration.isConsoleTransport() )
{
exp.add( LoggingSpanExporter.create() );
}

OtlpGrpcSpanExporterBuilder grpcExporterBuilder = OtlpGrpcSpanExporter.builder();
grpcExporterBuilder.setEndpoint( grpcEndpoint );
Map<String, String> exporterHeaders = otelConfig.getGrpcHeaders();
if ( exporterHeaders != null )
{
exporterHeaders.forEach( grpcExporterBuilder::addHeader );
}

grpcExporterBuilder.build();
exp.add( grpcExporterBuilder.build() );

spanExporters = exp.toArray( new SpanExporter[] {} );
}

SpanProcessor processor = BatchSpanProcessor.builder( SpanExporter.composite( spanExporters ) ).build();

SdkTracerProviderBuilder tracerProviderBuilder = SdkTracerProvider.builder().addSpanProcessor( processor );

Map<String, String> otelResources = otelConfig.getResources();
if ( otelResources != null && !otelResources.isEmpty() )
{
logger.debug( "Additional Trace Attributes for OTEL: {}", otelResources );
AttributesBuilder builder = Attributes.builder();
otelResources.forEach( builder::put );
Resource resource = Resource.getDefault().merge( Resource.create( builder.build() ) );
tracerProviderBuilder.setResource( resource );
}

SdkTracerProvider tracerProvider = tracerProviderBuilder.build();

OpenTelemetry otel;
try
{
otel = OpenTelemetrySdk.builder()
.setTracerProvider( tracerProvider )
.setPropagators(
ContextPropagators.create( W3CTraceContextPropagator.getInstance() ) )
.build();
logger.debug( "The OpenTelemetry instance has been setup successfully." );
}
catch ( IllegalStateException e )
{
logger.warn( "The OpenTelemetry instance has not been setup successfully, will use a global one. Error: {}",
e.getMessage() );
otel = GlobalOpenTelemetry.get();
}
return otel;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public void close()

SpanContext context = span.getSpanContext();
logger.trace( "Closing span {} in trace {}", getSpanId(), getTraceId() );
logger.trace( "Span in progress attributes {}", inProgress );
logger.trace( "Span attributes {}", attributes );
logger.trace( "==============================" );
span.end();
}

Expand Down

0 comments on commit fe0fc70

Please sign in to comment.