From 8b3fc49b2840793fae15a370561e62c3b06669e1 Mon Sep 17 00:00:00 2001 From: r1viollet Date: Thu, 13 Apr 2023 09:17:02 +0200 Subject: [PATCH] Minor refactoring - libddprof helper Introduce a helper to cleanup the libddprof errors --- include/ddog_profiling_utils.hpp | 9 +++++++++ src/exporter/ddprof_exporter.cc | 22 ++++++++++------------ src/pprof/ddprof_pprof.cc | 11 ++++++----- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/include/ddog_profiling_utils.hpp b/include/ddog_profiling_utils.hpp index d9b5b1d30..fc861c916 100644 --- a/include/ddog_profiling_utils.hpp +++ b/include/ddog_profiling_utils.hpp @@ -6,9 +6,11 @@ #pragma once extern "C" { +#include "datadog/common.h" #include "datadog/profiling.h" } +#include "defer.hpp" #include "string_view.hpp" #include @@ -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); }; + ddog_CharSlice char_slice = ddog_Error_message(error); + LG_WRN("%s (%.*s)", message.data(), (int)char_slice.len, char_slice.ptr); +} diff --git a/src/exporter/ddprof_exporter.cc b/src/exporter/ddprof_exporter.cc index 54870b0ee..74708556c 100644 --- a/src/exporter/ddprof_exporter.cc +++ b/src/exporter/ddprof_exporter.cc @@ -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", + &push_tag_res.err); DDRES_RETURN_ERROR_LOG(DD_WHAT_EXPORTER, "Failed to generate tags"); } return ddres_init(); @@ -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", + &res_exporter.err); DDRES_RETURN_ERROR_LOG(DD_WHAT_EXPORTER, "Failure creating exporter - %s", res_exporter.err.message.ptr); } @@ -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); } @@ -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) { @@ -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); } } diff --git a/src/pprof/ddprof_pprof.cc b/src/pprof/ddprof_pprof.cc index d2306f3eb..62095dcaa 100644 --- a/src/pprof/ddprof_pprof.cc +++ b/src/pprof/ddprof_pprof.cc @@ -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); @@ -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 @@ -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); } @@ -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(); }