Skip to content

Commit

Permalink
opentelemetry: add opentelemetry source code attributes to spans (#1411)
Browse files Browse the repository at this point in the history
The opentelemetry specification calls for a number of attributes to correlate
traces to their location in the code. They are documented here [1]. This commit
adds support for the following fields based on the tracing span metadata (all
relative to span creation):

- `code.namespace`: Crate & module path (`my_crate::my_mod`)
- `code.filepath`: Relative path to the source code file (`src/my_mod.rs`)
- `code.lineno`: Line number in the file indicated by `code.filepath` (`72`)

As written this will annotate all spans with these attributes. If we want to be
a bit more conservative, I could add an instance variable to the Subscriber that
allows users to opt-in or opt-out of this functionality.

[1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/span-general.md#source-code-attributes

Co-authored-by: Lily Mara <lilymara@onesignal.com>
  • Loading branch information
2 people authored and hawkw committed Jun 25, 2021
1 parent 64f63e6 commit 656f911
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tracing-opentelemetry/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ where
builder.trace_id = Some(self.tracer.new_trace_id());
}

let builder_attrs = builder.attributes.get_or_insert(Vec::new());

let meta = attrs.metadata();

if let Some(filename) = meta.file() {
builder_attrs.push(KeyValue::new("code.filepath", filename));
}

if let Some(module) = meta.module_path() {
builder_attrs.push(KeyValue::new("code.namespace", module));
}

if let Some(line) = meta.line() {
builder_attrs.push(KeyValue::new("code.lineno", line as i64));
}

attrs.record(&mut SpanAttributeVisitor(&mut builder));
extensions.insert(builder);
}
Expand Down

0 comments on commit 656f911

Please sign in to comment.