1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
5 #ifdef ENABLE_LOGS_PREVIEW
8 # include <unordered_map>
9 # include "opentelemetry/sdk/common/attribute_utils.h"
10 # include "opentelemetry/sdk/logs/recordable.h"
11 # include "opentelemetry/sdk/resource/resource.h"
12 # include "opentelemetry/version.h"
14 OPENTELEMETRY_BEGIN_NAMESPACE
21 * A default Recordable implemenation to be passed in log statements,
22 * matching the 10 fields of the Log Data Model.
23 * (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#log-and-event-record-definition)
26 class LogRecord final
: public Recordable
29 // Default values are set by the respective data structures' constructors for all fields,
30 // except the severity field, which must be set manually (an enum with no default value).
31 opentelemetry::logs::Severity severity_
= opentelemetry::logs::Severity::kInvalid
;
32 const opentelemetry::sdk::resource::Resource
*resource_
= nullptr;
33 common::AttributeMap attributes_map_
;
34 std::string body_
; // Currently a simple string, but should be changed to "Any" type
35 opentelemetry::trace::TraceId trace_id_
;
36 opentelemetry::trace::SpanId span_id_
;
37 opentelemetry::trace::TraceFlags trace_flags_
;
38 opentelemetry::common::SystemTimestamp timestamp_
; // uint64 nanoseconds since Unix epoch
41 /********** Setters for each field (overrides methods from the Recordable interface) ************/
44 * Set the severity for this log.
45 * @param severity the severity of the event
47 void SetSeverity(opentelemetry::logs::Severity severity
) noexcept override
53 * Set body field for this log.
54 * @param message the body to set
56 void SetBody(nostd::string_view message
) noexcept override
{ body_
= std::string(message
); }
59 * Set Resource of this log
60 * @param Resource the resource to set
62 void SetResource(const opentelemetry::sdk::resource::Resource
&resource
) noexcept override
64 resource_
= &resource
;
68 * Set an attribute of a log.
69 * @param name the name of the attribute
70 * @param value the attribute value
73 void SetAttribute(nostd::string_view key
,
74 const opentelemetry::common::AttributeValue
&value
) noexcept override
76 attributes_map_
.SetAttribute(key
, value
);
80 * Set trace id for this log.
81 * @param trace_id the trace id to set
83 void SetTraceId(opentelemetry::trace::TraceId trace_id
) noexcept override
89 * Set span id for this log.
90 * @param span_id the span id to set
92 virtual void SetSpanId(opentelemetry::trace::SpanId span_id
) noexcept override
98 * Inject a trace_flags for this log.
99 * @param trace_flags the span id to set
101 void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags
) noexcept override
103 trace_flags_
= trace_flags
;
107 * Set the timestamp for this log.
108 * @param timestamp the timestamp of the event
110 void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp
) noexcept override
112 timestamp_
= timestamp
;
115 /************************** Getters for each field ****************************/
118 * Get the severity for this log
119 * @return the severity for this log
121 opentelemetry::logs::Severity
GetSeverity() const noexcept
{ return severity_
; }
124 * Get the body of this log
125 * @return the body of this log
127 std::string
GetBody() const noexcept
{ return body_
; }
130 * Get the resource for this log
131 * @return the resource for this log
133 const opentelemetry::sdk::resource::Resource
&GetResource() const noexcept
135 if (nullptr == resource_
)
137 return sdk::resource::Resource::GetDefault();
143 * Get the attributes for this log
144 * @return the attributes for this log
146 const std::unordered_map
<std::string
, common::OwnedAttributeValue
> &GetAttributes() const noexcept
148 return attributes_map_
.GetAttributes();
152 * Get the trace id for this log
153 * @return the trace id for this log
155 opentelemetry::trace::TraceId
GetTraceId() const noexcept
{ return trace_id_
; }
158 * Get the span id for this log
159 * @return the span id for this log
161 opentelemetry::trace::SpanId
GetSpanId() const noexcept
{ return span_id_
; }
164 * Get the trace flags for this log
165 * @return the trace flags for this log
167 opentelemetry::trace::TraceFlags
GetTraceFlags() const noexcept
{ return trace_flags_
; }
170 * Get the timestamp for this log
171 * @return the timestamp for this log
173 opentelemetry::common::SystemTimestamp
GetTimestamp() const noexcept
{ return timestamp_
; }
176 * Set instrumentation_library for this log.
177 * @param instrumentation_library the instrumentation library to set
179 void SetInstrumentationLibrary(
180 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
181 &instrumentation_library
) noexcept
183 instrumentation_library_
= &instrumentation_library
;
187 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
188 *instrumentation_library_
= nullptr;
192 OPENTELEMETRY_END_NAMESPACE