001/**
002 * Copyright (C) 2006-2020 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.server.configuration;
017
018import static java.lang.System.getenv;
019import static java.util.Optional.ofNullable;
020
021import java.util.HashMap;
022import java.util.Map;
023
024import org.eclipse.microprofile.config.spi.ConfigSource;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028public class OpenTracingConfigSource implements ConfigSource {
029
030    private final static Logger LOGGER = LoggerFactory.getLogger(OpenTracingConfigSource.class);
031
032    private final Map<String, String> configuration = new HashMap<String, String>() {
033
034        {
035            int tracingRate;
036            try {
037                tracingRate = ofNullable(getenv("TRACING_SAMPLING_RATE")).map(Integer::parseInt).orElse(1);
038            } catch (final NumberFormatException e) {
039                LOGGER.warn("Can't parse value of environment property TRACING_SAMPLING_RATE", e);
040                tracingRate = 1;
041            }
042
043            final String isTracingOn = String.valueOf(Boolean.parseBoolean(getenv("TRACING_ON")) && tracingRate == 1);
044            put("geronimo.opentracing.filter.active", isTracingOn);
045            put("span.converter.zipkin.active", isTracingOn);
046            put("span.converter.zipkin.logger.active", isTracingOn);
047        }
048    };
049
050    @Override
051    public Map<String, String> getProperties() {
052        return configuration;
053    }
054
055    @Override
056    public int getOrdinal() {
057        return 2000;
058    }
059
060    @Override
061    public String getValue(final String propertyName) {
062        return configuration.get(propertyName);
063    }
064
065    @Override
066    public String getName() {
067        return "talend-opentracing";
068    }
069}