]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/src/logs/logger.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / src / logs / logger.cc
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifdef ENABLE_LOGS_PREVIEW
5 # include "opentelemetry/sdk/logs/logger.h"
6 # include "opentelemetry/sdk/logs/log_record.h"
7 # include "opentelemetry/sdk_config.h"
8 # include "opentelemetry/trace/provider.h"
9
10 OPENTELEMETRY_BEGIN_NAMESPACE
11 namespace sdk
12 {
13 namespace logs
14 {
15 namespace trace_api = opentelemetry::trace;
16 namespace nostd = opentelemetry::nostd;
17 namespace common = opentelemetry::common;
18
19 Logger::Logger(nostd::string_view name,
20 std::shared_ptr<LoggerContext> context,
21 std::unique_ptr<instrumentationlibrary::InstrumentationLibrary>
22 instrumentation_library) noexcept
23 : logger_name_(std::string(name)),
24 instrumentation_library_(std::move(instrumentation_library)),
25 context_(context)
26 {}
27
28 const nostd::string_view Logger::GetName() noexcept
29 {
30 return logger_name_;
31 }
32
33 /**
34 * Create and populate recordable with the log event's fields passed in.
35 * The timestamp, severity, traceid, spanid, and traceflags, are injected
36 * if the user does not specify them.
37 */
38 void Logger::Log(opentelemetry::logs::Severity severity,
39 nostd::string_view body,
40 const common::KeyValueIterable &attributes,
41 trace_api::TraceId trace_id,
42 trace_api::SpanId span_id,
43 trace_api::TraceFlags trace_flags,
44 common::SystemTimestamp timestamp) noexcept
45 {
46 // If this logger does not have a processor, no need to create a log record
47 if (!context_)
48 {
49 return;
50 }
51 auto &processor = context_->GetProcessor();
52
53 // TODO: Sampler (should include check for minSeverity)
54
55 auto recordable = processor.MakeRecordable();
56 if (recordable == nullptr)
57 {
58 OTEL_INTERNAL_LOG_ERROR("[LOGGER] Recordable creation failed");
59 return;
60 }
61
62 // Populate recordable fields
63 recordable->SetTimestamp(timestamp);
64 recordable->SetSeverity(severity);
65 recordable->SetBody(body);
66 recordable->SetInstrumentationLibrary(GetInstrumentationLibrary());
67
68 recordable->SetResource(context_->GetResource());
69
70 attributes.ForEachKeyValue([&](nostd::string_view key, common::AttributeValue value) noexcept {
71 recordable->SetAttribute(key, value);
72 return true;
73 });
74
75 // Inject trace_id/span_id/trace_flags if none is set by user
76 auto provider = trace_api::Provider::GetTracerProvider();
77 auto tracer = provider->GetTracer(logger_name_);
78 auto span_context = tracer->GetCurrentSpan()->GetContext();
79
80 // Leave these fields in the recordable empty if neither the passed in values
81 // nor the context values are valid (e.g. the application is not using traces)
82
83 // TraceId
84 if (trace_id.IsValid())
85 {
86 recordable->SetTraceId(trace_id);
87 }
88 else if (span_context.trace_id().IsValid())
89 {
90 recordable->SetTraceId(span_context.trace_id());
91 }
92
93 // SpanId
94 if (span_id.IsValid())
95 {
96 recordable->SetSpanId(span_id);
97 }
98 else if (span_context.span_id().IsValid())
99 {
100 recordable->SetSpanId(span_context.span_id());
101 }
102
103 // TraceFlags
104 if (trace_flags.IsSampled())
105 {
106 recordable->SetTraceFlags(trace_flags);
107 }
108 else if (span_context.trace_flags().IsSampled())
109 {
110 recordable->SetTraceFlags(span_context.trace_flags());
111 }
112
113 // Send the log record to the processor
114 processor.OnReceive(std::move(recordable));
115 }
116
117 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &
118 Logger::GetInstrumentationLibrary() const noexcept
119 {
120 return *instrumentation_library_;
121 }
122
123 } // namespace logs
124 } // namespace sdk
125 OPENTELEMETRY_END_NAMESPACE
126 #endif