Add new license claim
[ric-plt/tracelibcpp.git] / src / tracelib.cpp
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property.
3  * Copyright (c) 2018-2019 Nokia.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18  * platform project (RICP).
19  */
20
21 #include "tracelibcpp.hpp"
22 #include "config.hpp"
23
24 using namespace tracelibcpp;
25
26 std::string ConfMaker::getEnv(const char* envName, std::string defVal)
27 {
28     const char *ev = getenv(envName);
29     if (!ev)
30         return defVal;
31     return std::string(ev);
32 }
33
34 bool ConfMaker::isTracingEnabled()
35 {
36     std::string envValue = getEnv(TRACING_ENABLED_ENV, "false");
37     if (envValue == "1" || boost::iequals(envValue, "true"))
38         return true;
39     else
40         return false;
41 }
42
43 jaegertracing::Config ConfMaker::makeNopTraceConfig()
44 {
45     return jaegertracing::Config(true,
46             jaegertracing::samplers::Config("const", 0));
47 }
48
49 jaegertracing::samplers::Config ConfMaker::getSamplerConfig()
50 {
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);
55 }
56
57 jaegertracing::reporters::Config ConfMaker::getReporterConfig()
58 {
59     std::string agentHostPort = getEnv(JAEGER_AGENT_ADDR_ENV, jaegertracing::reporters::Config::kDefaultLocalAgentHostPort);
60
61     if (agentHostPort.find(':') == std::string::npos)
62         agentHostPort += ":6831";
63
64     return jaegertracing::reporters::Config(
65         0, std::chrono::seconds(0),     // use jaeger defaults
66         getLoggingLevel() == LOG_ALL,   // log spans
67         agentHostPort
68     );
69 }
70
71 LogLevel ConfMaker::getLoggingLevel()
72 {
73     std::string logLevel = getEnv(JAEGER_LOG_LEVEL_ENV, "error");
74     if (boost::iequals(logLevel, "all"))
75         return LOG_ALL;
76     else if (boost::iequals(logLevel, "error"))
77         return LOG_ERR;
78     else
79         return LOG_NONE;
80 }
81
82 std::unique_ptr<jaegertracing::logging::Logger> ConfMaker::getLogger()
83 {
84     switch (getLoggingLevel())
85     {
86         case LOG_ALL:
87         case LOG_ERR:
88             return jaegertracing::logging::consoleLogger();
89             break;
90         default:
91             return jaegertracing::logging::nullLogger();
92     }
93 }
94
95 jaegertracing::Config ConfMaker::getTraceConfig()
96 {
97     if (!isTracingEnabled())
98         return makeNopTraceConfig();
99     auto sampler = getSamplerConfig();
100     auto reporter = getReporterConfig();
101     return jaegertracing::Config(false, sampler, reporter);
102 }
103
104 std::shared_ptr<opentracing::Tracer> tracelibcpp::createTracer(std::string serviceName)
105 {
106     auto cm = ConfMaker(serviceName);
107     auto config = cm.getTraceConfig();
108     try {
109         return jaegertracing::Tracer::make(serviceName, config, cm.getLogger());
110     } catch (std::exception& e)
111     {
112         if (cm.getLoggingLevel() != LOG_NONE)
113             std::cerr << "Cannot create tracer: " << e.what() << std::endl;
114         return jaegertracing::Tracer::make(serviceName, cm.makeNopTraceConfig());
115     }
116 }
117
118