diff --git a/trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelTracePlugin.java b/trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelTracePlugin.java index 129d5f44..b5355a28 100644 --- a/trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelTracePlugin.java +++ b/trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelTracePlugin.java @@ -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; @@ -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 @@ -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 exp = new ArrayList<>(); - if ( traceConfiguration.isConsoleTransport() ) - { - exp.add( LoggingSpanExporter.create() ); - } - - OtlpGrpcSpanExporterBuilder grpcExporterBuilder = OtlpGrpcSpanExporter.builder(); - grpcExporterBuilder.setEndpoint( grpcEndpoint ); - Map 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 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() ); @@ -150,4 +68,5 @@ public ThreadTracingContext getThreadTracingContext() { return threadTracingContext; } + } diff --git a/trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelUtil.java b/trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelUtil.java new file mode 100644 index 00000000..687ff0a9 --- /dev/null +++ b/trace/otel/src/main/java/org/commonjava/o11yphant/otel/OtelUtil.java @@ -0,0 +1,111 @@ +/** + * Copyright (C) 2020-2023 Red Hat, Inc. (https://github.com/commonjava/o11yphant) + * + * 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 exp = new ArrayList<>(); + if ( traceConfiguration.isConsoleTransport() ) + { + exp.add( LoggingSpanExporter.create() ); + } + + OtlpGrpcSpanExporterBuilder grpcExporterBuilder = OtlpGrpcSpanExporter.builder(); + grpcExporterBuilder.setEndpoint( grpcEndpoint ); + Map 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 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; + } + +} diff --git a/trace/otel/src/main/java/org/commonjava/o11yphant/otel/impl/adapter/OtelSpan.java b/trace/otel/src/main/java/org/commonjava/o11yphant/otel/impl/adapter/OtelSpan.java index 3e36f001..823753d7 100644 --- a/trace/otel/src/main/java/org/commonjava/o11yphant/otel/impl/adapter/OtelSpan.java +++ b/trace/otel/src/main/java/org/commonjava/o11yphant/otel/impl/adapter/OtelSpan.java @@ -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(); }