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 opentelemetry source code attributes to spans #1411

Merged
merged 1 commit into from
May 25, 2021

Conversation

lilymara-onesignal
Copy link
Contributor

Motivation

There is some more context in #1397 but the main idea is that we should be able to trace a Span back to the location in the code where it was created. Currently many crates have spans with very generic names (h2 has send_data and reserve_capacity) and this makes it very difficult to figure out where spans are coming from. This change will make it much easier to find the location in the code where spans are created.

Solution

We can capture the location information using the existing location data on the tracing::Span.

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.

Note

I saw that there is a requirement to add tests for new functionality, but I do not see any tests around the attributes that this crate adds to opentelemetry Spans and Events. I have not added any tests because I'm not sure how to write those or where they would go.

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
@lilymara-onesignal lilymara-onesignal requested review from jtescher and a team as code owners May 19, 2021 22:11
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

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

looks good to me!

Comment on lines +436 to +438
if let Some(line) = meta.line() {
builder_attrs.push(KeyValue::new("code.lineno", line as i64));
}
Copy link
Member

Choose a reason for hiding this comment

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

i wonder if we want to only produce this attribute if the module_path or file values are present? a line number without a file or module name doesn't make a lot of sense.

on the other hand, the tracing macros will never generate metadata that has a line number without a filename or module path, and i'd be very surprised if any other instrumentation did that. so, it's not a big deal either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking was that we should try to be defensive and include the maximum possible amount of information on each span.

@hawkw hawkw merged commit 1f8a895 into tokio-rs:master May 25, 2021
@lilymara-onesignal lilymara-onesignal deleted the opentelemetry-source-code-attrs branch May 25, 2021 22:02
@jtescher
Copy link
Collaborator

Looking at the existing benchmarks it appears this may have a performance impact for cases that don't have many fields. Perhaps worth adding a flag here?

hawkw pushed a commit that referenced this pull request Jun 25, 2021
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>
hawkw pushed a commit that referenced this pull request Jun 26, 2021
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>
hawkw added a commit that referenced this pull request Jul 9, 2021
# 0.14.0 (July 9, 2021)

### Breaking Changes

- Upgrade to `v0.15.0` of `opentelemetry` ([#1441])
  For list of breaking changes in OpenTelemetry, see the
  [v0.14.0 changelog](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry/CHANGELOG.md#v0140).

### Added

- Spans now include Opentelemetry `code.namespace`, `code.filepath`, and
  `code.lineno` attributes ([#1411])

### Changed

- Improve performance by pre-allocating attribute `Vec`s ([#1327])

Thanks to @Drevoed, @lilymara-onesignal, and @Folyd for contributing
to this release!
hawkw added a commit that referenced this pull request Jul 9, 2021
# 0.14.0 (July 9, 2021)

### Breaking Changes

- Upgrade to `v0.15.0` of `opentelemetry` ([#1441])
  For list of breaking changes in OpenTelemetry, see the
  [v0.14.0 changelog](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry/CHANGELOG.md#v0140).

### Added

- Spans now include Opentelemetry `code.namespace`, `code.filepath`, and
  `code.lineno` attributes ([#1411])

### Changed

- Improve performance by pre-allocating attribute `Vec`s ([#1327])

Thanks to @Drevoed, @lilymara-onesignal, and @Folyd for contributing
to this release!

Closes #1462
@altanozlu
Copy link

Is there any way to disable it ?

kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
…io-rs#1411)

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>
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
# 0.14.0 (July 9, 2021)

### Breaking Changes

- Upgrade to `v0.15.0` of `opentelemetry` ([tokio-rs#1441])
  For list of breaking changes in OpenTelemetry, see the
  [v0.14.0 changelog](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry/CHANGELOG.md#v0140).

### Added

- Spans now include Opentelemetry `code.namespace`, `code.filepath`, and
  `code.lineno` attributes ([tokio-rs#1411])

### Changed

- Improve performance by pre-allocating attribute `Vec`s ([tokio-rs#1327])

Thanks to @Drevoed, @lilymara-onesignal, and @Folyd for contributing
to this release!

Closes tokio-rs#1462
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.

4 participants