DatadogのAPM Instrumentation for C++はCustom instrumentationをサポートしております。
本章では、詳細なInstrumentationの手順をご紹介します。
- C++ compiler supporting C++17 or later
ℹ️ Remark
dd-trace-cppv1.0.0 and later requires a C++17-compatible compiler (e.g., GCC 7+, Clang 5+, MSVC 2017+).
If your existing codebase uses C++11/14, it must be upgraded to compile under C++17 for integration.
Requirement: >= C++17 compiler
- Supported Platforms
x86_64 and arm64 Linux.
x86_64 Windows.
arm64 macOS.
wget https://github.com/DataDog/dd-trace-cpp/archive/v1.0.0.tar.gz -O dd-trace-cpp.tar.gzmkdir dd-trace-cpp
tar zxvf dd-trace-cpp.tar.gz -C ./dd-trace-cpp/ --strip-components=1cd dd-trace-cpp
cmake -B build -DCMAKE_BUILD_TYPE=Release .
cmake --build build -j
cmake --install build※tracer_demo.cppをフルコードとして参考可能
{
// Create a root span for the current request.
auto root_span = tracer.create_span();
root_span.set_name("get_ingredients");
// Set a resource name for the root span.
root_span.set_resource_name("bologna_sandwich");
// Create a child span with the root span as its parent.
auto child_span = root_span.create_child();
child_span.set_name("cache_lookup");
// Set a resource name for the child span.
child_span.set_resource_name("ingredients.bologna_sandwich");
// Spans can be finished at an explicit time ...
child_span.set_end_time(std::chrono::steady_clock::now());
} // ... or implicitly when the destructor is invoked.
// For example, root_span finishes here.<ローカル定義方式>
// Add tags directly to a span by calling `Span::set_tag`
auto span = tracer.create_span();
span.set_tag("key must be string", "value must also be a string");
// Or, add tags by setting a `SpanConfig`
datadog::tracing::SpanConfig opts;
opts.tags.emplace("team", "apm-proxy");
auto span2 = tracer.create_span(opts);<グローバル定義方式>
- 環境変数に定義
export DD_TAGS=team:apm-proxy,key:value- それから環境変数を利用してコード改修
datadog::tracing::TracerConfig tracer_config;
tracer_config.tags = {
{"team", "apm-proxy"},
{"apply", "on all spans"}
};
const auto validated_config = datadog::tracing::finalize_config(tracer_config);
auto tracer = datadog::tracing::Tracer(*validated_config);
// All new spans will have contains tags defined in `tracer_config.tags`
auto span = tracer.create_span();span.set_error(true);
//エラーのタグ追加
span.set_error_message("error");
span.set_error_stack("[EBADF] invalid file");
span.set_error_type("errno"); ・app_demo.cpp: オリジンプログラム
・tracer_demo.cpp: app_demo.cppに対してDatadog APMをInstrumentation後のコード
・construction.txt: app_demoの呼び出し構造、特に利用する必要はない
g++ -std=c++17 -I/usr/local/include -L/usr/local/lib -o tracer_demo tracer_demo.cpp -ldd_trace_cpp-static -lcurl -lpthread -ldl./tracer_demo