Skip to content

Commit ad10ac7

Browse files
committed
Send Laravel endpoints
1 parent 63d2d29 commit ad10ac7

File tree

5 files changed

+106
-30
lines changed

5 files changed

+106
-30
lines changed

ext/ddtrace.c

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,49 +2752,106 @@ zend_string *get_env() {
27522752
return get_DD_ENV();
27532753
}
27542754

2755+
ddog_Method dd_string_to_method(zend_string *method) {
2756+
if (zend_string_equals_literal(method, "GET")) {
2757+
return DDOG_METHOD_GET;
2758+
}
2759+
if (zend_string_equals_literal(method, "POST")) {
2760+
return DDOG_METHOD_POST;
2761+
}
2762+
if (zend_string_equals_literal(method, "PUT")) {
2763+
return DDOG_METHOD_PUT;
2764+
}
2765+
if (zend_string_equals_literal(method, "DELETE")) {
2766+
return DDOG_METHOD_DELETE;
2767+
}
2768+
if (zend_string_equals_literal(method, "PATCH")) {
2769+
return DDOG_METHOD_PATCH;
2770+
}
2771+
if (zend_string_equals_literal(method, "HEAD")) {
2772+
return DDOG_METHOD_HEAD;
2773+
}
2774+
if (zend_string_equals_literal(method, "OPTIONS")) {
2775+
return DDOG_METHOD_OPTIONS;
2776+
}
2777+
if (zend_string_equals_literal(method, "TRACE")) {
2778+
return DDOG_METHOD_TRACE;
2779+
}
2780+
if (zend_string_equals_literal(method, "CONNECT")) {
2781+
return DDOG_METHOD_CONNECT;
2782+
}
2783+
return DDOG_METHOD_OTHER;
2784+
}
2785+
27552786
PHP_FUNCTION(DDTrace_add_endpoint) {
27562787
UNUSED(execute_data);
2757-
zend_string *type = NULL;
27582788
zend_string *path = NULL;
27592789
zend_string *operation_name = NULL;
27602790
zend_string *resource_name = NULL;
2791+
zend_string *method = NULL;
2792+
zend_string *type = NULL;
27612793
zend_string *request_body_type = NULL;
27622794
zend_string *response_body_type = NULL;
27632795
zend_long response_code = 0;
27642796
zend_long authentication = 0;
2765-
zend_string *metadata = NULL;
2797+
zend_string *metadata_input = NULL;
27662798

2767-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SSSSSSllS", &type, &path, &operation_name, &resource_name, &request_body_type, &response_body_type,
2768-
&response_code, &authentication, &metadata) == FAILURE) {
2799+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SSSS|SSllS", &path, &operation_name, &resource_name, &method, &type, &request_body_type, &response_body_type,
2800+
&response_code, &authentication, &metadata_input) == FAILURE) {
27692801
RETURN_FALSE;
27702802
}
27712803

2804+
zend_string *default_metadata = NULL;
2805+
zend_string *metadata = metadata_input;
2806+
if (metadata == NULL) {
2807+
default_metadata = zend_string_init(ZEND_STRL("{}"), 0);
2808+
metadata = default_metadata;
2809+
}
2810+
27722811
ddog_CharSlice type_slice = dd_zend_string_to_CharSlice(type);
2773-
ddog_Method method_enum = DDOG_METHOD_GET;
2812+
ddog_Method method_enum = dd_string_to_method(method);
27742813
ddog_CharSlice path_slice = dd_zend_string_to_CharSlice(path);
27752814
ddog_CharSlice operation_name_slice = dd_zend_string_to_CharSlice(operation_name);
27762815
ddog_CharSlice resource_name_slice = dd_zend_string_to_CharSlice(resource_name);
2777-
struct ddog_Vec_CharSlice *request_body_type_vec = ddog_CharSlice_to_owned(dd_zend_string_to_CharSlice(request_body_type));
2778-
struct ddog_Vec_CharSlice *response_body_type_vec = ddog_CharSlice_to_owned(dd_zend_string_to_CharSlice(response_body_type));
2779-
struct ddog_Vec_Authentication *authentication_vec = ddog_number_to_owned_Authentication(authentication);
2816+
struct ddog_Vec_CharSlice *request_body_type_vec = NULL;
2817+
if (request_body_type) {
2818+
request_body_type_vec = ddog_CharSlice_to_owned(dd_zend_string_to_CharSlice(request_body_type));
2819+
}
2820+
struct ddog_Vec_CharSlice *response_body_type_vec = NULL;
2821+
if (response_body_type) {
2822+
response_body_type_vec = ddog_CharSlice_to_owned(dd_zend_string_to_CharSlice(response_body_type));
2823+
}
2824+
struct ddog_Vec_Authentication *authentication_vec = NULL;
2825+
if (authentication) {
2826+
authentication_vec = ddog_number_to_owned_Authentication(authentication);
2827+
}
27802828

