]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/opentelemetry-cpp/exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_recordable.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / exporters / elasticsearch / include / opentelemetry / exporters / elasticsearch / es_log_recordable.h
CommitLineData
1e59de90
TL
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 <sstream>
9# include <type_traits>
10# include <unordered_map>
11
12# include "nlohmann/json.hpp"
13# include "opentelemetry/sdk/common/attribute_utils.h"
14# include "opentelemetry/sdk/logs/recordable.h"
15# include "opentelemetry/version.h"
16
17OPENTELEMETRY_BEGIN_NAMESPACE
18namespace exporter
19{
20namespace logs
21{
22
23/**
24 * An Elasticsearch Recordable implemenation that stores the 10 fields of the Log Data Model inside
25 * a JSON object,
26 */
27class ElasticSearchRecordable final : public sdk::logs::Recordable
28{
29private:
30 // Define a JSON object that will be populated with the log data
31 nlohmann::json json_;
32
33 /**
34 * A helper method that writes a key/value pair under a specified name, the two names used here
35 * being "attributes" and "resources"
36 */
37 void WriteKeyValue(nostd::string_view key,
38 const opentelemetry::common::AttributeValue &value,
39 std::string name)
40 {
41 switch (value.index())
42 {
43 case common::AttributeType::kTypeBool:
44 json_[name][key.data()] = opentelemetry::nostd::get<bool>(value) ? true : false;
45 return;
46 case common::AttributeType::kTypeInt:
47 json_[name][key.data()] = opentelemetry::nostd::get<int>(value);
48 return;
49 case common::AttributeType::kTypeInt64:
50 json_[name][key.data()] = opentelemetry::nostd::get<int64_t>(value);
51 return;
52 case common::AttributeType::kTypeUInt:
53 json_[name][key.data()] = opentelemetry::nostd::get<unsigned int>(value);
54 return;
55 case common::AttributeType::kTypeUInt64:
56 json_[name][key.data()] = opentelemetry::nostd::get<uint64_t>(value);
57 return;
58 case common::AttributeType::kTypeDouble:
59 json_[name][key.data()] = opentelemetry::nostd::get<double>(value);
60 return;
61 case common::AttributeType::kTypeCString:
62 json_[name][key.data()] = opentelemetry::nostd::get<const char *>(value);
63 return;
64 case common::AttributeType::kTypeString:
65 json_[name][key.data()] =
66 opentelemetry::nostd::get<opentelemetry::nostd::string_view>(value).data();
67 return;
68 default:
69 return;
70 }
71 }
72
73 void WriteKeyValue(nostd::string_view key,
74 const opentelemetry::sdk::common::OwnedAttributeValue &value,
75 std::string name)
76 {
77 namespace common = opentelemetry::sdk::common;
78 switch (value.index())
79 {
80 case common::kTypeBool:
81 json_[name][key.data()] = opentelemetry::nostd::get<bool>(value) ? true : false;
82 return;
83 case common::kTypeInt:
84 json_[name][key.data()] = opentelemetry::nostd::get<int>(value);
85 return;
86 case common::kTypeInt64:
87 json_[name][key.data()] = opentelemetry::nostd::get<int64_t>(value);
88 return;
89 case common::kTypeUInt:
90 json_[name][key.data()] = opentelemetry::nostd::get<unsigned int>(value);
91 return;
92 case common::kTypeUInt64:
93 json_[name][key.data()] = opentelemetry::nostd::get<uint64_t>(value);
94 return;
95 case common::kTypeDouble:
96 json_[name][key.data()] = opentelemetry::nostd::get<double>(value);
97 return;
98 case common::kTypeString:
99 json_[name][key.data()] = opentelemetry::nostd::get<std::string>(value).data();
100 return;
101 default:
102 return;
103 }
104 }
105
106public:
107 /**
108 * Set the severity for this log.
109 * @param severity the severity of the event
110 */
111 void SetSeverity(opentelemetry::logs::Severity severity) noexcept override
112 {
113 // Convert the severity enum to a string
114 std::uint32_t severity_index = static_cast<std::uint32_t>(severity);
115 if (severity_index >= std::extent<decltype(opentelemetry::logs::SeverityNumToText)>::value)
116 {
117 std::stringstream sout;
118 sout << "Invalid severity(" << severity_index << ")";
119 json_["severity"] = sout.str();
120 }
121 else
122 {
123 json_["severity"] = opentelemetry::logs::SeverityNumToText[severity_index];
124 }
125 }
126
127 /**
128 * Set body field for this log.
129 * @param message the body to set
130 */
131 void SetBody(nostd::string_view message) noexcept override { json_["body"] = message.data(); }
132
133 /**
134 * Set Resource of this log
135 * @param Resource the resource to set
136 */
137 void SetResource(const opentelemetry::sdk::resource::Resource &resource) noexcept override
138 {
139 for (auto &kv : resource.GetAttributes())
140 {
141 WriteKeyValue(kv.first, kv.second, "resource");
142 }
143 }
144
145 /**
146 * Set an attribute of a log.
147 * @param key the key of the attribute
148 * @param value the attribute value
149 */
150 void SetAttribute(nostd::string_view key,
151 const opentelemetry::common::AttributeValue &value) noexcept override
152 {
153 WriteKeyValue(key, value, "attributes");
154 }
155
156 /**
157 * Set trace id for this log.
158 * @param trace_id the trace id to set
159 */
160 void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept override
161 {
162 char trace_buf[32];
163 trace_id.ToLowerBase16(trace_buf);
164 json_["traceid"] = std::string(trace_buf, sizeof(trace_buf));
165 }
166
167 /**
168 * Set span id for this log.
169 * @param span_id the span id to set
170 */
171 virtual void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept override
172 {
173 char span_buf[16];
174 span_id.ToLowerBase16(span_buf);
175 json_["spanid"] = std::string(span_buf, sizeof(span_buf));
176 }
177
178 /**
179 * Inject a trace_flags for this log.
180 * @param trace_flags the span id to set
181 */
182 void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override
183 {
184 char flag_buf[2];
185 trace_flags.ToLowerBase16(flag_buf);
186 json_["traceflags"] = std::string(flag_buf, sizeof(flag_buf));
187 }
188
189 /**
190 * Set the timestamp for this log.
191 * @param timestamp the timestamp of the event
192 */
193 void SetTimestamp(common::SystemTimestamp timestamp) noexcept override
194 {
195 json_["timestamp"] = timestamp.time_since_epoch().count();
196 }
197
198 /**
199 * Returns a JSON object contain the log information
200 */
201 nlohmann::json GetJSON() noexcept { return json_; }
202
203 /**
204 * Set instrumentation_library for this log.
205 * @param instrumentation_library the instrumentation library to set
206 */
207 void SetInstrumentationLibrary(
208 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
209 &instrumentation_library) noexcept
210 {
211 json_["name"] = instrumentation_library.GetName();
212 instrumentation_library_ = &instrumentation_library;
213 }
214
215 /** Returns the associated instruementation library */
216 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary &
217 GetInstrumentationLibrary() const noexcept
218 {
219 return *instrumentation_library_;
220 }
221
222private:
223 const opentelemetry::sdk::instrumentationlibrary::InstrumentationLibrary
224 *instrumentation_library_ = nullptr;
225};
226} // namespace logs
227} // namespace exporter
228OPENTELEMETRY_END_NAMESPACE
229#endif