diff --git a/src/backend/common/common-service/src/main/kotlin/com/tencent/bkrepo/common/service/otel/web/OtelWebConfiguration.kt b/src/backend/common/common-service/src/main/kotlin/com/tencent/bkrepo/common/service/otel/web/OtelWebConfiguration.kt index 0af46b7a29..cc1c755b78 100644 --- a/src/backend/common/common-service/src/main/kotlin/com/tencent/bkrepo/common/service/otel/web/OtelWebConfiguration.kt +++ b/src/backend/common/common-service/src/main/kotlin/com/tencent/bkrepo/common/service/otel/web/OtelWebConfiguration.kt @@ -27,10 +27,18 @@ package com.tencent.bkrepo.common.service.otel.web +import io.opentelemetry.sdk.trace.SpanProcessor +import io.opentelemetry.sdk.trace.export.BatchSpanProcessor +import io.opentelemetry.sdk.trace.export.BatchSpanProcessorBuilder +import io.opentelemetry.sdk.trace.export.SpanExporter +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.web.servlet.FilterRegistrationBean +import org.springframework.cloud.sleuth.autoconfig.otel.OtelProcessorProperties +import org.springframework.cloud.sleuth.autoconfig.otel.SpanProcessorProvider import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.core.Ordered +import java.util.concurrent.TimeUnit @Configuration class OtelWebConfiguration { @@ -43,4 +51,40 @@ class OtelWebConfiguration { registrationBean.addUrlPatterns("/*") return registrationBean } + + @Bean + @ConditionalOnMissingBean + fun otelBatchSpanProcessorProvider(otelProcessorProperties: OtelProcessorProperties): SpanProcessorProvider { + return object : SpanProcessorProvider { + override fun toSpanProcessor(spanExporter: SpanExporter): SpanProcessor { + val builder = BatchSpanProcessor.builder(spanExporter) + setBuilderProperties(otelProcessorProperties, builder) + return builder.build() + } + + fun setBuilderProperties( + otelProcessorProperties: OtelProcessorProperties, + builder: BatchSpanProcessorBuilder + ) { + if (otelProcessorProperties.batch.exporterTimeout != null) { + builder.setExporterTimeout( + otelProcessorProperties.batch.exporterTimeout, + TimeUnit.MILLISECONDS + ) + } + if (otelProcessorProperties.batch.maxExportBatchSize != null) { + builder.setMaxExportBatchSize(otelProcessorProperties.batch.maxExportBatchSize) + } + if (otelProcessorProperties.batch.maxQueueSize != null) { + builder.setMaxQueueSize(otelProcessorProperties.batch.maxQueueSize) + } + if (otelProcessorProperties.batch.scheduleDelay != null) { + builder.setScheduleDelay( + otelProcessorProperties.batch.scheduleDelay, + TimeUnit.MILLISECONDS + ) + } + } + } + } }