|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package co.elastic.otel; |
| 20 | + |
| 21 | +import com.google.auto.service.AutoService; |
| 22 | +import io.opentelemetry.api.common.AttributeKey; |
| 23 | +import io.opentelemetry.api.common.Attributes; |
| 24 | +import io.opentelemetry.api.trace.SpanBuilder; |
| 25 | +import io.opentelemetry.api.trace.SpanContext; |
| 26 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; |
| 27 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.function.BiConsumer; |
| 31 | +import java.util.logging.Level; |
| 32 | +import java.util.logging.Logger; |
| 33 | + |
| 34 | +@AutoService(AutoConfigurationCustomizerProvider.class) |
| 35 | +public class InferredSpansBackwardsCompatibilityConfig |
| 36 | + implements AutoConfigurationCustomizerProvider { |
| 37 | + |
| 38 | + private static final Logger log = |
| 39 | + Logger.getLogger(InferredSpansBackwardsCompatibilityConfig.class.getName()); |
| 40 | + |
| 41 | + private static final Map<String, String> CONFIG_MAPPING = new HashMap<>(); |
| 42 | + |
| 43 | + static { |
| 44 | + CONFIG_MAPPING.put("elastic.otel.inferred.spans.enabled", "otel.inferred.spans.enabled"); |
| 45 | + CONFIG_MAPPING.put( |
| 46 | + "elastic.otel.inferred.spans.logging.enabled", "otel.inferred.spans.logging.enabled"); |
| 47 | + CONFIG_MAPPING.put( |
| 48 | + "elastic.otel.inferred.spans.backup.diagnostic.files", |
| 49 | + "otel.inferred.spans.backup.diagnostic.files"); |
| 50 | + CONFIG_MAPPING.put("elastic.otel.inferred.spans.safe.mode", "otel.inferred.spans.safe.mode"); |
| 51 | + CONFIG_MAPPING.put( |
| 52 | + "elastic.otel.inferred.spans.post.processing.enabled", |
| 53 | + "otel.inferred.spans.post.processing.enabled"); |
| 54 | + CONFIG_MAPPING.put( |
| 55 | + "elastic.otel.inferred.spans.sampling.interval", "otel.inferred.spans.sampling.interval"); |
| 56 | + CONFIG_MAPPING.put( |
| 57 | + "elastic.otel.inferred.spans.min.duration", "otel.inferred.spans.min.duration"); |
| 58 | + CONFIG_MAPPING.put( |
| 59 | + "elastic.otel.inferred.spans.included.classes", "otel.inferred.spans.included.classes"); |
| 60 | + CONFIG_MAPPING.put( |
| 61 | + "elastic.otel.inferred.spans.excluded.classes", "otel.inferred.spans.excluded.classes"); |
| 62 | + CONFIG_MAPPING.put("elastic.otel.inferred.spans.interval", "otel.inferred.spans.interval"); |
| 63 | + CONFIG_MAPPING.put("elastic.otel.inferred.spans.duration", "otel.inferred.spans.duration"); |
| 64 | + CONFIG_MAPPING.put( |
| 65 | + "elastic.otel.inferred.spans.lib.directory", "otel.inferred.spans.lib.directory"); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void customize(AutoConfigurationCustomizer config) { |
| 70 | + config.addPropertiesCustomizer( |
| 71 | + props -> { |
| 72 | + Map<String, String> overrides = new HashMap<>(); |
| 73 | + for (String oldKey : CONFIG_MAPPING.keySet()) { |
| 74 | + String value = props.getString(oldKey); |
| 75 | + if (value != null) { |
| 76 | + String newKey = CONFIG_MAPPING.get(oldKey); |
| 77 | + if (props.getString(newKey) == null) { // new value has not been configured |
| 78 | + log.log( |
| 79 | + Level.WARNING, |
| 80 | + "The configuration property {0} is deprecated, use {1} instead", |
| 81 | + new Object[] {oldKey, newKey}); |
| 82 | + overrides.put(newKey, value); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + String userDefinedHandler = |
| 88 | + props.getString("otel.inferred.spans.parent.override.handler"); |
| 89 | + if (userDefinedHandler == null || userDefinedHandler.isEmpty()) { |
| 90 | + overrides.put( |
| 91 | + "otel.inferred.spans.parent.override.handler", |
| 92 | + BackwardsCompatibilitySpanLinkHandler.class.getName()); |
| 93 | + } |
| 94 | + |
| 95 | + return overrides; |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + public static class BackwardsCompatibilitySpanLinkHandler |
| 100 | + implements BiConsumer<SpanBuilder, SpanContext> { |
| 101 | + |
| 102 | + private static final Attributes LINK_ATTRIBUTES = |
| 103 | + Attributes.of( |
| 104 | + AttributeKey.booleanKey("is_child"), true, |
| 105 | + AttributeKey.booleanKey("elastic.is_child"), true); |
| 106 | + |
| 107 | + @Override |
| 108 | + public void accept(SpanBuilder spanBuilder, SpanContext spanContext) { |
| 109 | + spanBuilder.addLink(spanContext, LINK_ATTRIBUTES); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments