1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
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"
10 OPENTELEMETRY_BEGIN_NAMESPACE
15 namespace trace_api
= opentelemetry::trace
;
16 namespace nostd
= opentelemetry::nostd
;
17 namespace common
= opentelemetry::common
;
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
)),
28 const nostd::string_view
Logger::GetName() noexcept
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.
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
46 // If this logger does not have a processor, no need to create a log record
51 auto &processor
= context_
->GetProcessor();
53 // TODO: Sampler (should include check for minSeverity)
55 auto recordable
= processor
.MakeRecordable();
56 if (recordable
== nullptr)
58 OTEL_INTERNAL_LOG_ERROR("[LOGGER] Recordable creation failed");
62 // Populate recordable fields
63 recordable
->SetTimestamp(timestamp
);
64 recordable
->SetSeverity(severity
);
65 recordable
->SetBody(body
);
66 recordable
->SetInstrumentationLibrary(GetInstrumentationLibrary());
68 recordable
->SetResource(context_
->GetResource());
70 attributes
.ForEachKeyValue([&](nostd::string_view key
, common::AttributeValue value
) noexcept
{
71 recordable
->SetAttribute(key
, value
);
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();
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)
84 if (trace_id
.IsValid())
86 recordable
->SetTraceId(trace_id
);
88 else if (span_context
.trace_id().IsValid())
90 recordable
->SetTraceId(span_context
.trace_id());
94 if (span_id
.IsValid())
96 recordable
->SetSpanId(span_id
);
98 else if (span_context
.span_id().IsValid())
100 recordable
->SetSpanId(span_context
.span_id());
104 if (trace_flags
.IsSampled())
106 recordable
->SetTraceFlags(trace_flags
);
108 else if (span_context
.trace_flags().IsSampled())
110 recordable
->SetTraceFlags(span_context
.trace_flags());
113 // Send the log record to the processor
114 processor
.OnReceive(std::move(recordable
));
117 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
&
118 Logger::GetInstrumentationLibrary() const noexcept
120 return *instrumentation_library_
;
125 OPENTELEMETRY_END_NAMESPACE