-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47cfbfc
commit 79e6591
Showing
5 changed files
with
211 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
exporters/elasticsearch/include/opentelemetry/exporters/elasticsearch/es_log_recordable.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <map> | ||
#include <unordered_map> | ||
#include "nlohmann/json.hpp" | ||
#include "opentelemetry/sdk/common/attribute_utils.h" // same as traces/attribute_utils | ||
#include "opentelemetry/sdk/logs/recordable.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace exporter | ||
{ | ||
namespace logs | ||
{ | ||
|
||
/** | ||
* An Elasticsearch Recordable implemenation that stores the 10 fields of the Log Data Model inside | ||
* a JSON object, | ||
*/ | ||
class ElasticSearchRecordable final : public sdk::logs::Recordable | ||
{ | ||
private: | ||
// Define a JSON object that will be populated with the log data | ||
nlohmann::json json_; | ||
|
||
/** | ||
* Converts a common::AttributeValue into a string, which is used for converting a | ||
* common::AttributeValue into a string | ||
*/ | ||
const std::string ValueToString(const common::AttributeValue &value) | ||
{ | ||
switch (value.index()) | ||
{ | ||
case common::AttributeType::TYPE_BOOL: | ||
return (opentelemetry::nostd::get<bool>(value) ? "true" : "false"); | ||
break; | ||
case common::AttributeType::TYPE_INT: | ||
return std::to_string(opentelemetry::nostd::get<int>(value)); | ||
break; | ||
case common::AttributeType::TYPE_INT64: | ||
return std::to_string(opentelemetry::nostd::get<int64_t>(value)); | ||
break; | ||
case common::AttributeType::TYPE_UINT: | ||
return std::to_string(opentelemetry::nostd::get<unsigned int>(value)); | ||
break; | ||
case common::AttributeType::TYPE_UINT64: | ||
return std::to_string(opentelemetry::nostd::get<uint64_t>(value)); | ||
break; | ||
case common::AttributeType::TYPE_DOUBLE: | ||
return std::to_string(opentelemetry::nostd::get<double>(value)); | ||
break; | ||
case common::AttributeType::TYPE_STRING: | ||
case common::AttributeType::TYPE_CSTRING: | ||
return opentelemetry::nostd::get<opentelemetry::nostd::string_view>(value).data(); | ||
break; | ||
default: | ||
return "Invalid type"; | ||
break; | ||
} | ||
} | ||
|
||
public: | ||
/** | ||
* Set the severity for this log. | ||
* @param severity the severity of the event | ||
*/ | ||
void SetSeverity(opentelemetry::logs::Severity severity) noexcept override | ||
{ | ||
// Convert the severity enum to a string | ||
static opentelemetry::nostd::string_view severityStringMap_[25] = { | ||
"kInvalid", "kTrace", "kTrace2", "kTrace3", "kTrace4", "kDebug", "kDebug2", | ||
"kDebug3", "kDebug4", "kInfo", "kInfo2", "kInfo3", "kInfo4", "kWarn", | ||
"kWarn2", "kWarn3", "kWarn4", "kError", "kError2", "kError3", "kError4", | ||
"kFatal", "kFatal2", "kFatal3", "kFatal4"}; | ||
json_["severity"] = severityStringMap_[static_cast<int>(severity)]; | ||
} | ||
|
||
/** | ||
* Set name for this log | ||
* @param name the name to set | ||
*/ | ||
void SetName(nostd::string_view name) noexcept override { json_["name"] = name.data(); } | ||
|
||
/** | ||
* Set body field for this log. | ||
* @param message the body to set | ||
*/ | ||
void SetBody(nostd::string_view message) noexcept override { json_["body"] = message.data(); } | ||
|
||
/** | ||
* Set a resource for this log. | ||
* @param name the name of the resource | ||
* @param value the resource value | ||
*/ | ||
void SetResource(nostd::string_view key, | ||
const opentelemetry::common::AttributeValue &value) noexcept override | ||
{ | ||
json_["resource"][key.data()] = ValueToString(value); | ||
} | ||
|
||
/** | ||
* Set an attribute of a log. | ||
* @param key the key of the attribute | ||
* @param value the attribute value | ||
*/ | ||
void SetAttribute(nostd::string_view key, | ||
const opentelemetry::common::AttributeValue &value) noexcept override | ||
{ | ||
json_["attributes"][key.data()] = ValueToString(value); | ||
} | ||
|
||
/** | ||
* Set trace id for this log. | ||
* @param trace_id the trace id to set | ||
*/ | ||
void SetTraceId(opentelemetry::trace::TraceId trace_id) noexcept override | ||
{ | ||
char trace_buf[32]; | ||
trace_id.ToLowerBase16(trace_buf); | ||
json_["traceid"] = std::string(trace_buf, sizeof(trace_buf)); | ||
} | ||
|
||
/** | ||
* Set span id for this log. | ||
* @param span_id the span id to set | ||
*/ | ||
virtual void SetSpanId(opentelemetry::trace::SpanId span_id) noexcept override | ||
{ | ||
char span_buf[16]; | ||
span_id.ToLowerBase16(span_buf); | ||
json_["spanid"] = std::string(span_buf, sizeof(span_buf)); | ||
} | ||
|
||
/** | ||
* Inject a trace_flags for this log. | ||
* @param trace_flags the span id to set | ||
*/ | ||
void SetTraceFlags(opentelemetry::trace::TraceFlags trace_flags) noexcept override | ||
{ | ||
char flag_buf[2]; | ||
trace_flags.ToLowerBase16(flag_buf); | ||
json_["traceflags"] = std::string(flag_buf, sizeof(flag_buf)); | ||
} | ||
|
||
/** | ||
* Set the timestamp for this log. | ||
* @param timestamp the timestamp of the event | ||
*/ | ||
void SetTimestamp(core::SystemTimestamp timestamp) noexcept override | ||
{ | ||
json_["timestamp"] = timestamp.time_since_epoch().count(); | ||
} | ||
|
||
/** | ||
* Returns a JSON object contain the log information | ||
*/ | ||
nlohmann::json GetJSON() noexcept { return json_; }; | ||
}; | ||
} // namespace logs | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.