]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/include/opentelemetry/sdk/logs/log_record.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / include / opentelemetry / sdk / logs / log_record.h
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #pragma once
5 #ifdef ENABLE_LOGS_PREVIEW
6
7 # include <map>
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"
13
14 OPENTELEMETRY_BEGIN_NAMESPACE
15 namespace sdk
16 {
17 namespace logs
18 {
19
20 /**
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)
24 *
25 */
26 class LogRecord final : public Recordable
27 {
28 private:
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
39
40 public:
41 /********** Setters for each field (overrides methods from the Recordable interface) ************/
42
43 /**
44 * Set the severity for this log.
45 * @param severity the severity of the event
46 */
47 void SetSeverity(opentelemetry::logs::Severity severity) noexcept override
48 {
49 severity_ = severity;
50 }
51
52 /**
53 * Set body field for this log.
54 * @param message the body to set
55 */
56 void SetBody(nostd::string_view message) noexcept override { body_ = std::string(message); }
57
58 /**
59 * Set Resource of this log
60 * @param Resource the resource to set
61 */
62 void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override
63 {
64 resource_ = &resource;
65 }
66
67 /**
68 * Set an attribute of a log.
69 * @param name the name of the attribute
70 * @param value the attribute value
71 */
72
73 void SetAttribute(nostd::string_view key,
74 const opentelemetry::common::AttributeValue &value) noexcept override
75 {
76 attributes_map_.SetAttribute(key, value);
77 }
78
79 /**
80 * Set trace id for this log.
81 * @param trace_id the trace id to set
82 */
83 void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept override
84 {
85 trace_id_ = trace_id;
86 }
87
88 /**
89 * Set span id for this log.
90 * @param span_id the span id to set
91 */
92 virtual void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept override
93 {
94 span_id_ = span_id;
95 }
96
97 /**
98 * Inject a trace_flags for this log.
99 * @param trace_flags the span id to set
100 */
101 void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override
102 {
103 trace_flags_ = trace_flags;
104 }
105
106 /**
107 * Set the timestamp for this log.
108 * @param timestamp the timestamp of the event
109 */
110 void SetTimestamp(opentelemetry::common::SystemTimestamp timestamp) noexcept override
111 {
112 timestamp_ = timestamp;
113 }
114
115 /************************** Getters for each field ****************************/
116
117 /**
118 * Get the severity for this log
119 * @return the severity for this log
120 */
121 opentelemetry::logs::Severity GetSeverity() const noexcept { return severity_; }
122
123 /**
124 * Get the body of this log
125 * @return the body of this log
126 */
127 std::string GetBody() const noexcept { return body_; }
128
129 /**
130 * Get the resource for this log
131 * @return the resource for this log
132 */
133 const opentelemetry::sdk::resource::Resource &GetResource() const noexcept
134 {
135 if (nullptr == resource_)
136 {
137 return sdk::resource::Resource::GetDefault();
138 }
139 return *resource_;
140 }
141
142 /**
143 * Get the attributes for this log
144 * @return the attributes for this log
145 */
146 const std::unordered_map<std::string, common::OwnedAttributeValue> &GetAttributes() const noexcept
147 {
148 return attributes_map_.GetAttributes();
149 }
150
151 /**
152 * Get the trace id for this log
153 * @return the trace id for this log
154 */
155 opentelemetry::trace::TraceId GetTraceId() const noexcept { return trace_id_; }
156
157 /**
158 * Get the span id for this log
159 * @return the span id for this log
160 */
161 opentelemetry::trace::SpanId GetSpanId() const noexcept { return span_id_; }
162
163 /**
164 * Get the trace flags for this log
165 * @return the trace flags for this log
166 */
167 opentelemetry::trace::TraceFlags GetTraceFlags() const noexcept { return trace_flags_; }
168
169 /**
170 * Get the timestamp for this log
171 * @return the timestamp for this log
172 */
173 opentelemetry::common::SystemTimestamp GetTimestamp() const noexcept { return timestamp_; }
174
175 /**
176 * Set instrumentation_library for this log.
177 * @param instrumentation_library the instrumentation library to set
178 */
179 void SetInstrumentationLibrary(
180 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
181 &instrumentation_library) noexcept
182 {
183 instrumentation_library_ = &instrumentation_library;
184 }
185
186 private:
187 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
188 *instrumentation_library_ = nullptr;
189 };
190 } // namespace logs
191 } // namespace sdk
192 OPENTELEMETRY_END_NAMESPACE
193 #endif