2781-
size_t len = ZSTR_LEN(metadata);
2782-
zend_string *metadata_utf8 = NULL;
27832829
ddog_CharSlice metadata_slice = {0};
2784-
char *stripped_utf8 = ddtrace_strip_invalid_utf8(ZSTR_VAL(metadata), &len);
2785-
if (stripped_utf8 != NULL && len > 0) {
2786-
metadata_utf8 = zend_string_init(stripped_utf8, len, 0);
2787-
metadata_slice = dd_zend_string_to_CharSlice(metadata_utf8);
2830+
if (metadata && ZSTR_LEN(metadata) > 0) {
2831+
size_t len = ZSTR_LEN(metadata);
2832+
char *stripped_utf8 = ddtrace_strip_invalid_utf8(ZSTR_VAL(metadata), &len);
2833+
if (stripped_utf8 != NULL && len > 0) {
2834+
zend_string *metadata_utf8 = zend_string_init(stripped_utf8, len, 0);
2835+
metadata_slice = dd_zend_string_to_CharSlice(metadata_utf8);
2836+
ddtrace_drop_rust_string(stripped_utf8, len);
2837+
// metadata_utf8 will be released by Zend after function returns or at cleanup
2838+
} else {
2839+
metadata_slice = dd_zend_string_to_CharSlice(metadata);
2840+
}
27882841
}
27892842

27902843
if (!ddtrace_sidecar || !ddtrace_sidecar_instance_id || !DDTRACE_G(sidecar_queue_id)) {
27912844
RETURN_FALSE;
27922845
}
27932846

2794-
ddog_sidecar_telemetry_addEndpoint(&ddtrace_sidecar, ddtrace_sidecar_instance_id, &DDTRACE_G(sidecar_queue_id), type_slice, method_enum,
2847+
ddog_sidecar_telemetry_addEndpoint_buffer(&ddtrace_sidecar, ddtrace_sidecar_instance_id, &DDTRACE_G(sidecar_queue_id), type_slice, method_enum,
27952848
path_slice, operation_name_slice, resource_name_slice, request_body_type_vec, response_body_type_vec,
27962849
response_code, authentication_vec, metadata_slice);
27972850

2851+
if (!metadata_input) {
2852+
zend_string_release(default_metadata);
2853+
}
2854+
27982855
RETURN_TRUE;
27992856
}
28002857

ext/ddtrace.stub.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,18 @@ function are_endpoints_collected(): bool {}
833833
/**
834834
* Add an endpoint
835835
*
836-
* @param string $endpoint The endpoint to add
837-
*/
838-
function add_endpoint(string $type, string $path, string $operation_name, string $resource_name, string $body_type, string $response_type, int $response_code, int $authentication, string $metadata): bool {}
836+
* @param string $path The path of the endpoint
837+
* @param string $operation_name The operation name of the endpoint
838+
* @param string $resource_name The resource name of the endpoint
839+
* @param string $method The method of the endpoint
840+
* @param string $type The type of the endpoint
841+
* @param string $body_type The body type of the endpoint
842+
* @param string $response_type The response type of the endpoint
843+
* @param int $response_code The response code of the endpoint
844+
* @param int $authentication The authentication of the endpoint
845+
* @param string $metadata The metadata of the endpoint
846+
*/
847+
function add_endpoint(string $path, string $operation_name, string $resource_name, string $method, ?string $type = NULL, ?string $body_type = NULL, ?string $response_type = NULL, ?int $response_code = NULL, ?int $authentication = NULL, ?string $metadata = NULL): bool {}
839848
}
840849

