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

feat: Support http.route attribute for gin and chi #1359

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -16,6 +16,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http

- Add support for span attribute limits to `go.opentelemtry.io/auto/sdk`. ([#1315](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1315))
- Add support for span link limits to `go.opentelemtry.io/auto/sdk`. ([#1320](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1320))
- Support `http.route` attribute for `gin` and `chi`. ([#1359](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1359))

## [v0.18.0-alpha] - 2024-11-20

Expand Down
203 changes: 203 additions & 0 deletions internal/pkg/inject/offset_results.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,207 @@
[
{
"module": "github.com/gin-gonic/gin",
"packages": [
{
"package": "github.com/gin-gonic/gin",
"structs": [
{
"struct": "Context",
"fields": [
{
"field": "Request",
"offsets": [
{
"offset": null,
"versions": [
"1.7.5"
]
},
{
"offset": 32,
"versions": [
"1.1.1",
"1.1.2",
"1.1.3",
"1.1.4",
"1.3.0",
"1.4.0",
"1.5.0",
"1.6.0",
"1.6.1",
"1.6.2",
"1.6.3",
"1.7.0",
"1.7.1",
"1.7.2",
"1.7.3",
"1.7.4",
"1.7.6",
"1.7.7",
"1.8.0",
"1.8.1",
"1.8.2",
"1.9.0",
"1.9.1",
"1.10.0"
]
}
]
},
{
"field": "fullPath",
"offsets": [
{
"offset": null,
"versions": [
"1.1.1",
"1.1.2",
"1.1.3",
"1.1.4",
"1.3.0",
"1.4.0",
"1.7.5"
]
},
{
"offset": 112,
"versions": [
"1.5.0",
"1.6.0",
"1.6.1",
"1.6.2",
"1.6.3",
"1.7.0",
"1.7.1",
"1.7.2",
"1.7.3",
"1.7.4",
"1.7.6",
"1.7.7",
"1.8.0",
"1.8.1",
"1.8.2",
"1.9.0",
"1.9.1",
"1.10.0"
]
}
]
}
]
}
]
}
]
},
{
"module": "github.com/go-chi/chi/v5",
"packages": [
{
"package": "github.com/go-chi/chi/v5",
"structs": [
{
"struct": "Context",
"fields": [
{
"field": "RouteMethod",
"offsets": [
{
"offset": 32,
"versions": [
"5.0.0"
]
},
{
"offset": 48,
"versions": [
"5.0.1",
"5.0.2",
"5.0.3",
"5.0.4",
"5.0.5",
"5.0.6",
"5.0.7",
"5.0.8",
"5.0.9",
"5.0.10",
"5.0.11",
"5.0.12",
"5.0.13",
"5.0.14",
"5.1.0"
]
}
]
},
{
"field": "parentCtx",
"offsets": [
{
"offset": 16,
"versions": [
"5.0.1",
"5.0.2",
"5.0.3",
"5.0.4",
"5.0.5",
"5.0.6",
"5.0.7",
"5.0.8",
"5.0.9",
"5.0.10",
"5.0.11",
"5.0.12",
"5.0.13",
"5.0.14",
"5.1.0"
]
},
{
"offset": 192,
"versions": [
"5.0.0"
]
}
]
},
{
"field": "routePattern",
"offsets": [
{
"offset": 120,
"versions": [
"5.0.0"
]
},
{
"offset": 160,
"versions": [
"5.0.1",
"5.0.2",
"5.0.3",
"5.0.4",
"5.0.5",
"5.0.6",
"5.0.7",
"5.0.8",
"5.0.9",
"5.0.10",
"5.0.11",
"5.0.12",
"5.0.13",
"5.0.14",
"5.1.0"
]
}
]
}
]
}
]
}
]
},
{
"module": "github.com/segmentio/kafka-go",
"packages": [
Expand Down
137 changes: 137 additions & 0 deletions internal/pkg/instrumentation/bpf/github.com/chi/bpf/probe.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "arguments.h"
#include "trace/span_context.h"
#include "go_context.h"
#include "go_types.h"
#include "uprobe.h"
#include "trace/span_output.h"
#include "trace/start_span.h"

char __license[] SEC("license") = "Dual MIT/GPL";

#define PATH_MAX_LEN 128
#define METHOD_MAX_LEN 8
#define MAX_CONCURRENT 50

struct http_request_t {
BASE_SPAN_PROPERTIES
char method[METHOD_MAX_LEN];
char path[PATH_MAX_LEN];
char path_pattern[PATH_MAX_LEN];
};

struct uprobe_data_t
{
struct http_request_t req;
u64 rctx_ptr;
};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, void *);
__type(value, struct uprobe_data_t);
__uint(max_entries, MAX_CONCURRENT);
} http_events SEC(".maps");

struct
{
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(key_size, sizeof(u32));
__uint(value_size, sizeof(struct uprobe_data_t));
__uint(max_entries, 1);
} chi_uprobe_storage_map SEC(".maps");

// Injected in init
volatile const u64 pctx_ptr_pos;
volatile const u64 rp_str_pos;
volatile const u64 method_str_pos;

// This instrumentation attaches uprobe to the following function:
// func (n *node) FindRoute(rctx *Context, method methodTyp, path string) (*node, endpoints, http.Handler)
SEC("uprobe/chi_node_FindRoute")
int uprobe_chi_node_FindRoute(struct pt_regs *ctx) {
u64 rctx_pos = 2;
void *rctx_ptr = get_argument(ctx, rctx_pos);

struct go_iface go_context = {0};
get_Go_context(ctx, rctx_pos, pctx_ptr_pos, false, &go_context);

void *key = get_consistent_key(ctx, go_context.data);

u32 map_id = 0;
struct uprobe_data_t *uprobe_data = bpf_map_lookup_elem(&chi_uprobe_storage_map, &map_id);
if (uprobe_data == NULL)
{
bpf_printk("uprobe/chi_node_FindRoute: http_server_span is NULL");
return 0;
}

__builtin_memset(uprobe_data, 0, sizeof(struct uprobe_data_t));

// Save Request
uprobe_data->rctx_ptr = (u64)rctx_ptr;

struct http_request_t *http_request = &uprobe_data->req;
http_request->start_time = bpf_ktime_get_ns();

start_span_params_t start_span_params = {
.ctx = ctx,
.go_context = &go_context,
.psc = &http_request->psc,
.sc = &http_request->sc,
.get_parent_span_context_fn = NULL,
.get_parent_span_context_arg = NULL,
};
start_span(&start_span_params);

// get path from 3rd arg
void *path_str_ptr = get_argument(ctx, 4);
u64 path_str_len = (u64)get_argument(ctx, 5);
const u64 path_str_size = sizeof(http_request->path) < path_str_len ? sizeof(http_request->path) : path_str_len;
bpf_probe_read(http_request->path, path_str_size, path_str_ptr);

bpf_map_update_elem(&http_events, &key, uprobe_data, 0);
start_tracking_span(go_context.data, &http_request->sc);
return 0;
}

SEC("uprobe/chi_node_FindRoute")
int uprobe_chi_node_FindRoute_Returns(struct pt_regs *ctx) {
u64 end_time = bpf_ktime_get_ns();

struct go_iface go_context = {0};
get_Go_context(ctx, 2, pctx_ptr_pos, false, &go_context);

void *key = get_consistent_key(ctx, go_context.data);

struct uprobe_data_t *uprobe_data = bpf_map_lookup_elem(&http_events, &key);
if (uprobe_data == NULL) {
bpf_printk("uprobe/chi_node_FindRoute: entry_state is NULL");
return 0;
}

struct http_request_t *http_request = &uprobe_data->req;
http_request->end_time = end_time;

void *rctx_ptr = (void *)uprobe_data->rctx_ptr;

// Get method from rctx
if (!get_go_string_from_user_ptr((void *)(rctx_ptr + method_str_pos), http_request->method, sizeof(http_request->method))) {
bpf_printk("failed to get method from rctx");
}

// get rctx.routePattern
if (!get_go_string_from_user_ptr((void *)(rctx_ptr + rp_str_pos), http_request->path_pattern, sizeof(http_request->path_pattern))) {
bpf_printk("failed to get path_pattern from rctx");
}

bpf_map_update_elem(&http_events, &key, uprobe_data, 0);

output_span_event(ctx, http_request, sizeof(*http_request), &http_request->sc);

stop_tracking_span(&http_request->sc, &http_request->psc);
bpf_map_delete_elem(&http_events, &key);
return 0;
}
Loading