Skip to content

Commit

Permalink
[part 1] refactor!: expose telemetry module (#166)
Browse files Browse the repository at this point in the history
Part 1 of the telemetry module refactoring is to make the telemetry module publicly usable by other Datadog products without any dependencies on the tracer.

Changes:
- Replace `report_telemetry` configuration with telemetry configuration struct.
- Add support for telemetry environment variables.
- Implement unit tests
  • Loading branch information
dmehala authored Oct 30, 2024
1 parent 159629e commit 963f78b
Show file tree
Hide file tree
Showing 26 changed files with 616 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
name: Building
command: |
& 'C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\Tools\\Launch-VsDevShell.ps1' -arch << parameters.arch >>
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DDD_TRACE_BUILD_TESTING=1 -G Ninja .
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=OFF -DDD_TRACE_STATIC_CRT=1 -DDD_TRACE_BUILD_TESTING=1 -G Ninja .
cmake --build build -j $env:MAKE_JOB_COUNT -v
- run:
name: Testing
Expand Down
8 changes: 6 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cc_library(
name = "dd_trace_cpp",
srcs = [
"src/datadog/telemetry/configuration.cpp",
"src/datadog/telemetry/metrics.cpp",
"src/datadog/telemetry/telemetry.cpp",
"src/datadog/base64.cpp",
"src/datadog/cerr_logger.cpp",
"src/datadog/clock.cpp",
Expand All @@ -17,7 +20,6 @@ cc_library(
"src/datadog/id_generator.cpp",
"src/datadog/limiter.cpp",
"src/datadog/logger.cpp",
"src/datadog/metrics.cpp",
"src/datadog/msgpack.cpp",
"src/datadog/parse_util.cpp",
"src/datadog/platform_util.cpp",
Expand Down Expand Up @@ -58,7 +60,6 @@ cc_library(
"src/datadog/json.hpp",
"src/datadog/json_serializer.h",
"src/datadog/limiter.h",
"src/datadog/metrics.h",
"src/datadog/msgpack.h",
"src/datadog/parse_util.h",
"src/datadog/platform_util.h",
Expand Down Expand Up @@ -111,6 +112,9 @@ cc_library(
"include/datadog/trace_sampler_config.h",
"include/datadog/trace_segment.h",
"include/datadog/version.h",
"include/datadog/telemetry/configuration.h",
"include/datadog/telemetry/metrics.h",
"include/datadog/telemetry/telemetry.h",
"include/datadog/remote_config/capability.h",
"include/datadog/remote_config/listener.h",
"include/datadog/remote_config/product.h",
Expand Down
15 changes: 14 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_STATIC_LIBS "Build static libraries" ON)

if (WIN32)
option(DD_TRACE_STATIC_CRT "Build dd-trace-cpp with static CRT with MSVC" OFF)
endif ()

if (DD_TRACE_STATIC_CRT)
set(CURL_STATIC_CRT ON)
endif ()

set(DD_TRACE_TRANSPORT "curl" CACHE STRING "HTTP transport that dd-trace-cpp uses to communicate with the Datadog Agent, can be either 'none' or 'curl'")

# Consumer of the library using FetchContent do not need
Expand Down Expand Up @@ -46,6 +54,9 @@ unset(DD_TRACE_VERSION_CPP_CONTENTS)
message(STATUS "dd-trace-cpp transport=${DD_TRACE_TRANSPORT}")

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
if (BUILD_SHARED_LIBS AND BUILD_STATIC_LIBS)
message(FATAL_ERROR "Can't build both static and shared libary for MSVC")
endif ()
message(STATUS "Compiler: MSVC ${CMAKE_CXX_COMPILER_VERSION}")
include(cmake/compiler/msvc.cmake)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
Expand Down Expand Up @@ -87,6 +98,9 @@ target_sources(dd_trace_cpp-objects
BASE_DIRS include
FILES ${public_header_files}
PRIVATE
src/datadog/telemetry/configuration.cpp
src/datadog/telemetry/metrics.cpp
src/datadog/telemetry/telemetry.cpp
src/datadog/base64.cpp
src/datadog/cerr_logger.cpp
src/datadog/clock.cpp
Expand All @@ -102,7 +116,6 @@ target_sources(dd_trace_cpp-objects
src/datadog/id_generator.cpp
src/datadog/limiter.cpp
src/datadog/logger.cpp
src/datadog/metrics.cpp
src/datadog/msgpack.cpp
src/datadog/parse_util.cpp
src/datadog/platform_util.cpp
Expand Down
10 changes: 10 additions & 0 deletions cmake/compiler/msvc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ set(WINDOWS_EXPORT_ALL_SYMBOLS ON)
add_library(dd_trace_cpp-specs INTERFACE)
add_library(dd_trace::specs ALIAS dd_trace_cpp-specs)

if (DD_TRACE_STATIC_CRT)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
# set_target_properties(dd_trace_cpp-specs
# PROPERTIES
# MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
# )
endif ()

target_compile_options(dd_trace_cpp-specs
INTERFACE
/W4
Expand Down
60 changes: 32 additions & 28 deletions include/datadog/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,38 @@ namespace environment {

// To enforce correspondence between `enum Variable` and `variable_names`, the
// preprocessor is used so that the DD_* symbols are listed exactly once.
#define LIST_ENVIRONMENT_VARIABLES(MACRO) \
MACRO(DD_AGENT_HOST) \
MACRO(DD_ENV) \
MACRO(DD_INSTRUMENTATION_TELEMETRY_ENABLED) \
MACRO(DD_PROPAGATION_STYLE_EXTRACT) \
MACRO(DD_PROPAGATION_STYLE_INJECT) \
MACRO(DD_REMOTE_CONFIGURATION_ENABLED) \
MACRO(DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS) \
MACRO(DD_SERVICE) \
MACRO(DD_SPAN_SAMPLING_RULES) \
MACRO(DD_SPAN_SAMPLING_RULES_FILE) \
MACRO(DD_TRACE_DELEGATE_SAMPLING) \
MACRO(DD_TRACE_PROPAGATION_STYLE_EXTRACT) \
MACRO(DD_TRACE_PROPAGATION_STYLE_INJECT) \
MACRO(DD_TRACE_PROPAGATION_STYLE) \
MACRO(DD_TAGS) \
MACRO(DD_TRACE_AGENT_PORT) \
MACRO(DD_TRACE_AGENT_URL) \
MACRO(DD_TRACE_DEBUG) \
MACRO(DD_TRACE_ENABLED) \
MACRO(DD_TRACE_RATE_LIMIT) \
MACRO(DD_TRACE_REPORT_HOSTNAME) \
MACRO(DD_TRACE_SAMPLE_RATE) \
MACRO(DD_TRACE_SAMPLING_RULES) \
MACRO(DD_TRACE_STARTUP_LOGS) \
MACRO(DD_TRACE_TAGS_PROPAGATION_MAX_LENGTH) \
MACRO(DD_VERSION) \
MACRO(DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED)
#define LIST_ENVIRONMENT_VARIABLES(MACRO) \
MACRO(DD_AGENT_HOST) \
MACRO(DD_ENV) \
MACRO(DD_INSTRUMENTATION_TELEMETRY_ENABLED) \
MACRO(DD_PROPAGATION_STYLE_EXTRACT) \
MACRO(DD_PROPAGATION_STYLE_INJECT) \
MACRO(DD_REMOTE_CONFIGURATION_ENABLED) \
MACRO(DD_REMOTE_CONFIG_POLL_INTERVAL_SECONDS) \
MACRO(DD_SERVICE) \
MACRO(DD_SPAN_SAMPLING_RULES) \
MACRO(DD_SPAN_SAMPLING_RULES_FILE) \
MACRO(DD_TRACE_DELEGATE_SAMPLING) \
MACRO(DD_TRACE_PROPAGATION_STYLE_EXTRACT) \
MACRO(DD_TRACE_PROPAGATION_STYLE_INJECT) \
MACRO(DD_TRACE_PROPAGATION_STYLE) \
MACRO(DD_TAGS) \
MACRO(DD_TRACE_AGENT_PORT) \
MACRO(DD_TRACE_AGENT_URL) \
MACRO(DD_TRACE_DEBUG) \
MACRO(DD_TRACE_ENABLED) \
MACRO(DD_TRACE_RATE_LIMIT) \
MACRO(DD_TRACE_REPORT_HOSTNAME) \
MACRO(DD_TRACE_SAMPLE_RATE) \
MACRO(DD_TRACE_SAMPLING_RULES) \
MACRO(DD_TRACE_STARTUP_LOGS) \
MACRO(DD_TRACE_TAGS_PROPAGATION_MAX_LENGTH) \
MACRO(DD_VERSION) \
MACRO(DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED) \
MACRO(DD_TELEMETRY_HEARTBEAT_INTERVAL) \
MACRO(DD_TELEMETRY_METRICS_ENABLED) \
MACRO(DD_TELEMETRY_METRICS_INTERVAL_SECONDS) \
MACRO(DD_TELEMETRY_DEBUG)

#define WITH_COMMA(ARG) ARG,

Expand Down
50 changes: 50 additions & 0 deletions include/datadog/telemetry/configuration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <datadog/expected.h>
#include <datadog/optional.h>

#include <chrono>
#include <string>

namespace datadog::telemetry {

struct Configuration {
// Turn on or off telemetry module
// Enabled by default.
// Overwritten by `DD_INSTRUMENTATION_TELEMETRY_ENABLED` env var.
tracing::Optional<bool> enabled;
// Turn on or off metrics reporting
// Overwritten by `DD_TELEMETRY_METRICS_ENABLED` env var.
tracing::Optional<bool> report_metrics;
// Interval of metrics payload
// Overwritten by `DD_TELEMETRY_METRICS_INTERVAL_SECONDS` env var.
tracing::Optional<int> metrics_interval_seconds;
// Interval of heartbeat payload
// Overwritten by `DD_TELEMETRY_HEARTBEAT_INTERVAL` env var.
tracing::Optional<int> heartbeat_interval_seconds;
// `integration_name` is the name of the product integrating this library.
// Example: "nginx", "envoy" or "istio".
tracing::Optional<std::string> integration_name;
// `integration_version` is the version of the product integrating this
// library.
// Example: "1.2.3", "6c44da20", "2020.02.13"
tracing::Optional<std::string> integration_version;
};

struct FinalizedConfiguration {
bool debug;
bool enabled;
bool report_metrics;
std::chrono::steady_clock::duration metrics_interval;
std::chrono::steady_clock::duration heartbeat_interval;
std::string integration_name;
std::string integration_version;

friend tracing::Expected<FinalizedConfiguration> finalize_config(
const Configuration&);
};

tracing::Expected<FinalizedConfiguration> finalize_config(
const Configuration& = Configuration{});

} // namespace datadog::telemetry
17 changes: 11 additions & 6 deletions src/datadog/metrics.h → include/datadog/telemetry/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <vector>

namespace datadog {
namespace tracing {
namespace telemetry {

class Metric {
// The name of the metric that will be published. A transformation occurs
Expand All @@ -20,6 +20,8 @@ class Metric {
std::string name_;
// The type of the metric. This will currently be count or gauge.
std::string type_;
// Namespace of the metric.
std::string scope_;
// Tags associated with this specific instance of the metric.
std::vector<std::string> tags_;
// This affects the transformation of the metric name, where it can be a
Expand All @@ -29,14 +31,15 @@ class Metric {

protected:
std::atomic<uint64_t> value_ = 0;
Metric(std::string name, std::string type, std::vector<std::string> tags,
bool common);
Metric(std::string name, std::string type, std::string scope,
std::vector<std::string> tags, bool common);

public:
// Accessors for name, type, tags, common and capture_and_reset_value are used
// when producing the JSON message for reporting metrics.
std::string name();
std::string type();
std::string scope();
std::vector<std::string> tags();
bool common();
uint64_t value();
Expand All @@ -47,7 +50,8 @@ class Metric {
// number of actions, or incrementing the current number of actions by 1.
class CounterMetric : public Metric {
public:
CounterMetric(std::string name, std::vector<std::string> tags, bool common);
CounterMetric(std::string name, std::string scope,
std::vector<std::string> tags, bool common);
void inc();
void add(uint64_t amount);
};
Expand All @@ -57,13 +61,14 @@ class CounterMetric : public Metric {
// state by 1.
class GaugeMetric : public Metric {
public:
GaugeMetric(std::string name, std::vector<std::string> tags, bool common);
GaugeMetric(std::string name, std::string scope,
std::vector<std::string> tags, bool common);
void set(uint64_t value);
void inc();
void add(uint64_t amount);
void dec();
void sub(uint64_t amount);
};

} // namespace tracing
} // namespace telemetry
} // namespace datadog
35 changes: 35 additions & 0 deletions include/datadog/telemetry/telemetry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <datadog/logger.h>
#include <datadog/telemetry/configuration.h>
#include <datadog/telemetry/metrics.h>

#include <memory>
#include <vector>

namespace datadog {

namespace tracing {
class DatadogAgent;
class TracerTelemetry;
} // namespace tracing

namespace telemetry {

class Telemetry {
FinalizedConfiguration config_;
std::shared_ptr<tracing::Logger> logger_;

std::shared_ptr<tracing::DatadogAgent> datadog_agent_;
std::shared_ptr<tracing::TracerTelemetry> tracer_telemetry_;

public:
Telemetry(FinalizedConfiguration configuration,
std::shared_ptr<tracing::Logger> logger,
std::vector<std::shared_ptr<Metric>> metrics);

~Telemetry() = default;
};

} // namespace telemetry
} // namespace datadog
13 changes: 6 additions & 7 deletions include/datadog/tracer_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// `Tracer`. `Tracer` is instantiated with a `FinalizedTracerConfig`, which
// must be obtained from the result of a call to `finalize_config`.

#include <datadog/telemetry/configuration.h>

#include <cstddef>
#include <memory>
#include <variant>
Expand Down Expand Up @@ -73,12 +75,9 @@ struct TracerConfig {
// variable.
Optional<bool> report_traces;

// `report_telemetry` indicates whether telemetry about the tracer will be
// sent to a collector (`true`) or discarded on completion (`false`). If
// `report_telemetry` is `false`, then this feature is disabled.
// `report_telemetry` is overridden by the
// `DD_INSTRUMENTATION_TELEMETRY_ENABLED` environment variable.
Optional<bool> report_telemetry;
// `telemetry` configures the telemetry module. See
// `telemetry/configuration.h` By default, the telemetry module is enabled.
telemetry::Configuration telemetry;

// `delegate_trace_sampling` indicates whether the tracer will consult a child
// service for a trace sampling decision, and prefer the resulting decision
Expand Down Expand Up @@ -180,6 +179,7 @@ class FinalizedTracerConfig final {

FinalizedTraceSamplerConfig trace_sampler;
FinalizedSpanSamplerConfig span_sampler;
telemetry::FinalizedConfiguration telemetry;

std::vector<PropagationStyle> injection_styles;
std::vector<PropagationStyle> extraction_styles;
Expand All @@ -189,7 +189,6 @@ class FinalizedTracerConfig final {
std::shared_ptr<Logger> logger;
bool log_on_startup;
bool generate_128bit_trace_ids;
bool report_telemetry;
Optional<RuntimeID> runtime_id;
Clock clock;
std::string integration_name;
Expand Down
2 changes: 1 addition & 1 deletion src/datadog/datadog_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
#include <datadog/collector.h>
#include <datadog/event_scheduler.h>
#include <datadog/http_client.h>
#include <datadog/telemetry/metrics.h>
#include <datadog/tracer_signature.h>

#include <memory>
#include <mutex>
#include <vector>

#include "config_manager.h"
#include "metrics.h"
#include "remote_config/remote_config.h"
#include "tracer_telemetry.h"

Expand Down
Loading

0 comments on commit 963f78b

Please sign in to comment.