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
9 changes: 9 additions & 0 deletions include/ddog_profiling_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#pragma once

extern "C" {
#include "datadog/common.h"
#include "datadog/profiling.h"
}

#include "defer.hpp"
#include "string_view.hpp"
#include <string_view>

Expand All @@ -19,3 +21,10 @@ inline ddog_CharSlice to_CharSlice(std::string_view str) {
inline ddog_CharSlice to_CharSlice(string_view slice) {
return {.ptr = slice.ptr, .len = slice.len};
}

inline void log_warn_and_drop_error(std::string_view message,
ddog_Error *error) {
defer { ddog_Error_drop(error); };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use defer here ?

ddog_CharSlice char_slice = ddog_Error_message(error);
LG_WRN("%s (%.*s)", message.data(), (int)char_slice.len, char_slice.ptr);
}
22 changes: 10 additions & 12 deletions src/exporter/ddprof_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ static DDRes add_single_tag(ddog_Vec_Tag &tags_exporter, std::string_view key,
ddog_Vec_Tag_PushResult push_tag_res =
ddog_Vec_Tag_push(&tags_exporter, to_CharSlice(key), to_CharSlice(value));
if (push_tag_res.tag == DDOG_VEC_TAG_PUSH_RESULT_ERR) {
defer { ddog_Error_drop(&push_tag_res.err); };

LG_ERR("[EXPORTER] Failure generate tag (%.*s)",
(int)push_tag_res.err.message.len, push_tag_res.err.message.ptr);
log_warn_and_drop_error("[EXPORTER] Failure generate tag",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't the error message be logged twice ?
What is the benefit of using log_warn_and_drop_error ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was not using ddog_Error_message. The aim is to have a function you can always call in case of error and not think too hard about what to do.
Ideally it would handle the error returns with it, though that behavior is different across the files.

&push_tag_res.err);
DDRES_RETURN_ERROR_LOG(DD_WHAT_EXPORTER, "Failed to generate tags");
}
return ddres_init();
Expand Down Expand Up @@ -240,7 +238,8 @@ DDRes ddprof_exporter_new(const UserTags *user_tags, DDProfExporter *exporter) {
if (res_exporter.tag == DDOG_PROF_EXPORTER_NEW_RESULT_OK) {
exporter->_exporter = res_exporter.ok;
} else {
defer { ddog_Error_drop(&res_exporter.err); };
log_warn_and_drop_error("[EXPORTER] Failure form ddog_prof_Exporter_new",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double log to be fixed

&res_exporter.err);
DDRES_RETURN_ERROR_LOG(DD_WHAT_EXPORTER, "Failure creating exporter - %s",
res_exporter.err.message.ptr);
}
Expand Down Expand Up @@ -294,7 +293,8 @@ DDRes ddprof_exporter_export(const ddog_prof_Profile *profile,
ddog_prof_Profile_SerializeResult serialized_result =
ddog_prof_Profile_serialize(profile, nullptr, nullptr);
if (serialized_result.tag != DDOG_PROF_PROFILE_SERIALIZE_RESULT_OK) {
defer { ddog_Error_drop(&serialized_result.err); };
log_warn_and_drop_error("[EXPORTER] Failure in ddog_prof_Profile_serialize",
&serialized_result.err);
DDRES_RETURN_ERROR_LOG(DD_WHAT_EXPORTER, "Failed to serialize: %s",
serialized_result.err.message.ptr);
}
Expand Down Expand Up @@ -349,10 +349,9 @@ DDRes ddprof_exporter_export(const ddog_prof_Profile *profile,
ddog_prof_Exporter_send(exporter->_exporter, &request, nullptr);

if (result.tag == DDOG_PROF_EXPORTER_SEND_RESULT_ERR) {
defer { ddog_Error_drop(&result.err); };
LG_WRN("Failure to establish connection, check url %s", exporter->_url);
LG_WRN("Failure to send profiles (%.*s)", (int)result.err.message.len,
result.err.message.ptr);
log_warn_and_drop_error("[EXPORTER] Failure to send profiles",
&result.err);
// Free error buffer (prefer this API to the free API)
if (exporter->_nb_consecutive_errors++ >=
K_NB_CONSECUTIVE_ERRORS_ALLOWED) {
Expand All @@ -367,9 +366,8 @@ DDRes ddprof_exporter_export(const ddog_prof_Profile *profile,
res = check_send_response_code(result.http_response.code);
}
} else {
defer { ddog_Error_drop(&res_request.err); };
LG_ERR("[EXPORTER] Failure to build request: %s",
res_request.err.message.ptr);
log_warn_and_drop_error("[EXPORTER] Failure to build request",
&res_request.err);
res = ddres_error(DD_WHAT_EXPORTER);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/pprof/ddprof_pprof.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ DDRes pprof_create_profile(DDProfPProf *pprof, DDProfContext *ctx) {
const char *value_name = sample_type_name_from_idx(i);
const char *value_unit = sample_type_unit_from_idx(i);
if (!value_name || !value_unit) {
LG_WRN("Malformed sample type (%d), ignoring", i);
LG_WRN("[PPROF] Malformed sample type (%d), ignoring", i);
continue;
}
perf_value_type[num_sample_type_ids].type_ = to_CharSlice(value_name);
Expand Down Expand Up @@ -112,7 +112,7 @@ DDRes pprof_create_profile(DDProfPProf *pprof, DDProfContext *ctx) {
pprof->_profile = ddog_prof_Profile_new(
sample_types, num_sample_type_ids > 0 ? &period : nullptr, nullptr);
if (!pprof->_profile) {
DDRES_RETURN_ERROR_LOG(DD_WHAT_PPROF, "Unable to create profile");
DDRES_RETURN_ERROR_LOG(DD_WHAT_PPROF, "[PPROF] Unable to create profile");
}

// Add relevant tags
Expand Down Expand Up @@ -255,8 +255,9 @@ DDRes pprof_aggregate(const UnwindOutput *uw_output,
// uint64_t id_sample = ddog_prof_Profile_add(profile, sample);
ddog_prof_Profile_AddResult add_res = ddog_prof_Profile_add(profile, sample);
if (add_res.tag == DDOG_PROF_PROFILE_ADD_RESULT_ERR) {
defer { ddog_Error_drop(&add_res.err); };
DDRES_RETURN_ERROR_LOG(DD_WHAT_PPROF, "Unable to add profile: %s",
log_warn_and_drop_error("[PPROF] Error from ddog_prof_Profile_add",
&add_res.err);
DDRES_RETURN_ERROR_LOG(DD_WHAT_PPROF, "[PPROF] Unable to add profile: %s",
add_res.err.message.ptr);
}

Expand All @@ -265,7 +266,7 @@ DDRes pprof_aggregate(const UnwindOutput *uw_output,

DDRes pprof_reset(DDProfPProf *pprof) {
if (!ddog_prof_Profile_reset(pprof->_profile, nullptr)) {
DDRES_RETURN_ERROR_LOG(DD_WHAT_PPROF, "Unable to reset profile");
DDRES_RETURN_ERROR_LOG(DD_WHAT_PPROF, "[PPROF] Unable to reset profile");
}
return ddres_init();
}
Expand Down