|
| 1 | +/* |
| 2 | + * Copyright 2013-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.sleuth.autoconfig.otel.wavefront; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | + |
| 21 | +import com.wavefront.spring.autoconfigure.WavefrontAutoConfiguration; |
| 22 | +import com.wavefront.spring.autoconfigure.WavefrontSleuthSpanHandler; |
| 23 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 24 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 25 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 26 | + |
| 27 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 31 | +import org.springframework.cloud.sleuth.TraceContext; |
| 32 | +import org.springframework.cloud.sleuth.otel.bridge.OtelFinishedSpan; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | + |
| 36 | +/** |
| 37 | + * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration} |
| 38 | + * Auto-configuration enables reporting to Wavefront. |
| 39 | + * |
| 40 | + * @author Marcin Grzejszczak |
| 41 | + * @since 1.0.0 |
| 42 | + */ |
| 43 | +@Configuration(proxyBeanMethods = false) |
| 44 | +@ConditionalOnProperty(value = { "spring.sleuth.enabled", "spring.sleuth.wavefront.enabled" }, matchIfMissing = true) |
| 45 | +@ConditionalOnClass(WavefrontSleuthSpanHandler.class) |
| 46 | +@AutoConfigureAfter(WavefrontAutoConfiguration.class) |
| 47 | +public class WavefrontOtelAutoConfiguration { |
| 48 | + |
| 49 | + @Bean |
| 50 | + @ConditionalOnBean(WavefrontSleuthSpanHandler.class) |
| 51 | + SpanExporter wavefrontSpanExporter(WavefrontSleuthSpanHandler spanHandler) { |
| 52 | + return new SpanExporter() { |
| 53 | + @Override |
| 54 | + public CompletableResultCode export(Collection<SpanData> spans) { |
| 55 | + spans.forEach(spanData -> spanHandler.end(traceContext(spanData), OtelFinishedSpan.fromOtel(spanData))); |
| 56 | + return CompletableResultCode.ofSuccess(); |
| 57 | + } |
| 58 | + |
| 59 | + private TraceContext traceContext(SpanData spanData) { |
| 60 | + return new TraceContext() { |
| 61 | + @Override |
| 62 | + public String traceId() { |
| 63 | + return spanData.getTraceId(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String parentId() { |
| 68 | + return spanData.getParentSpanId(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String spanId() { |
| 73 | + return spanData.getSpanId(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Boolean sampled() { |
| 78 | + return spanData.getSpanContext().isSampled(); |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public CompletableResultCode flush() { |
| 85 | + return CompletableResultCode.ofSuccess(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public CompletableResultCode shutdown() { |
| 90 | + spanHandler.close(); |
| 91 | + return CompletableResultCode.ofSuccess(); |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments