Skip to content
Draft
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ cc_library(
"src/datadog/telemetry/log_binned_distr.h",
"src/datadog/telemetry/metric_context.h",
"src/datadog/telemetry/metric_handle.cpp",
"src/datadog/telemetry/status_tags.h",
"src/datadog/telemetry/telemetry.cpp",
"src/datadog/telemetry/telemetry_impl.cpp",
"src/datadog/telemetry/telemetry_impl.h",
Expand Down
63 changes: 0 additions & 63 deletions include/datadog/telemetry/telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,42 +151,6 @@ std::shared_ptr<CounterHandle<Tags>> create(
});
}

/// Increments the specified counter by 1.
///
/// @param `counter` the counter to increment.
void increment(const Counter& counter);

/// Increments the specified counter by 1.
///
/// @param `counter` the counter to increment.
/// @param `tags` the distribution tags.
void increment(const Counter& counter, const std::vector<std::string>& tags);

/// Decrements the specified counter by 1.
///
/// @param `counter` the counter to decrement.
void decrement(const Counter& counter);

/// Decrements the specified counter by 1.
///
/// @param `counter` the counter to decrement.
/// @param `tags` the distribution tags.
void decrement(const Counter& counter, const std::vector<std::string>& tags);

/// Sets the counter to a specific value.
///
/// @param `counter` the counter to update.
/// @param `value` the value to assign to the counter.
void set(const Counter& counter, uint64_t value);

/// Sets the counter to a specific value.
///
/// @param `counter` the counter to update.
/// @param `tags` the distribution tags.
/// @param `value` the value to assign to the counter.
void set(const Counter& counter, const std::vector<std::string>& tags,
uint64_t value);

} // namespace counter

/// The `rate` namespace provides support for rate metrics-values.
Expand All @@ -207,19 +171,6 @@ std::shared_ptr<RateHandle<Tags>> create(
});
}

/// Sets the rate to a specific value.
///
/// @param `rate` the rate to update.
/// @param `value` the value to assign to the counter.
void set(const Rate& rate, uint64_t value);

/// Sets the rate to a specific value.
///
/// @param `rate` the rate to update.
/// @param `tags` the distribution tags.
/// @param `value` the value to assign to the counter.
void set(const Rate& rate, const std::vector<std::string>&, uint64_t value);

} // namespace rate

/// The `distribution` namespace provides support for statistical distribution.
Expand All @@ -242,20 +193,6 @@ std::shared_ptr<DistributionHandle<Tags>> create(
});
}

/// Adds a value to the distribution.
///
/// @param `distribution` the distribution to update.
/// @param `value` the value to add to the distribution.
void add(const Distribution& distribution, uint64_t value);

/// Adds a value to the distribution.
///
/// @param `distribution` the distribution to update.
/// @param `tags` the distribution tags.
/// @param `value` the value to add to the distribution.
void add(const Distribution& distribution, const std::vector<std::string>& tags,
uint64_t value);

} // namespace distribution

} // namespace datadog::telemetry
41 changes: 14 additions & 27 deletions src/datadog/datadog_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,12 @@ void DatadogAgent::flush() {
auto encode_result = msgpack_encode(body, trace_chunks);
auto end = std::chrono::steady_clock::now();

telemetry::distribution::add(
metrics::tracer::trace_chunk_serialization_duration,
std::chrono::duration_cast<std::chrono::microseconds>(end - beg).count());
telemetry::distribution::add(metrics::tracer::trace_chunk_serialized_bytes,
static_cast<uint64_t>(body.size()));
metrics::tracer_metrics().trace_chunk_serialization_duration->record(
{}, static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::microseconds>(end - beg)
.count()));
metrics::tracer_metrics().trace_chunk_serialized_bytes->record(
{}, static_cast<std::uint64_t>(body.size()));

