Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for FastCGI protocol #1417

Merged
merged 7 commits into from
Dec 2, 2024
Merged

Add support for FastCGI protocol #1417

merged 7 commits into from
Dec 2, 2024

Conversation

grcevski
Copy link
Contributor

@grcevski grcevski commented Nov 28, 2024

This PR adds support for tracking the FastCGI protocol which is very popular with modern PHP applications. It's more less an extension to what we already do for other types of TCP requests, I just added a detector for it.

TODO:

  • Unit tests for reading the FastCGI request and response
  • Integration tests with PHP-FPM

Relates to #1395

I didn't want to put closes on the issue because I still need to ensure Beyla works with Unix sockets. If it's business as usual, then we can close the issue, if not I have to look into if it's possible at all.

Copy link

codecov bot commented Nov 28, 2024

Codecov Report

Attention: Patch coverage is 95.45455% with 5 lines in your changes missing coverage. Please review.

Project coverage is 81.07%. Comparing base (9efc255) to head (db9fa52).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
.../internal/ebpf/common/fast_cgi_detect_transform.go 95.09% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1417      +/-   ##
==========================================
+ Coverage   72.41%   81.07%   +8.66%     
==========================================
  Files         145      147       +2     
  Lines       14868    15033     +165     
==========================================
+ Hits        10766    12188    +1422     
+ Misses       3389     2257    -1132     
+ Partials      713      588     -125     
Flag Coverage Δ
integration-test 59.21% <84.54%> (+0.07%) ⬆️
k8s-integration-test 59.65% <0.00%> (-0.38%) ⬇️
oats-test 33.74% <9.09%> (-0.15%) ⬇️
unittests 51.60% <57.27%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@grcevski grcevski marked this pull request as ready for review November 29, 2024 18:43
Copy link
Contributor

@rafaelroquetto rafaelroquetto left a comment

Choose a reason for hiding this comment

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

LGTM - just a few suggestions at your discretion

Comment on lines +89 to +91
//unsigned char tp_buf[TP_MAX_VAL_LENGTH];
//make_tp_string(tp_buf, &tp_p->tp);
//bpf_dbg_printk("tp: %s", tp_buf);
Copy link
Contributor

Choose a reason for hiding this comment

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

And these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same, that's expensive but when I debug traceparent issues, I keep needing to write the code again.

Comment on lines +73 to +74
//bpf_dbg_printk("Looking up existing trace for connection");
//dbg_print_http_connection_info(conn);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just double checking you intended to leave those in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it's good for the future if we need to debug the code.

//make_tp_string(tp_buf, &tp_p->tp);
//bpf_dbg_printk("tp: %s", tp_buf);

#ifdef BPF_TRACEPARENT
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are moving this around, may I suggest the following?

// at the top of this file or some common header

#ifdef BPF_TRACEPARENT
enum { PARSE_TRACEPARENT = 1 };
#else
enum { PARSE_TRACEPARENT = 0 };
#endif 

// then here

if (PARSE_TRACEPARENT) { ... }

the rationale is that this ensures this code is always sieved through the compiler, thus allowing it to catch any compilation error even when the macro is used. The code will not be present in the final binary when PARSE_TRACEPARENT is 0 because the compiler will optimise it out anyway, so best of two worlds and less error-prone.

Another unrelated to the above thought: should this happen before the find_trace_for_xxx(...) code? This snippet seems to run irrespective of found_tp, so perhaps moving it before and bailing earlier if tp is found makes sense?

Or would it even make sense to only run this if found_tp == false, or is this some sort of unconditional fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think the compilation is an issue, since we built all versions when compiling. Also the found_tp is not considered, because if we find something encoded in headers we don't care if we found something else before.

@@ -94,15 +119,15 @@ static __always_inline u8 correlated_request_with_current(tp_info_pid_t *existin
return 0;
}

u64 pid_tid = bpf_get_current_pid_tgid();
//u64 pid_tid = bpf_get_current_pid_tgid();
Copy link
Contributor

Choose a reason for hiding this comment

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

just noting those here in case they were not intended

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I removed the pid checking, I'll clean up this code in the next round of work I'm doing. I moved the type to be in the key, so now we don't have to worry about the pid.

@grcevski grcevski merged commit f517939 into grafana:main Dec 2, 2024
15 checks passed
@grcevski grcevski deleted the php-fpm branch December 2, 2024 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants