2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
21 #include "tracelibcpp.hpp"
24 using namespace tracelibcpp;
26 std::string ConfMaker::getEnv(const char* envName, std::string defVal)
28 const char *ev = getenv(envName);
31 return std::string(ev);
34 bool ConfMaker::isTracingEnabled()
36 std::string envValue = getEnv(TRACING_ENABLED_ENV, "false");
37 if (envValue == "1" || boost::iequals(envValue, "true"))
43 jaegertracing::Config ConfMaker::makeNopTraceConfig()
45 return jaegertracing::Config(true,
46 jaegertracing::samplers::Config("const", 0));
49 jaegertracing::samplers::Config ConfMaker::getSamplerConfig()
51 std::string samplerType = getEnv(JAEGER_SAMPLER_TYPE_ENV, "const");
52 // Use value 0.001 as default param, same way as jaeger does it
53 double param = atof(getEnv(JAEGER_SAMPLER_PARAM_ENV, "0.001").c_str());
54 return jaegertracing::samplers::Config(samplerType, param);
57 jaegertracing::reporters::Config ConfMaker::getReporterConfig()
59 std::string agentHostPort = getEnv(JAEGER_AGENT_ADDR_ENV, jaegertracing::reporters::Config::kDefaultLocalAgentHostPort);
61 if (agentHostPort.find(':') == std::string::npos)
62 agentHostPort += ":6831";
64 return jaegertracing::reporters::Config(
65 0, std::chrono::seconds(0), // use jaeger defaults
66 getLoggingLevel() == LOG_ALL, // log spans
71 LogLevel ConfMaker::getLoggingLevel()
73 std::string logLevel = getEnv(JAEGER_LOG_LEVEL_ENV, "error");
74 if (boost::iequals(logLevel, "all"))
76 else if (boost::iequals(logLevel, "error"))
82 std::unique_ptr<jaegertracing::logging::Logger> ConfMaker::getLogger()
84 switch (getLoggingLevel())
88 return jaegertracing::logging::consoleLogger();
91 return jaegertracing::logging::nullLogger();
95 jaegertracing::Config ConfMaker::getTraceConfig()
97 if (!isTracingEnabled())
98 return makeNopTraceConfig();
99 auto sampler = getSamplerConfig();
100 auto reporter = getReporterConfig();
101 return jaegertracing::Config(false, sampler, reporter);
104 std::shared_ptr<opentracing::Tracer> tracelibcpp::createTracer(std::string serviceName)
106 auto cm = ConfMaker(serviceName);
107 auto config = cm.getTraceConfig();
109 return jaegertracing::Tracer::make(serviceName, config, cm.getLogger());
110 } catch (std::exception& e)
112 if (cm.getLoggingLevel() != LOG_NONE)
113 std::cerr << "Cannot create tracer: " << e.what() << std::endl;
114 return jaegertracing::Tracer::make(serviceName, cm.makeNopTraceConfig());