Skip to content

Commit 2e79399

Browse files
authored
implement log and trace integration (#79)
1 parent 7ed4662 commit 2e79399

File tree

10 files changed

+127
-5
lines changed

10 files changed

+127
-5
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ After setup configurations, try to put values with
158158
curl --request PUT --data-binary "@./config.yaml" http://localhost:8500/v1/kv/configuration-discovery.default.agentConfigurations
159159
```
160160

161+
## Trace and Log integration
162+
163+
cpp2sky implements to output logs which is the key to integrate with actual tracing context.
164+
165+
#### Supported Logger
166+
167+
- [spdlog](https://github.com/gabime/spdlog)
168+
169+
```cpp
170+
#include <spdlog/spdlog.h>
171+
#include <cpp2sky/trace_log.h>
172+
173+
int main() {
174+
auto logger = spdlog::default_logger();
175+
// set_pattern must be called.
176+
logger->set_pattern(logFormat<decltype(logger)::element_type>());
177+
178+
// It will generate log message as follows.
179+
//
180+
// {"level": "warning", "msg": "sample", "SW_CTX": ["service","instance","trace_id","segment_id","span_id"]}
181+
//
182+
logger->warn(tracing_context->logMessage("sample"));
183+
}
184+
```
185+
161186
## Security
162187

163188
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.

cpp2sky/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ cc_library(
2323
"well_known_names.h",
2424
"exception.h",
2525
"time.h",
26+
"assert.h",
27+
"trace_log.h",
2628
],
2729
deps = [
2830
":config_cc_proto",
@@ -38,6 +40,7 @@ cc_library(
3840
"well_known_names.h",
3941
"exception.h",
4042
"time.h",
43+
"assert.h",
4144
],
4245
deps = [
4346
":config_cc_proto",

cpp2sky/assert.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 SkyAPM
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
namespace cpp2sky {
18+
19+
template <class T>
20+
static constexpr bool false_v = false;
21+
22+
#define CPP2SKY_STATIC_ASSERT(T, m) static_assert(false_v<T>, m)
23+
24+
} // namespace cpp2sky

cpp2sky/time.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616

1717
#include <chrono>
1818

19+
#include "cpp2sky/assert.h"
20+
1921
namespace cpp2sky {
2022

2123
using SystemTime = std::chrono::system_clock::time_point;
2224
using SteadyTime = std::chrono::steady_clock::time_point;
2325

24-
template <class T>
25-
static constexpr bool false_v = false;
26-
2726
template <class T>
2827
class TimePoint {
29-
static_assert(false_v<T>, "Invalid time type");
28+
CPP2SKY_STATIC_ASSERT(T, "Invalid time type");
3029
};
3130

3231
template <>

cpp2sky/trace_log.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2021 SkyAPM
2+
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <spdlog/logger.h>
18+
19+
#include <string_view>
20+
#include <type_traits>
21+
22+
#include "cpp2sky/assert.h"
23+
24+
namespace cpp2sky {
25+
26+
static constexpr std::string_view SPDLOG_LOG_FORMAT =
27+
"{\"level\": \"%^%l%$\", \"msg\": \"%v";
28+
29+
template <class T>
30+
std::string logFormat() {
31+
if constexpr (std::is_same_v<T, spdlog::logger>) {
32+
return SPDLOG_LOG_FORMAT.data();
33+
} else {
34+
CPP2SKY_STATIC_ASSERT(T, "non-supported logger type");
35+
}
36+
}
37+
38+
} // namespace cpp2sky

cpp2sky/tracing_context.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ class TracingContext {
264264
* Determine whether to send this segment or not.
265265
*/
266266
virtual bool readyToSend() = 0;
267+
268+
/**
269+
* Get log message. Output value of this function is based on default cpp2sky
270+
* logging format following with any format extracted with
271+
* cpp2sky::logFormat().
272+
*/
273+
virtual std::string logMessage(std::string_view message) const = 0;
267274
};
268275

269276
using TracingContextPtr = std::shared_ptr<TracingContext>;

source/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cc_library(
88
"grpc_async_client_impl.h",
99
"tracer_impl.h",
1010
"cds_impl.h",
11-
"dynamic_config.h"
11+
"dynamic_config.h",
1212
],
1313
srcs = [
1414
"propagation_impl.cc",

source/tracing_context_impl.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,23 @@ bool TracingContextImpl::readyToSend() {
267267
return true;
268268
}
269269

270+
std::string TracingContextImpl::logMessage(std::string_view message) const {
271+
std::string output = message.data();
272+
output += "\", \"SW_CTX\": [";
273+
output += "\"" + service_ + "\",";
274+
output += "\"" + service_instance_ + "\",";
275+
output += "\"" + trace_id_ + "\",";
276+
output += "\"" + trace_segment_id_ + "\",";
277+
278+
if (!spans_.empty()) {
279+
output += "\"" + std::to_string(spans_.back()->spanId()) + "\"]}";
280+
} else {
281+
output += "\"-1\"]}";
282+
}
283+
284+
return output;
285+
}
286+
270287
TracingContextFactory::TracingContextFactory(const TracerConfig& config)
271288
: service_name_(config.service_name()),
272289
instance_name_(config.instance_name()) {}

source/tracing_context_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class TracingContextImpl : public TracingContext {
155155
void setSkipAnalysis() override { should_skip_analysis_ = true; }
156156
bool skipAnalysis() override { return should_skip_analysis_; }
157157
bool readyToSend() override;
158+
std::string logMessage(std::string_view message) const override;
158159

159160
private:
160161
std::string encodeSpan(TracingSpanPtr parent_span,

test/tracing_context_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,12 @@ TEST_F(TracingContextTest, ReadyToSendTest) {
372372
EXPECT_FALSE(sc->readyToSend());
373373
}
374374

375+
TEST_F(TracingContextTest, TraceLogTest) {
376+
TracingContextImpl sc(config_.service_name(), config_.instance_name(),
377+
span_ctx_, span_ext_ctx_, random_);
378+
EXPECT_EQ(
379+
"test\", \"SW_CTX\": [\"mesh\",\"service_0\",\"1\",\"uuid\",\"-1\"]}",
380+
sc.logMessage("test"));
381+
}
382+
375383
} // namespace cpp2sky

0 commit comments

Comments
 (0)