Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support span serializer #1903

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions core/constants/SpanConstants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2024 iLogtail 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.

#include "SpanConstants.h"

namespace logtail {

const std::string DEFAULT_TRACE_TAG_TRACE_ID = "traceId";
const std::string DEFAULT_TRACE_TAG_SPAN_ID = "spanId";
const std::string DEFAULT_TRACE_TAG_PARENT_ID = "parentSpanId";
const std::string DEFAULT_TRACE_TAG_SPAN_NAME = "spanName";
const std::string DEFAULT_TRACE_TAG_SERVICE_NAME = "serviceName";
const std::string DEFAULT_TRACE_TAG_START_TIME_NANO = "startTime";
const std::string DEFAULT_TRACE_TAG_END_TIME_NANO = "endTime";
const std::string DEFAULT_TRACE_TAG_DURATION = "duration";
const std::string DEFAULT_TRACE_TAG_ATTRIBUTES = "attributes";
const std::string DEFAULT_TRACE_TAG_RESOURCE = "resources";
const std::string DEFAULT_TRACE_TAG_LINKS = "links";
const std::string DEFAULT_TRACE_TAG_EVENTS = "events";
const std::string DEFAULT_TRACE_TAG_TIMESTAMP = "timestamp";
const std::string DEFAULT_TRACE_TAG_STATUS_CODE = "statusCode";
const std::string DEFAULT_TRACE_TAG_STATUS_MESSAGE = "statusMessage";
const std::string DEFAULT_TRACE_TAG_SPAN_KIND = "kind";
const std::string DEFAULT_TRACE_TAG_TRACE_STATE = "traceState";
const std::string DEFAULT_TRACE_TAG_SPAN_EVENT_NAME = "name";
#ifdef __ENTERPRISE__
// for arms
const std::string DEFAULT_TRACE_TAG_APP_ID = "pid";
const std::string DEFAULT_TRACE_TAG_IP = "ip";
#endif

} // namespace logtail
45 changes: 45 additions & 0 deletions core/constants/SpanConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 iLogtail 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 <string>

namespace logtail {
extern const std::string DEFAULT_TRACE_TAG_TRACE_ID;
extern const std::string DEFAULT_TRACE_TAG_SPAN_ID;
extern const std::string DEFAULT_TRACE_TAG_PARENT_ID;
extern const std::string DEFAULT_TRACE_TAG_SPAN_NAME;
extern const std::string DEFAULT_TRACE_TAG_SERVICE_NAME;
extern const std::string DEFAULT_TRACE_TAG_START_TIME_NANO;
extern const std::string DEFAULT_TRACE_TAG_END_TIME_NANO;
extern const std::string DEFAULT_TRACE_TAG_DURATION;
extern const std::string DEFAULT_TRACE_TAG_ATTRIBUTES;
extern const std::string DEFAULT_TRACE_TAG_RESOURCE;
extern const std::string DEFAULT_TRACE_TAG_LINKS;
extern const std::string DEFAULT_TRACE_TAG_EVENTS;
extern const std::string DEFAULT_TRACE_TAG_TIMESTAMP;
extern const std::string DEFAULT_TRACE_TAG_STATUS_CODE;
extern const std::string DEFAULT_TRACE_TAG_STATUS_MESSAGE;
extern const std::string DEFAULT_TRACE_TAG_SPAN_KIND;
extern const std::string DEFAULT_TRACE_TAG_TRACE_STATE;
extern const std::string DEFAULT_TRACE_TAG_SPAN_EVENT_NAME ;
#ifdef __ENTERPRISE__
// for arms
extern const std::string DEFAULT_TRACE_TAG_APP_ID;
extern const std::string DEFAULT_TRACE_TAG_IP;
#endif

} // namespace logtail
3 changes: 3 additions & 0 deletions core/constants/TagConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ namespace logtail {
const std::string DEFAULT_METRIC_TAG_CONTAINER_IP = DEFAULT_TAG_CONTAINER_IP;
const std::string DEFAULT_METRIC_TAG_IMAGE_NAME = DEFAULT_TAG_IMAGE_NAME;

////////////////////////// TRACE ////////////////////////


} // namespace logtail
4 changes: 4 additions & 0 deletions core/constants/TagConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ namespace logtail {
extern const std::string DEFAULT_METRIC_TAG_CONTAINER_IP;
extern const std::string DEFAULT_METRIC_TAG_IMAGE_NAME;

////////////////////////// TRACE ////////////////////////



} // namespace logtail
90 changes: 62 additions & 28 deletions core/models/SpanEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "models/SpanEvent.h"
#include "constants/SpanConstants.h"