if (auto* error = encode_result.if_error()) {
logger_->log_error(*error);
Expand Down Expand Up @@ -295,21 +296,9 @@ void DatadogAgent::flush() {
logger = logger_](int response_status,
const DictReader& /*response_headers*/,
std::string response_body) {
if (response_status >= 500) {
telemetry::counter::increment(metrics::tracer::api::responses,
{"status_code:5xx"});
} else if (response_status >= 400) {
telemetry::counter::increment(metrics::tracer::api::responses,
{"status_code:4xx"});
} else if (response_status >= 300) {
telemetry::counter::increment(metrics::tracer::api::responses,
{"status_code:3xx"});
} else if (response_status >= 200) {
telemetry::counter::increment(metrics::tracer::api::responses,
{"status_code:2xx"});
} else if (response_status >= 100) {
telemetry::counter::increment(metrics::tracer::api::responses,
{"status_code:1xx"});
if (const auto tags =
telemetry::StatusTags::from_http_status(response_status)) {
metrics::tracer_metrics().api_responses->record(*tags, 1);
}
if (response_status != 200) {
logger->log_error([&](auto& stream) {
Expand Down Expand Up @@ -347,24 +336,22 @@ void DatadogAgent::flush() {
// request or retrieving the response. It's invoked
// asynchronously.
auto on_error = [logger = logger_](Error error) {
telemetry::counter::increment(metrics::tracer::api::errors,
{"type:network"});
metrics::tracer_metrics().api_errors->record({}, 1);
logger->log_error(error.with_prefix(
"Error occurred during HTTP request for submitting traces: "));
};

telemetry::counter::increment(metrics::tracer::api::requests);
telemetry::distribution::add(metrics::tracer::api::bytes_sent,
static_cast<uint64_t>(body.size()));
metrics::tracer_metrics().api_requests->record({}, 1);
metrics::tracer_metrics().api_bytes_sent->record(
{}, static_cast<std::uint64_t>(body.size()));

auto post_result =
http_client_->post(traces_endpoint_, std::move(set_request_headers),
std::move(body), std::move(on_response),
std::move(on_error), clock_().tick + request_timeout_);
if (auto* error = post_result.if_error()) {
// NOTE(@dmehala): `technical` is a better kind of errors.
telemetry::counter::increment(metrics::tracer::api::errors,
{"type:network"});
metrics::tracer_metrics().api_errors->record({}, 1);
logger_->log_error(
error->with_prefix("Unexpected error submitting traces: "));
}
Expand Down
6 changes: 6 additions & 0 deletions src/datadog/telemetry/log.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#include <datadog/optional.h>
#include <datadog/string_view.h>

#include <array>
#include <string>

namespace datadog::telemetry {

enum class LogLevel : char { ERROR, WARNING };

// The names of the `level` tag of the logs_created metric.
constexpr std::array<const char*, 2> tag_value_names(LogLevel) {
return {"error", "warning"};
}

struct LogMessage final {
std::string message;
LogLevel level;
Expand Down
44 changes: 44 additions & 0 deletions src/datadog/telemetry/status_tags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

// The `status_code` tag of the metrics that count HTTP responses.

#include <datadog/optional.h>
#include <datadog/telemetry/metric_handle.h>

#include <array>
#include <tuple>

namespace datadog::telemetry {

enum class StatusClass {
informational,
success,
redirection,
client_error,
server_error
};

constexpr std::array<const char*, 5> tag_value_names(StatusClass) {
return {"1xx", "2xx", "3xx", "4xx", "5xx"};
}

struct StatusTags {
StatusClass status_code = StatusClass::success;

// The class of an HTTP status, such as 4xx for 404. Empty for a status
// below 100.
static tracing::Optional<StatusTags> from_http_status(int status) {
if (status >= 500) return StatusTags{StatusClass::server_error};
if (status >= 400) return StatusTags{StatusClass::client_error};
if (status >= 300) return StatusTags{StatusClass::redirection};
if (status >= 200) return StatusTags{StatusClass::success};
if (status >= 100) return StatusTags{StatusClass::informational};
return tracing::nullopt;
}

static auto tag_fields() {
return std::tuple{tag("status_code", &StatusTags::status_code)};
}
};

} // namespace datadog::telemetry
112 changes: 0 additions & 112 deletions src/datadog/telemetry/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,116 +152,4 @@ void error(std::string message, std::string stacktrace) {
}
} // namespace log

namespace counter {
void increment(const Counter& counter) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->increment_counter(counter);
},
[](auto&&) {},
},
instance());
}

void increment(const Counter& counter, const std::vector<std::string>& tags) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->increment_counter(counter, tags);
},
[](auto&&) {},
},
instance());
}

void decrement(const Counter& counter) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->decrement_counter(counter);
},
[](auto&&) {},
},
instance());
}

void decrement(const Counter& counter, const std::vector<std::string>& tags) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->decrement_counter(counter, tags);
},
[](auto&&) {},
},
instance());
}

void set(const Counter& counter, uint64_t value) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_counter(counter, value);
},
[](auto&&) {},
},
instance());
}

void set(const Counter& counter, const std::vector<std::string>& tags,
uint64_t value) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_counter(counter, tags, value);
},
[](auto&&) {},
},
instance());
}

} // namespace counter

namespace rate {
void set(const Rate& rate, uint64_t value) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_rate(rate, value);
},
[](auto&&) {},
},
instance());
}

void set(const Rate& rate, const std::vector<std::string>& tags,
uint64_t value) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->set_rate(rate, tags, value);
},
[](auto&&) {},
},
instance());
}
} // namespace rate

namespace distribution {

void add(const Distribution& distribution, uint64_t value) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->add_datapoint(distribution, value);
},
[](auto&&) {},
},
instance());
}

void add(const Distribution& distribution, const std::vector<std::string>& tags,
uint64_t value) {
std::visit(details::Overload{
[&](std::shared_ptr<Telemetry>& telemetry) {
telemetry->add_datapoint(distribution, tags, value);
},
[](auto&&) {},
},
instance());
}

} // namespace distribution

} // namespace datadog::telemetry
Loading
Loading