|
| 1 | +/* |
| 2 | + * Copyright Splunk Inc. |
| 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 | + * http://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 com.splunk.opentelemetry.appd; |
| 18 | + |
| 19 | +import static io.opentelemetry.sdk.autoconfigure.AdditionalPropertiesUtil.getAdditionalPropertyOrDefault; |
| 20 | + |
| 21 | +import com.google.auto.service.AutoService; |
| 22 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizer; |
| 23 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfigurationCustomizerProvider; |
| 24 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel; |
| 25 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.PropagatorModel; |
| 26 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.SpanProcessorModel; |
| 27 | +import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.TracerProviderModel; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +@AutoService(DeclarativeConfigurationCustomizerProvider.class) |
| 31 | +public final class AppdBonusConfigurationCustomizerProvider |
| 32 | + implements DeclarativeConfigurationCustomizerProvider { |
| 33 | + |
| 34 | + private static final String CONFIG_CISCO_CTX_ENABLED = "cisco.ctx.enabled"; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void customize(DeclarativeConfigurationCustomizer autoConfiguration) { |
| 38 | + autoConfiguration.addModelCustomizer( |
| 39 | + model -> { |
| 40 | + if (model.getInstrumentationDevelopment() == null |
| 41 | + || model.getInstrumentationDevelopment().getJava() == null) { |
| 42 | + return model; |
| 43 | + } |
| 44 | + Map<String, Object> properties = |
| 45 | + model.getInstrumentationDevelopment().getJava().getAdditionalProperties(); |
| 46 | + |
| 47 | + if (isFeatureEnabled(model, properties)) { |
| 48 | + if (maybeAddAppdBonusPropagator(model)) { |
| 49 | + // Appd propagator has been added so add also a corresponding Appd span processor |
| 50 | + SpanProcessorModel appdSpanProcessorModel = |
| 51 | + new SpanProcessorModel() |
| 52 | + .withAdditionalProperty( |
| 53 | + AppdBonusSpanProcessorComponentProvider.NAME, null); |
| 54 | + if (model.getTracerProvider() == null) { |
| 55 | + model.withTracerProvider(new TracerProviderModel()); |
| 56 | + } |
| 57 | + model.getTracerProvider().getProcessors().add(appdSpanProcessorModel); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return model; |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + private static boolean isFeatureEnabled( |
| 66 | + OpenTelemetryConfigurationModel model, Map<String, Object> properties) { |
| 67 | + return (getAdditionalPropertyOrDefault(properties, CONFIG_CISCO_CTX_ENABLED, false)); |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean canAddPropagator(String compositeList) { |
| 71 | + for (String propagatorNames : compositeList.split(",")) { |
| 72 | + if (propagatorNames.trim().equals("none")) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + private static boolean maybeAddAppdBonusPropagator(OpenTelemetryConfigurationModel model) { |
| 80 | + if (model.getPropagator() == null) { |
| 81 | + model.withPropagator(new PropagatorModel()); |
| 82 | + } |
| 83 | + |
| 84 | + String compositeList = model.getPropagator().getCompositeList(); |
| 85 | + if (compositeList == null) { |
| 86 | + compositeList = ""; |
| 87 | + } else if (!canAddPropagator(compositeList)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Possible duplicates are handled by the upstream |
| 92 | + compositeList = |
| 93 | + compositeList.isEmpty() |
| 94 | + ? AppdBonusPropagator.NAME |
| 95 | + : AppdBonusPropagator.NAME + "," + compositeList; |
| 96 | + |
| 97 | + model.getPropagator().withCompositeList(compositeList); |
| 98 | + |
| 99 | + return true; |
| 100 | + } |
| 101 | +} |
0 commit comments