Skip to content

Commit

Permalink
implement log and trace integration (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shikugawa authored Jun 14, 2021
1 parent 7ed4662 commit 2e79399
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 5 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ After setup configurations, try to put values with
curl --request PUT --data-binary "@./config.yaml" http://localhost:8500/v1/kv/configuration-discovery.default.agentConfigurations
```

## Trace and Log integration

cpp2sky implements to output logs which is the key to integrate with actual tracing context.

#### Supported Logger

- [spdlog](https://github.com/gabime/spdlog)

```cpp
#include <spdlog/spdlog.h>
#include <cpp2sky/trace_log.h>

int main() {
auto logger = spdlog::default_logger();
// set_pattern must be called.
logger->set_pattern(logFormat<decltype(logger)::element_type>());

// It will generate log message as follows.
//
// {"level": "warning", "msg": "sample", "SW_CTX": ["service","instance","trace_id","segment_id","span_id"]}
//
logger->warn(tracing_context->logMessage("sample"));
}
```

## Security

If you've found any security issues, please read [Security Reporting Process](https://github.com/SkyAPM/cpp2sky/blob/main/SECURITY.md) and take described steps.
Expand Down
3 changes: 3 additions & 0 deletions cpp2sky/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ cc_library(
"well_known_names.h",
"exception.h",
"time.h",
"assert.h",
"trace_log.h",
],
deps = [
":config_cc_proto",
Expand All @@ -38,6 +40,7 @@ cc_library(
"well_known_names.h",
"exception.h",
"time.h",
"assert.h",
],
deps = [
":config_cc_proto",
Expand Down
24 changes: 24 additions & 0 deletions cpp2sky/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020 SkyAPM

// 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

namespace cpp2sky {

template <class T>
static constexpr bool false_v = false;

#define CPP2SKY_STATIC_ASSERT(T, m) static_assert(false_v<T>, m)

} // namespace cpp2sky
7 changes: 3 additions & 4 deletions cpp2sky/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

#include <chrono>

#include "cpp2sky/assert.h"

namespace cpp2sky {

using SystemTime = std::chrono::system_clock::time_point;
using SteadyTime = std::chrono::steady_clock::time_point;

template <class T>
static constexpr bool false_v = false;

template <class T>
class TimePoint {
static_assert(false_v<T>, "Invalid time type");
CPP2SKY_STATIC_ASSERT(T, "Invalid time type");
};

template <>
Expand Down
38 changes: 38 additions & 0 deletions cpp2sky/trace_log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 SkyAPM

// 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 <spdlog/logger.h>

#include <string_view>
#include <type_traits>

#include "cpp2sky/assert.h"

namespace cpp2sky {

static constexpr std::string_view SPDLOG_LOG_FORMAT =
"{\"level\": \"%^%l%$\", \"msg\": \"%v";

template <class T>
std::string logFormat() {
if constexpr (std::is_same_v<T, spdlog::logger>) {
return SPDLOG_LOG_FORMAT.data();
} else {
CPP2SKY_STATIC_ASSERT(T, "non-supported logger type");
}
}

} // namespace cpp2sky
7 changes: 7 additions & 0 deletions cpp2sky/tracing_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ class TracingContext {
* Determine whether to send this segment or not.
*/
virtual bool readyToSend() = 0;

/**
* Get log message. Output value of this function is based on default cpp2sky
* logging format following with any format extracted with
* cpp2sky::logFormat().
*/
virtual std::string logMessage(std::string_view message) const = 0;
};

using TracingContextPtr = std::shared_ptr<TracingContext>;
Expand Down
2 changes: 1 addition & 1 deletion source/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cc_library(
"grpc_async_client_impl.h",
"tracer_impl.h",
"cds_impl.h",
"dynamic_config.h"
"dynamic_config.h",
],
srcs = [
"propagation_impl.cc",
Expand Down
17 changes: 17 additions & 0 deletions source/tracing_context_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ bool TracingContextImpl::readyToSend() {
return true;
}

std::string TracingContextImpl::logMessage(std::string_view message) const {
std::string output = message.data();
output += "\", \"SW_CTX\": [";
output += "\"" + service_ + "\",";
output += "\"" + service_instance_ + "\",";
output += "\"" + trace_id_ + "\",";
output += "\"" + trace_segment_id_ + "\",";

if (!spans_.empty()) {
output += "\"" + std::to_string(spans_.back()->spanId()) + "\"]}";
} else {
output += "\"-1\"]}";
}

return output;
}

TracingContextFactory::TracingContextFactory(const TracerConfig& config)
: service_name_(config.service_name()),
instance_name_(config.instance_name()) {}
Expand Down
1 change: 1 addition & 0 deletions source/tracing_context_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class TracingContextImpl : public TracingContext {
void setSkipAnalysis() override { should_skip_analysis_ = true; }
bool skipAnalysis() override { return should_skip_analysis_; }
bool readyToSend() override;
std::string logMessage(std::string_view message) const override;

private:
std::string encodeSpan(TracingSpanPtr parent_span,
Expand Down
8 changes: 8 additions & 0 deletions test/tracing_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,12 @@ TEST_F(TracingContextTest, ReadyToSendTest) {
EXPECT_FALSE(sc->readyToSend());
}

TEST_F(TracingContextTest, TraceLogTest) {
TracingContextImpl sc(config_.service_name(), config_.instance_name(),
span_ctx_, span_ext_ctx_, random_);
EXPECT_EQ(
"test\", \"SW_CTX\": [\"mesh\",\"service_0\",\"1\",\"uuid\",\"-1\"]}",
sc.logMessage("test"));
}

} // namespace cpp2sky

0 comments on commit 2e79399

Please sign in to comment.