Skip to content

Commit 5d6e7f3

Browse files
committed
send basic stacktrace
1 parent cf77058 commit 5d6e7f3

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/datadog_handler.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
#include <datadog/telemetry/telemetry.h>
44

5+
#include <exception>
6+
57
#include "datadog_context.h"
68
#include "ngx_http_datadog_module.h"
9+
#include "telemetry_util.h"
710

811
extern "C" {
912
#include <ngx_config.h>
@@ -29,8 +32,6 @@ static bool is_datadog_tracing_enabled(
2932
return loc_conf->enable_tracing && core_loc_conf->log_subrequest;
3033
}
3134
}
32-
#define CURRENT_FRAME \
33-
std::format("{}:{} in {}", __FILE__, __LINE__, __PRETTY_FUNCTION__)
3435

3536
ngx_int_t on_enter_block(ngx_http_request_t *request) noexcept try {
3637
auto core_loc_conf = static_cast<ngx_http_core_loc_conf_t *>(
@@ -54,7 +55,7 @@ ngx_int_t on_enter_block(ngx_http_request_t *request) noexcept try {
5455
try {
5556
context->on_change_block(request, core_loc_conf, loc_conf);
5657
} catch (const std::exception &e) {
57-
telemetry::report_error_log(e.what());
58+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
5859
// The DatadogContext may be broken, destroy it so that we don't
5960
// attempt to continue tracing.
6061
destroy_datadog_context(request);
@@ -63,7 +64,7 @@ ngx_int_t on_enter_block(ngx_http_request_t *request) noexcept try {
6364
}
6465
return NGX_DECLINED;
6566
} catch (const std::exception &e) {
66-
telemetry::report_error_log(e.what());
67+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
6768
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
6869
"Datadog instrumentation failed for request %p: %s", request,
6970
e.what());
@@ -86,7 +87,7 @@ ngx_int_t on_access(ngx_http_request_t *request) noexcept try {
8687
}
8788
return NGX_DECLINED;
8889
} catch (const std::exception &e) {
89-
telemetry::report_error_log(e.what());
90+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
9091
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
9192
"Datadog instrumentation failed for request %p: %s", request,
9293
e.what());
@@ -100,7 +101,7 @@ ngx_int_t on_log_request(ngx_http_request_t *request) noexcept {
100101
try {
101102
context->on_log_request(request);
102103
} catch (const std::exception &e) {
103-
telemetry::report_error_log(e.what());
104+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
104105
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
105106
"Datadog instrumentation failed for request %p: %s", request,
106107
e.what());
@@ -117,7 +118,7 @@ ngx_int_t on_header_filter(ngx_http_request_t *request) noexcept {
117118
try {
118119
return context->on_header_filter(request);
119120
} catch (const std::exception &e) {
120-
telemetry::report_error_log(e.what());
121+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
121122
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
122123
"Datadog instrumentation failed for request %p: %s", request,
123124
e.what());
@@ -142,7 +143,7 @@ ngx_int_t request_body_filter(ngx_http_request_t *request,
142143
try {
143144
return context->request_body_filter(request, chain);
144145
} catch (const std::exception &e) {
145-
telemetry::report_error_log(e.what());
146+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
146147
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
147148
"Datadog instrumentation failed in request body filter for "
148149
"request %p: %s",
@@ -166,7 +167,7 @@ ngx_int_t on_output_body_filter(ngx_http_request_t *request,
166167
try {
167168
return context->on_output_body_filter(request, chain);
168169
} catch (const std::exception &e) {
169-
telemetry::report_error_log(e.what());
170+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
170171
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
171172
"Datadog instrumentation failed for request %p: %s", request,
172173
e.what());

src/datadog_variable.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "global_tracer.h"
1313
#include "ngx_http_datadog_module.h"
1414
#include "string_util.h"
15+
#include "telemetry_util.h"
1516
#include "tracing_library.h"
1617

1718
namespace datadog {
@@ -63,7 +64,7 @@ static ngx_int_t expand_span_variable(ngx_http_request_t* request,
6364

6465
return NGX_OK;
6566
} catch (const std::exception& e) {
66-
telemetry::report_error_log(e.what());
67+
telemetry::report_error_log(e.what(), CURRENT_FRAME(request));
6768
ngx_log_error(NGX_LOG_ERR, request->connection->log, 0,
6869
"failed to expand %V"
6970
" for request %p: %s",

src/telemetry_util.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#pragma once
2+
3+
#include <format>
4+
#include <string_view>
5+
6+
#include "string_util.h"
7+
8+
extern "C" {
9+
#include <ngx_http.h>
10+
}
11+
12+
namespace datadog::nginx {
13+
14+
constexpr std::string_view relative_filepath(
15+
std::string_view absolute_filepath) {
16+
constexpr std::string_view prefix = "nginx-datadog";
17+
size_t pos = absolute_filepath.find(prefix);
18+
return pos != std::string_view::npos ? absolute_filepath.substr(pos)
19+
: absolute_filepath;
20+
}
21+
22+
inline std::string make_current_frame(ngx_http_request_t *request,
23+
std::string_view file, size_t line,
24+
std::string_view function) {
25+
// clang-format off
26+
return std::format("Exception catched:\n"
27+
" at {} ({}:{})\n"
28+
" at {}", function, file, line,
29+
to_string_view(request->uri));
30+
// clang-format on
31+
}
32+
33+
} // namespace datadog::nginx
34+
35+
#define CURRENT_FRAME(request) \
36+
datadog::nginx::make_current_frame( \
37+
request, datadog::nginx::relative_filepath(__FILE__), __LINE__, \
38+
__FUNCTION__)

0 commit comments

Comments
 (0)