-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathcomponent.cpp
62 lines (52 loc) · 1.98 KB
/
component.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <userver/tracing/component.hpp>
#include <userver/components/component.hpp>
#include <userver/logging/component.hpp>
#include <userver/tracing/tracer.hpp>
#include <userver/yaml_config/merge_schemas.hpp>
USERVER_NAMESPACE_BEGIN
namespace components {
namespace {
constexpr std::string_view kNativeTrace = "native";
}
Tracer::Tracer(const ComponentConfig& config, const ComponentContext& context) {
auto& logging_component = context.FindComponent<Logging>();
auto opentracing_logger = logging_component.GetLoggerOptional("opentracing");
auto service_name = config["service-name"].As<std::string>({});
const auto tracer_type = config["tracer"].As<std::string>(kNativeTrace);
if (tracer_type == kNativeTrace) {
if (service_name.empty() && opentracing_logger) {
throw std::runtime_error(
"Opentracing logger was set, but the `service-name` is empty. "
"Please provide a service name to use in OpenTracing"
);
}
if (opentracing_logger) {
LOG_INFO() << "Opentracing enabled.";
} else {
LOG_INFO() << "Opentracing logger is not registered";
}
tracing::Tracer::SetTracer(
tracing::MakeTracer(std::move(service_name), std::move(opentracing_logger), tracer_type)
);
} else {
throw std::runtime_error("Tracer type is not supported: " + tracer_type);
}
}
yaml_config::Schema Tracer::GetStaticConfigSchema() {
return yaml_config::MergeSchemas<RawComponentBase>(R"(
type: object
description: Component that initializes the request tracing facilities.
additionalProperties: false
properties:
service-name:
type: string
description: name of the service to write in traces
defaultDescription: ''
tracer:
type: string
description: type of the tracer to trace, currently supported only 'native'
defaultDescription: 'native'
)");
}
} // namespace components
USERVER_NAMESPACE_END