using namespace std;

Expand Down Expand Up @@ -75,34 +76,34 @@ size_t SpanEvent::SpanLink::DataSize() const {
return mTraceId.size() + mSpanId.size() + mTraceState.size() + mTags.DataSize();
}

#ifdef APSARA_UNIT_TEST_MAIN
Json::Value SpanEvent::SpanLink::ToJson() const {
Json::Value root;
root["traceId"] = mTraceId.to_string();
root["spanId"] = mSpanId.to_string();
root[DEFAULT_TRACE_TAG_TRACE_ID] = mTraceId.to_string();
root[DEFAULT_TRACE_TAG_SPAN_ID] = mSpanId.to_string();
if (!mTraceState.empty()) {
root["traceState"] = mTraceState.to_string();
root[DEFAULT_TRACE_TAG_TRACE_STATE] = mTraceState.to_string();
}
if (!mTags.mInner.empty()) {
Json::Value& tags = root["tags"];
Json::Value& tags = root[DEFAULT_TRACE_TAG_ATTRIBUTES];
for (const auto& tag : mTags.mInner) {
tags[tag.first.to_string()] = tag.second.to_string();
}
}
return root;
}

#ifdef APSARA_UNIT_TEST_MAIN
void SpanEvent::SpanLink::FromJson(const Json::Value& value) {
SetTraceId(value["traceId"].asString());
SetSpanId(value["spanId"].asString());
if (value.isMember("traceState")) {
string s = value["traceState"].asString();
SetTraceId(value[DEFAULT_TRACE_TAG_TRACE_ID].asString());
SetSpanId(value[DEFAULT_TRACE_TAG_SPAN_ID].asString());
if (value.isMember(DEFAULT_TRACE_TAG_TRACE_STATE)) {
string s = value[DEFAULT_TRACE_TAG_TRACE_STATE].asString();
if (!s.empty()) {
SetTraceState(s);
}
}
if (value.isMember("tags")) {
Json::Value tags = value["tags"];
if (value.isMember(DEFAULT_TRACE_TAG_ATTRIBUTES)) {
Json::Value tags = value[DEFAULT_TRACE_TAG_ATTRIBUTES];
for (const auto& key : tags.getMemberNames()) {
SetTag(key, tags[key].asString());
}
Expand Down Expand Up @@ -155,25 +156,25 @@ size_t SpanEvent::InnerEvent::DataSize() const {
return sizeof(decltype(mTimestampNs)) + mName.size() + mTags.DataSize();
}

#ifdef APSARA_UNIT_TEST_MAIN
Json::Value SpanEvent::InnerEvent::ToJson() const {
Json::Value root;
root["name"] = mName.to_string();
root["timestampNs"] = static_cast<int64_t>(mTimestampNs);
root[DEFAULT_TRACE_TAG_SPAN_EVENT_NAME] = mName.to_string();
root[DEFAULT_TRACE_TAG_TIMESTAMP] = static_cast<int64_t>(mTimestampNs);
if (!mTags.mInner.empty()) {
Json::Value& tags = root["tags"];
Json::Value& tags = root[DEFAULT_TRACE_TAG_ATTRIBUTES];
for (const auto& tag : mTags.mInner) {
tags[tag.first.to_string()] = tag.second.to_string();
}
}
return root;
}

#ifdef APSARA_UNIT_TEST_MAIN
void SpanEvent::InnerEvent::FromJson(const Json::Value& value) {
SetName(value["name"].asString());
SetTimestampNs(value["timestampNs"].asUInt64());
if (value.isMember("tags")) {
Json::Value tags = value["tags"];
SetName(value[DEFAULT_TRACE_TAG_SPAN_EVENT_NAME].asString());
SetTimestampNs(value[DEFAULT_TRACE_TAG_TIMESTAMP].asUInt64());
if (value.isMember(DEFAULT_TRACE_TAG_ATTRIBUTES)) {
Json::Value tags = value[DEFAULT_TRACE_TAG_ATTRIBUTES];
for (const auto& key : tags.getMemberNames()) {
SetTag(key, tags[key].asString());
}
Expand Down Expand Up @@ -350,19 +351,19 @@ Json::Value SpanEvent::ToJson(bool enableEventMeta) const {
root["startTimeNs"] = static_cast<int64_t>(mStartTimeNs);
root["endTimeNs"] = static_cast<int64_t>(mEndTimeNs);
if (!mTags.mInner.empty()) {
Json::Value& tags = root["tags"];
Json::Value& tags = root[DEFAULT_TRACE_TAG_ATTRIBUTES];
for (const auto& tag : mTags.mInner) {
tags[tag.first.to_string()] = tag.second.to_string();
}
}
if (!mEvents.empty()) {
Json::Value& events = root["events"];
Json::Value& events = root[DEFAULT_TRACE_TAG_EVENTS];
for (const auto& event : mEvents) {
events.append(event.ToJson());
}
}
if (!mLinks.empty()) {
Json::Value& links = root["links"];
Json::Value& links = root[DEFAULT_TRACE_TAG_LINKS];
for (const auto& link : mLinks) {
links.append(link.ToJson());
}
Expand Down Expand Up @@ -405,21 +406,21 @@ bool SpanEvent::FromJson(const Json::Value& root) {
}
SetStartTimeNs(root["startTimeNs"].asUInt64());
SetEndTimeNs(root["endTimeNs"].asUInt64());
if (root.isMember("tags")) {
Json::Value tags = root["tags"];
if (root.isMember(DEFAULT_TRACE_TAG_ATTRIBUTES)) {
Json::Value tags = root[DEFAULT_TRACE_TAG_ATTRIBUTES];
for (const auto& key : tags.getMemberNames()) {
SetTag(key, tags[key].asString());
}
}
if (root.isMember("events")) {
Json::Value events = root["events"];
if (root.isMember(DEFAULT_TRACE_TAG_EVENTS)) {
Json::Value events = root[DEFAULT_TRACE_TAG_EVENTS];
for (const auto& event : events) {
InnerEvent* e = AddEvent();
e->FromJson(event);
}
}
if (root.isMember("links")) {
Json::Value links = root["links"];
if (root.isMember(DEFAULT_TRACE_TAG_LINKS)) {
Json::Value links = root[DEFAULT_TRACE_TAG_LINKS];
for (const auto& link : links) {
SpanLink* l = AddLink();
l->FromJson(link);
Expand All @@ -438,4 +439,37 @@ bool SpanEvent::FromJson(const Json::Value& root) {
}
#endif

const static std::string sSpanStatusCodeUnSet = "UNSET";
const static std::string sSpanStatusCodeOk = "OK";
const static std::string sSpanStatusCodeError = "ERROR";

const std::string& GetStatusString(SpanEvent::StatusCode status) {
switch (status) {
case SpanEvent::StatusCode::Unset: return sSpanStatusCodeUnSet;
case SpanEvent::StatusCode::Ok: return sSpanStatusCodeOk;
case SpanEvent::StatusCode::Error: return sSpanStatusCodeError;
default: return sSpanStatusCodeUnSet;
}
}

const static std::string sSpanKindUnspecified = "unspecified";
const static std::string sSpanKindInternal = "internal";
const static std::string sSpanKindServer = "server";
const static std::string sSpanKindClient = "client";
const static std::string sSpanKindProducer = "producer";
const static std::string sSpanKindConsumer = "consumer";
const static std::string sSpanKindUnknown = "unknown";

const std::string& GetKindString(SpanEvent::Kind kind) {
switch (kind) {
case SpanEvent::Kind::Unspecified: return sSpanKindUnspecified;
case SpanEvent::Kind::Internal: return sSpanKindInternal;
case SpanEvent::Kind::Server: return sSpanKindServer;
case SpanEvent::Kind::Client: return sSpanKindClient;
case SpanEvent::Kind::Producer: return sSpanKindProducer;
case SpanEvent::Kind::Consumer: return sSpanKindConsumer;
default: return sSpanKindUnknown;
}
}

} // namespace logtail
8 changes: 6 additions & 2 deletions core/models/SpanEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <map>
#include <string>
#include <vector>
#include <json/json.h>

#include "common/memory/SourceBuffer.h"
#include "models/PipelineEvent.h"
Expand Down Expand Up @@ -67,8 +68,8 @@ class SpanEvent : public PipelineEvent {

size_t DataSize() const;

#ifdef APSARA_UNIT_TEST_MAIN
Json::Value ToJson() const;
#ifdef APSARA_UNIT_TEST_MAIN
void FromJson(const Json::Value& value);
#endif

Expand Down Expand Up @@ -107,8 +108,8 @@ class SpanEvent : public PipelineEvent {

size_t DataSize() const;

#ifdef APSARA_UNIT_TEST_MAIN
Json::Value ToJson() const;
#ifdef APSARA_UNIT_TEST_MAIN
void FromJson(const Json::Value& value);
#endif

Expand Down Expand Up @@ -214,4 +215,7 @@ class SpanEvent : public PipelineEvent {
#endif
};

const std::string& GetStatusString(SpanEvent::StatusCode status);
const std::string& GetKindString(SpanEvent::Kind kind);

} // namespace logtail
Loading
Loading