841850
namespace DDTrace\System {

ext/ddtrace_arginfo.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 53e83c8bd04860fc3bc95d5a66b2af4cd63b5a3d */
2+
* Stub hash: 67e1cc5d41d7a0bd13f946b9c4ecd169f5414f78 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_DDTrace_trace_method, 0, 3, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, className, IS_STRING, 0)
@@ -168,16 +168,17 @@ ZEND_END_ARG_INFO()
168168
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_DDTrace_are_endpoints_collected, 0, 0, _IS_BOOL, 0)
169169
ZEND_END_ARG_INFO()
170170

171-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_DDTrace_add_endpoint, 0, 9, _IS_BOOL, 0)
172-
ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
171+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_DDTrace_add_endpoint, 0, 4, _IS_BOOL, 0)
173172
ZEND_ARG_TYPE_INFO(0, path, IS_STRING, 0)
174173
ZEND_ARG_TYPE_INFO(0, operation_name, IS_STRING, 0)
175174
ZEND_ARG_TYPE_INFO(0, resource_name, IS_STRING, 0)
176-
ZEND_ARG_TYPE_INFO(0, body_type, IS_STRING, 0)
177-
ZEND_ARG_TYPE_INFO(0, response_type, IS_STRING, 0)
178-
ZEND_ARG_TYPE_INFO(0, response_code, IS_LONG, 0)
179-
ZEND_ARG_TYPE_INFO(0, authentication, IS_LONG, 0)
180-
ZEND_ARG_TYPE_INFO(0, metadata, IS_STRING, 0)
175+
ZEND_ARG_TYPE_INFO(0, method, IS_STRING, 0)
176+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, type, IS_STRING, 1, "NULL")
177+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, body_type, IS_STRING, 1, "NULL")
178+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, response_type, IS_STRING, 1, "NULL")
179+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, response_code, IS_LONG, 1, "NULL")
180+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, authentication, IS_LONG, 1, "NULL")
181+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, metadata, IS_STRING, 1, "\'{}\'")
181182
ZEND_END_ARG_INFO()
182183

183184
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_DDTrace_System_container_id, 0, 0, IS_STRING, 1)

ext/sidecar.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ void ddtrace_sidecar_dogstatsd_set(zend_string *metric, zend_long value, zval *t
5050
bool ddtrace_alter_test_session_token(zval *old_value, zval *new_value, zend_string *new_str);
5151

5252
static inline ddog_CharSlice dd_zend_string_to_CharSlice(zend_string *str) {
53+
if (str == NULL) {
54+
return (ddog_CharSlice){ .len = 0, .ptr = NULL };
55+
}
5356
return (ddog_CharSlice){ .len = str->len, .ptr = str->val };
5457
}
5558

src/DDTrace/Integrations/Laravel/LaravelIntegration.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ function ($This, $scope, $args, $route) use ($integration) {
144144
}
145145
$rootSpan->meta[Tag::HTTP_METHOD] = $request->method();
146146
$rootSpan->meta[Tag::SPAN_KIND] = 'server';
147+
148+
if (!\DDTrace\are_endpoints_collected()) {
149+
$routeCollection = $This->getRoutes();
150+
foreach ($routeCollection as $value) {
151+
$path = $value->uri;
152+
$method = $value->methods[0] ?? '';
153+
$resourceName = $method . ' ' . $path;
154+
\DDTrace\add_endpoint($path, 'http.request', $resourceName, $method);
155+
}
156+
}
147157
}
148158
);
149159

@@ -566,10 +576,6 @@ function (HookData $hook) use ($integration) {
566576
}
567577
);
568578

569-
if (!\DDTrace\are_endpoints_collected()) {
570-
\DDTrace\add_endpoint("type", "/api/v1/traces", "operation_name", "resource_name", "body_type", "response_type", 1, 2, '{"some":"json"}');
571-
}
572-
573579
return Integration::LOADED;
574580
}
575581

0 commit comments

Comments
 (0)