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

Initial support for Trace-flags #868

Merged
merged 7 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http

## [Unreleased]

### Added

- Initial support for `trace-flags`. ([#868](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/868))

## [v0.13.0-alpha] - 2024-06-04

### Added
Expand Down
47 changes: 40 additions & 7 deletions internal/include/span_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,40 @@
#define SPAN_CONTEXT_STRING_SIZE 55
#define W3C_KEY_LENGTH 11 // length of the "traceparent" key
#define W3C_VAL_LENGTH 55
#define TRACE_ID_SIZE 16
#define TRACE_ID_STRING_SIZE 32
#define SPAN_ID_SIZE 8
#define SPAN_ID_STRING_SIZE 16
#define TRACE_FLAGS_SIZE 1
#define TRACE_FLAGS_STRING_SIZE 2

static const u8 FLAG_SAMPLED = 1;

struct span_context
{
unsigned char TraceID[TRACE_ID_SIZE];
unsigned char SpanID[SPAN_ID_SIZE];
u8 TraceID[TRACE_ID_SIZE];
u8 SpanID[SPAN_ID_SIZE];
u8 TraceFlags;
u8 padding[7];
};

// Fill the child span context based on the parent span context,
// generating a new span id and copying the trace id and trace flags
static __always_inline void get_span_context_from_parent(struct span_context *parent, struct span_context *child) {
copy_byte_arrays(parent->TraceID, child->TraceID, TRACE_ID_SIZE);
generate_random_bytes(child->SpanID, SPAN_ID_SIZE);
child->TraceFlags = parent->TraceFlags;
}

// Fill the passed span context as root span context
static __always_inline void get_root_span_context(struct span_context *sc) {
generate_random_bytes(sc->TraceID, TRACE_ID_SIZE);
generate_random_bytes(sc->SpanID, SPAN_ID_SIZE);
// currently we always sample
sc->TraceFlags = FLAG_SAMPLED;
}

// TODO: remove this function once all the probes move to the above functions
static __always_inline struct span_context generate_span_context()
{
struct span_context context = {};
Expand Down Expand Up @@ -55,17 +82,23 @@ static __always_inline void span_context_to_w3c_string(struct span_context *ctx,
out += SPAN_ID_STRING_SIZE;
*out++ = '-';

// Write sampled
*out++ = '0';
*out = '1';
// Write trace flags
bytes_to_hex_string(&ctx->TraceFlags, TRACE_FLAGS_SIZE, out);
}

static __always_inline void w3c_string_to_span_context(char *str, struct span_context *ctx)
{
u32 trace_id_start_pos = 3;
u32 span_id_start_pod = 36;
u32 span_id_start_pos = 36;
u32 trace_flags_start_pos = 53;
hex_string_to_bytes(str + trace_id_start_pos, TRACE_ID_STRING_SIZE, ctx->TraceID);
hex_string_to_bytes(str + span_id_start_pod, SPAN_ID_STRING_SIZE, ctx->SpanID);
hex_string_to_bytes(str + span_id_start_pos, SPAN_ID_STRING_SIZE, ctx->SpanID);
hex_string_to_bytes(str + trace_flags_start_pos, TRACE_FLAGS_STRING_SIZE, &ctx->TraceFlags);
}

static __always_inline bool is_sampled(struct span_context *ctx)
{
return ((ctx->TraceFlags & FLAG_SAMPLED) == FLAG_SAMPLED);
}

#endif
4 changes: 0 additions & 4 deletions internal/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

#include "bpf_helpers.h"

#define TRACE_ID_SIZE 16
#define TRACE_ID_STRING_SIZE 32
#define SPAN_ID_SIZE 8
#define SPAN_ID_STRING_SIZE 16

static __always_inline bool bpf_memcmp(char *s1, char *s2, s32 size)
{
Expand Down

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

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

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

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

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

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

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

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

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

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

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int uprobe_Transport_roundTrip(struct pt_regs *ctx) {
copy_byte_arrays(httpReq->psc.TraceID, httpReq->sc.TraceID, TRACE_ID_SIZE);
generate_random_bytes(httpReq->sc.SpanID, SPAN_ID_SIZE);
} else {
httpReq->sc = generate_span_context();
get_root_span_context(&httpReq->sc);
}

if (!get_go_string_from_user_ptr((void *)(req_ptr+method_ptr_pos), httpReq->method, sizeof(httpReq->method))) {
Expand Down

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

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

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

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

Loading
Loading