Skip to content

Replace net/http Request.pat.str with Request.Pattern #2090

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

Merged
merged 7 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
### Changed

- Upgrade OpenTelemetry semantic conventions to `v1.30.0`. ([#2032](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/2032))
- Modify how the pattern is fetch from `net/http.Request`. Now it uses `Request.Pattern` instead of `Request.pat.str` ([#2090](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/2090)) unless it's go1.22, which continue using `Request.pat.str`.

### Removed

Expand Down
83 changes: 83 additions & 0 deletions internal/pkg/inject/offset_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -5475,6 +5475,89 @@
}
]
},
{
"field": "Pattern",
"offsets": [
{
"offset": null,
"versions": [
"1.19.0",
"1.19.1",
"1.19.2",
"1.19.3",
"1.19.4",
"1.19.5",
"1.19.6",
"1.19.7",
"1.19.8",
"1.19.9",
"1.19.10",
"1.19.11",
"1.19.12",
"1.19.13",
"1.20.0",
"1.20.1",
"1.20.2",
"1.20.3",
"1.20.4",
"1.20.5",
"1.20.6",
"1.20.7",
"1.20.8",
"1.20.9",
"1.20.10",
"1.20.11",
"1.20.12",
"1.20.13",
"1.20.14",
"1.21.0",
"1.21.1",
"1.21.2",
"1.21.3",
"1.21.4",
"1.21.5",
"1.21.6",
"1.21.7",
"1.21.8",
"1.21.9",
"1.21.10",
"1.21.11",
"1.21.12",
"1.21.13",
"1.22.0",
"1.22.1",
"1.22.2",
"1.22.3",
"1.22.4",
"1.22.5",
"1.22.6",
"1.22.7",
"1.22.8",
"1.22.9",
"1.22.10",
"1.22.11",
"1.22.12"
]
},
{
"offset": 232,
"versions": [
"1.23.0",
"1.23.1",
"1.23.2",
"1.23.3",
"1.23.4",
"1.23.5",
"1.23.6",
"1.23.7",
"1.23.8",
"1.24.0",
"1.24.1",
"1.24.2"
]
}
]
},
{
"field": "Proto",
"offsets": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ volatile const u64 host_pos;
volatile const u64 proto_pos;

// A flag indicating whether pattern handlers are supported
volatile const bool pattern_path_public_supported;
// A flag indicating whether pattern handlers are supported via the private field
volatile const bool pattern_path_supported;
// In case pattern handlers are supported the following offsets will be used:
volatile const u64 req_pattern_pos;
volatile const u64 req_pat_pos;
volatile const u64 pat_str_pos;
// A flag indicating whether the Go version is using swiss maps
Expand Down Expand Up @@ -296,7 +299,9 @@ int uprobe_serverHandler_ServeHTTP_Returns(struct pt_regs *ctx) {
bpf_probe_read(&url_ptr, sizeof(url_ptr), (void *)(req_ptr + url_ptr_pos));
// Collect fields from response
read_go_string(req_ptr, method_ptr_pos, http_server_span->method, sizeof(http_server_span->method), "method from request");
if (pattern_path_supported) {
if (pattern_path_public_supported) {
read_go_string(req_ptr, req_pattern_pos, http_server_span->path_pattern, sizeof(http_server_span->path_pattern), "pattern from Request");
} else if (pattern_path_supported) {
void *pat_ptr = NULL;
bpf_probe_read(&pat_ptr, sizeof(pat_ptr), (void *)(req_ptr + req_pat_pos));
if (pat_ptr != NULL) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 42 additions & 38 deletions internal/pkg/instrumentation/bpf/net/http/server/bpf_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion internal/pkg/instrumentation/bpf/net/http/server/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func New(logger *slog.Logger, version string) probe.Probe {
Key: "proto_pos",
ID: structfield.NewID("std", "net/http", "Request", "Proto"),
},
probe.StructFieldConstMinVersion{
StructField: probe.StructFieldConst{
Key: "req_pattern_pos",
ID: structfield.NewID("std", "net/http", "Request", "Pattern"),
},
MinVersion: patternPathPublicMinVersion,
},
probe.StructFieldConstMinVersion{
StructField: probe.StructFieldConst{
Key: "req_pat_pos",
Expand All @@ -119,6 +126,7 @@ func New(logger *slog.Logger, version string) probe.Probe {
},
MinVersion: patternPathMinVersion,
},
patternPathPublicSupportedConst{},
patternPathSupportedConst{},
swissMapsUsedConst{},
},
Expand All @@ -145,6 +153,18 @@ func New(logger *slog.Logger, version string) probe.Probe {
}
}

type patternPathPublicSupportedConst struct{}

var (
patternPathPublicMinVersion = semver.New(1, 23, 0, "", "")
isPatternPathPublicSupported = false
)

func (c patternPathPublicSupportedConst) InjectOption(info *process.Info) (inject.Option, error) {
isPatternPathPublicSupported = info.GoVersion.GreaterThanEqual(patternPathPublicMinVersion)
return inject.WithKeyValue("pattern_path_public_supported", isPatternPathPublicSupported), nil
}

type patternPathSupportedConst struct{}

var (
Expand Down Expand Up @@ -232,7 +252,7 @@ func processFn(e *event) ptrace.SpanSlice {
}

spanName := method
if isPatternPathSupported && isValidPatternPath {
if (isPatternPathPublicSupported || isPatternPathSupported) && isValidPatternPath {
spanName = spanName + " " + patternPath
attrs = append(attrs, semconv.HTTPRouteKey.String(patternPath))
}
Expand Down
1 change: 1 addition & 0 deletions internal/tools/inspect/cmd/offsetgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func manifests() ([]inspect.Manifest, error) {
structfield.NewID("std", "net/http", "Request", "Proto"),
structfield.NewID("std", "net/http", "Request", "RequestURI"),
structfield.NewID("std", "net/http", "Request", "Host"),
structfield.NewID("std", "net/http", "Request", "Pattern"),
structfield.NewID("std", "net/http", "Request", "pat"),
structfield.NewID("std", "net/http", "pattern", "str"),
structfield.NewID("std", "net/url", "URL", "Path"),
Expand Down
Loading