-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathauto_flush.c
More file actions
167 lines (145 loc) · 6.02 KB
/
auto_flush.c
File metadata and controls
167 lines (145 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "auto_flush.h"
#include "asm_event.h"
#ifndef _WIN32
#include "comms_php.h"
#include "coms.h"
#endif
#include "ddtrace_string.h"
#include "configuration.h"
#include <components/log/log.h>
#include "serializer.h"
#include "span.h"
#include "sidecar.h"
#include "trace_source.h"
#include "ddshared.h"
#include "standalone_limiter.h"
#include <main/SAPI.h>
#include <components-rs/sidecar.h>
ZEND_EXTERN_MODULE_GLOBALS(ddtrace);
ZEND_RESULT_CODE ddtrace_flush_tracer(bool force_on_startup, bool collect_cycles, bool fast_shutdown) {
bool success = true;
ddog_TracesBytes *traces = ddog_get_traces();
if (collect_cycles) {
ddtrace_serialize_closed_spans_with_cycle(traces, fast_shutdown);
} else {
ddtrace_serialize_closed_spans(traces, fast_shutdown);
}
// Prevent traces from requests not executing any PHP code:
// PG(during_request_startup) will only be set to 0 upon execution of any PHP code.
// e.g. php-fpm call with uri pointing to non-existing file, fpm status page, ...
if (!force_on_startup && PG(during_request_startup)) {
ddog_free_traces(traces);
return SUCCESS;
}
if (!ddog_get_traces_size(traces)) {
ddog_free_traces(traces);
LOG(INFO, "No finished traces to be sent to the agent");
return SUCCESS;
}
size_t limit = get_global_DD_TRACE_AGENT_MAX_PAYLOAD_SIZE();
char *url = ddtrace_agent_url();
if (get_global_DD_TRACE_SIDECAR_TRACE_SENDER()) {
if (DDTRACE_G(sidecar)) {
ddog_SenderParameters parameters = {
.tracer_headers_tags = {
.container_id = ddtrace_get_container_id(),
.lang = DDOG_CHARSLICE_C_BARE("php"),
.lang_interpreter = (ddog_CharSlice) {.ptr = sapi_module.name, .len = strlen(sapi_module.name)},
.lang_vendor = DDOG_CHARSLICE_C_BARE(""),
.tracer_version = DDOG_CHARSLICE_C_BARE(PHP_DDTRACE_VERSION),
.lang_version = php_version_rt,
.client_computed_top_level = get_DD_TRACE_STATS_COMPUTATION_ENABLED(),
.client_computed_stats = !get_global_DD_APM_TRACING_ENABLED() || get_DD_TRACE_STATS_COMPUTATION_ENABLED(),
},
.transport = DDTRACE_G(sidecar),
.instance_id = ddtrace_sidecar_instance_id,
.limit = limit,
.n_requests = get_global_DD_TRACE_AGENT_FLUSH_AFTER_N_REQUESTS(),
.buffer_size = get_global_DD_TRACE_BUFFER_SIZE(),
.url = (ddog_CharSlice) {.ptr = url, .len = strlen(url)},
};
ddog_send_traces_to_sidecar(traces, ¶meters);
} else {
LOGEV(INFO, {
log("Skipping flushing trace as connection to sidecar failed");
});
}
} else {
#ifndef _WIN32
success = true;
size_t length = ddog_get_traces_size(traces);
for (size_t i = 0; i < length; i++) {
ddog_TraceBytes *trace = ddog_get_trace(traces, i);
ddog_CharSlice serialized_trace = ddog_serialize_trace_into_charslice(trace);
if (serialized_trace.len > 0) {
if (serialized_trace.len > limit) {
LOG(ERROR, "Agent request payload of %zu bytes exceeds configured %zu byte limit; dropping request", serialized_trace.len, limit);
success = false;
} else {
success = ddtrace_send_traces_via_thread(1, serialized_trace.ptr, serialized_trace.len);
if (success) {
LOGEV(INFO, {
log("Flushing trace of size %d to send-queue for %s", ddog_get_trace_size(trace), url);
});
}
dd_prepare_for_new_trace();
}
ddog_free_charslice(serialized_trace);
} else {
success = false;
}
}
#else
success = false;
#endif
}
free(url);
ddog_free_traces(traces);
return success ? SUCCESS : FAILURE;
}
DDTRACE_PUBLIC void ddtrace_close_all_spans_and_flush()
{
ddtrace_close_all_open_spans(true);
ddtrace_flush_tracer(true, true, false);
}
#define DEFAULT_UDS_PATH "/var/run/datadog/apm.socket"
char *ddtrace_agent_url(void) {
zend_string *url = get_global_DD_TRACE_AGENT_URL();
if (ZSTR_LEN(url) > 0) {
char *dup = zend_strndup(ZSTR_VAL(url), ZSTR_LEN(url) + 1);
// mess around with backslashes to support our test cases providing something like "file://C:\dir\test.out"
const char *fileprefix = "file://";
if (strncmp(ZSTR_VAL(url), fileprefix, strlen(fileprefix)) == 0 && strchr(ZSTR_VAL(url), '\\')) {
for (size_t i = strlen(fileprefix); i < ZSTR_LEN(url); ++i) {
if (dup[i] == '\\') {
dup[i] = '/';
}
}
}
return dup;
}
zend_string *hostname = get_global_DD_AGENT_HOST();
if (ZSTR_LEN(hostname) > 7 && strncmp(ZSTR_VAL(hostname), "unix://", 7) == 0) {
return zend_strndup(ZSTR_VAL(hostname), ZSTR_LEN(hostname));
}
if (ZSTR_LEN(hostname) > 0 && zai_config_memoized_entries[DDTRACE_CONFIG_DD_AGENT_HOST].name_index != ZAI_CONFIG_ORIGIN_DEFAULT) {
bool isIPv6 = memchr(ZSTR_VAL(hostname), ':', ZSTR_LEN(hostname));
int64_t port = get_global_DD_TRACE_AGENT_PORT();
if (port <= 0 || port > 65535) {
port = 8126;
}
char *formatted_url;
asprintf(&formatted_url, isIPv6 ? HOST_V6_FORMAT_STR : HOST_V4_FORMAT_STR, ZSTR_VAL(hostname), (uint32_t)port);
return formatted_url;
}
if (access(DEFAULT_UDS_PATH, F_OK) == SUCCESS) {
return zend_strndup(ZEND_STRL("unix://" DEFAULT_UDS_PATH));
}
int64_t port = get_global_DD_TRACE_AGENT_PORT();
if (port <= 0 || port > 65535) {
port = 8126;
}
char *formatted_url;
asprintf(&formatted_url, HOST_V4_FORMAT_STR, "localhost", (uint32_t)port);
return formatted_url;
}