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

Use weaver for semantic convention codegen #2098

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5583e3e
chore(build): Migrate to Weaver
lquerel Sep 10, 2024
5413e78
Merge branch 'open-telemetry:main' into main
lquerel Sep 10, 2024
2661464
chore(build): Fix deprecated string issues
lquerel Sep 11, 2024
63eb57a
Merge remote-tracking branch 'origin/main'
lquerel Sep 11, 2024
03178a1
chore(build): Generate resource and trace attributes only for resourc…
lquerel Sep 11, 2024
8211cb8
chore(build): Fix metric requirement_level rendering
lquerel Sep 11, 2024
ce8dcaa
chore(build): Update version and CHANGELOG.md
lquerel Sep 11, 2024
fdbb100
chore(build): Add event attributes in trace.rs
lquerel Sep 11, 2024
209c775
Merge branch 'main' into main
lquerel Sep 11, 2024
2b300bb
chore(build): Remove version update in Cargo.toml.
lquerel Sep 11, 2024
19aa24f
Merge remote-tracking branch 'origin/main'
lquerel Sep 11, 2024
a2a55c0
Merge branch 'main' into main
lquerel Sep 11, 2024
731ec69
Merge branch 'main' into main
lquerel Sep 11, 2024
c6af804
chore(build): Update CHANGELOG.md based on @tommycpp feedback
lquerel Sep 11, 2024
62c12ce
Merge remote-tracking branch 'origin/main'
lquerel Sep 11, 2024
672b66a
Merge branch 'main' into main
TommyCpp Sep 12, 2024
6beacd0
Merge branch 'main' into main
lalitb Sep 13, 2024
441df1f
Merge branch 'main' into main
lalitb Sep 13, 2024
7b3c044
Merge branch 'main' into main
lquerel Sep 18, 2024
4970ee9
chore(build): Take into account PR review feedback
lquerel Sep 18, 2024
13f1dbd
Merge remote-tracking branch 'origin/main'
lquerel Sep 18, 2024
d3a3ba6
chore(build): Fix cargo doc lint issues
lquerel Sep 18, 2024
6f9a59c
Merge branch 'main' into main
lalitb Sep 19, 2024
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
8 changes: 8 additions & 0 deletions opentelemetry-semantic-conventions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## vNext

## v0.26.0
lquerel marked this conversation as resolved.
Show resolved Hide resolved
### Changed

- Starting with this version, this crate will use Weaver for the generation of
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
the semantic conventions.
- Introduced a new feature `semconv_experimental` to enable experimental semantic conventions.
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
lquerel marked this conversation as resolved.
Show resolved Hide resolved
This feature is disabled by default.

## v0.25.0
### Changed

Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-semantic-conventions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ rust-version = "1.65"
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = []
semconv_experimental = []

[dev-dependencies]
opentelemetry = { default-features = false, path = "../opentelemetry" } # for doctests
opentelemetry_sdk = { features = ["trace"], path = "../opentelemetry-sdk" } # for doctests
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CRATE_DIR="${SCRIPT_DIR}/../"

# freeze the spec version and generator version to make generation reproducible
SPEC_VERSION=1.27.0
SEMCOVGEN_VERSION=0.25.0
WEAVER_VERSION=v0.9.2
Copy link
Member

Choose a reason for hiding this comment

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

To clarify, the process for generating the code remains unchanged, correct? Specifically, we still increment the spec/weaver version here and run this script as usual?

Copy link
Author

Choose a reason for hiding this comment

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

Yes correct.


cd "$CRATE_DIR"

Expand All @@ -20,54 +20,24 @@ git fetch origin "v$SPEC_VERSION"
git reset --hard FETCH_HEAD
cd "$CRATE_DIR"

docker run --rm \
Copy link
Author

Choose a reason for hiding this comment

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

We only need to call Weaver once, as the weaver.yaml file specifies what needs to be generated.

-v "${CRATE_DIR}/semantic-conventions/model:/source" \
-v "${CRATE_DIR}/scripts/templates:/templates" \
-v "${CRATE_DIR}/src:/output" \
otel/semconvgen:$SEMCOVGEN_VERSION \
-f /source code \
--template /templates/semantic_attributes.rs.j2 \
--output /output/attribute.rs \
--parameters conventions=attribute

docker run --rm \
-v "${CRATE_DIR}/semantic-conventions/model:/source" \
-v "${CRATE_DIR}/scripts/templates:/templates" \
-v "${CRATE_DIR}/src:/output" \
otel/semconvgen:$SEMCOVGEN_VERSION \
--only span,event \
-f /source code \
--template /templates/semantic_attributes.rs.j2 \
--output /output/trace.rs \
--parameters conventions=trace

docker run --rm \
-v "${CRATE_DIR}/semantic-conventions/model:/source" \
-v "${CRATE_DIR}/scripts/templates:/templates" \
-v "${CRATE_DIR}/src:/output" \
otel/semconvgen:$SEMCOVGEN_VERSION \
--only resource \
-f /source code \
--template /templates/semantic_attributes.rs.j2 \
--output /output/resource.rs \
--parameters conventions=resource

docker run --rm \
-v "${CRATE_DIR}/semantic-conventions/model:/source" \
-v "${CRATE_DIR}/scripts/templates:/templates" \
-v "${CRATE_DIR}/src:/output" \
otel/semconvgen:$SEMCOVGEN_VERSION \
-f /source code \
--template /templates/semantic_metrics.rs.j2 \
--output /output/metric.rs

SED=(sed -i)
if [[ "$(uname)" = "Darwin" ]]; then
SED=(sed -i "")
fi

# Keep `SCHEMA_URL` key in sync with spec version
"${SED[@]}" "s/\(opentelemetry.io\/schemas\/\)[^\"]*\"/\1$SPEC_VERSION\"/" src/lib.rs
"${SED[@]}" "s/\(opentelemetry.io\/schemas\/\)[^\"]*\"/\1$SPEC_VERSION\"/" scripts/templates/registry/rust/weaver.yaml

docker run --rm \
--mount type=bind,source=$CRATE_DIR/semantic-conventions/model,target=/home/weaver/source,readonly \
--mount type=bind,source=$CRATE_DIR/scripts/templates,target=/home/weaver/templates,readonly \
--mount type=bind,source=$CRATE_DIR/src,target=/home/weaver/target \
otel/weaver:$WEAVER_VERSION \
registry generate \
--registry=/home/weaver/source \
--templates=/home/weaver/templates \
rust \
/home/weaver/target/

# handle doc generation failures
"${SED[@]}" 's/\[2\]\.$//' src/attribute.rs # remove trailing [2] from few of the doc comments
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{%- import 'macros.j2' as attr_macros -%}
// DO NOT EDIT, this is an auto-generated file
//
// If you want to update the file:
// - Edit the template at scripts/templates/registry/rust/attributes.rs.j2
// - Run the script at scripts/generate-consts-from-spec.sh
TommyCpp marked this conversation as resolved.
Show resolved Hide resolved

//! # Semantic Attributes
//!
//! The entire set of semantic attributes (or [conventions](https://opentelemetry.io/docs/concepts/semantic-conventions/)) defined by the project. The resource, metric, and trace modules reference these attributes.

{% for root_ns in ctx %}
{% for attr in root_ns.attributes | rejectattr("name", "in", params.excluded_attributes) %}
{{ [attr.brief, concat_if("\n\n# Notes\n\n", attr.note), attr_macros.examples(attr)] | comment }}
lquerel marked this conversation as resolved.
Show resolved Hide resolved
{% if attr is experimental %}
#[cfg(feature = "semconv_experimental")]
{% endif %}
{% if attr is deprecated %}
#[deprecated(note="{{ attr.deprecated.strip(" \n\"") }}")]
{% endif %}
pub const {{ attr.name | screaming_snake_case }}: &str = "{{ attr.name }}";

{% endfor %}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//! OpenTelemetry semantic conventions are agreed standardized naming patterns
//! for OpenTelemetry things. This crate aims to be the centralized place to
//! interact with these conventions.
#![warn(
future_incompatible,
missing_debug_implementations,
missing_docs,
nonstandard_style,
rust_2018_idioms,
unreachable_pub,
unused
)]
#![cfg_attr(test, deny(warnings))]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/open-telemetry/opentelemetry-rust/main/assets/logo.svg"
)]

pub mod attribute;
pub mod metric;
pub mod resource;
pub mod trace;

/// The schema URL that matches the version of the semantic conventions that
/// this crate defines.
pub const SCHEMA_URL: &str = "{{ params.schema_url }}";
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{%- macro examples(entity) -%}
{% if entity.examples %}
# Examples

{% if entity.examples is sequence %}
{% for example in entity.examples %}
- {{ example | pprint }}
{% endfor %}
{% else %}
- {{ entity.examples | pprint }}
{% endif %}
{% endif %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{%- import 'macros.j2' as metric_macros -%}
// DO NOT EDIT, this is an auto-generated file
//
// If you want to update the file:
// - Edit the template at scripts{{template}}
lquerel marked this conversation as resolved.
Show resolved Hide resolved
// - Run the script at scripts/generate-consts-from-spec.sh

//! # Metric Semantic Conventions
//!
//! The [metric semantic conventions] define a set of standardized attributes to
//! be used in `Meter`s.
//!
//! [metric semantic conventions]: https://github.com/open-telemetry/semantic-conventions/tree/main/model/metric
//!
//! ## Usage
//!
//! ```rust
//! use opentelemetry::{global, KeyValue};
//! use opentelemetry_semantic_conventions as semconv;
//!
//! // Assumes we already have an initialized `MeterProvider`
//! // See: https://github.com/open-telemetry/opentelemetry-rust/blob/main/examples/metrics-basic/src/main.rs
//! // for an example
//! let meter = global::meter("mylibraryname");
//! let histogram = meter
//! .u64_histogram(semconv::metric::HTTP_SERVER_REQUEST_DURATION)
//! .with_unit("By")
//! .with_description("Duration of HTTP server requests.")
//! .init();
//! ```

{% for root_ns in ctx %}
{% for metric in root_ns.metrics %}
{{ ["## Description\n\n", metric.brief, concat_if("\n\n# Notes\n\n", metric.note), metric_macros.examples(metric)] | comment }}
lquerel marked this conversation as resolved.
Show resolved Hide resolved
/// ## Metadata
/// | | |
/// |:-|:-
/// | Instrument: | `{{ metric.instrument }}` |
/// | Unit: | `{{ metric.unit }}` |
/// | Status: | `{{ metric.stability | capitalize }}` |
{% if metric.attributes %}
///
/// ## Attributes
/// | Name | Requirement |
/// |:-|:- |
{% endif %}
{% for attribute in metric.attributes | rejectattr("name", "in", params.excluded_attributes) | sort(attribute="name") %}
{% if attribute.requirement_level %}
{% if attribute.requirement_level.conditionally_required %}
{% set req_level = "Conditionally_required" %}
{% set req_message = attribute.requirement_level.conditionally_required %}
{% else %}
{% set req_level = (attribute.requirement_level | capitalize) %}
{% set req_message = attribute.requirement_level_msg %}
{% endif %}
{% else %}
{% set req_level = "Unspecified" %}
{% set req_message = '' %}
{% endif %}
/// | [`crate::attribute::{{ attribute.name | screaming_snake_case }}`] | `{{ req_level }}`{{ (': ' + req_message if req_message else '') }}
{% endfor %}
{% if metric.examples %}
///
/// # Examples
lquerel marked this conversation as resolved.
Show resolved Hide resolved
///
{% for example in metric.examples %}
/// - `{{ example }}`
{% endfor %}
{% endif %}
{% if metric is experimental %}
#[cfg(feature = "semconv_experimental")]
{% endif %}
{% if metric is deprecated %}
#[deprecated(note="{{ metric.deprecated.strip(" \n\"") }}")]
{% endif %}
pub const {{ metric.metric_name | screaming_snake_case }}: &str = "{{ metric.metric_name }}";

{% endfor %}
{% endfor %}
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
//! # Resource Semantic Conventions
//!
//! The [resource semantic conventions] define a set of standardized attributes
//! to be used in `Resource`s.
//!
//! [resource semantic conventions]: https://github.com/open-telemetry/semantic-conventions/tree/main/model/resource
//!
//! ## Usage
//!
//! ```rust
//! use opentelemetry::KeyValue;
//! use opentelemetry_sdk::{trace::{config, TracerProvider}, Resource};
//! use opentelemetry_semantic_conventions as semconv;
//!
//! let _tracer = TracerProvider::builder()
//! .with_config(config().with_resource(Resource::new(vec![
//! KeyValue::new(semconv::resource::SERVICE_NAME, "my-service"),
//! KeyValue::new(semconv::resource::SERVICE_NAMESPACE, "my-namespace"),
//! ])))
//! .build();
//! ```
{%- import 'macros.j2' as attr_macros -%}
// DO NOT EDIT, this is an auto-generated file
//
// If you want to update the file:
// - Edit the template at scripts/templates/registry/rust/attributes.rs.j2
// - Run the script at scripts/generate-consts-from-spec.sh
lquerel marked this conversation as resolved.
Show resolved Hide resolved

//! # Resource Semantic Conventions
//!
//! The [resource semantic conventions] define a set of standardized attributes
//! to be used in `Resource`s.
//!
//! [resource semantic conventions]: https://github.com/open-telemetry/semantic-conventions/tree/main/model/resource
//!
//! ## Usage
//!
//! ```rust
//! use opentelemetry::KeyValue;
//! use opentelemetry_sdk::{trace::{config, TracerProvider}, Resource};
//! use opentelemetry_semantic_conventions as semconv;
//!
//! let _tracer = TracerProvider::builder()
//! .with_config(config().with_resource(Resource::new(vec![
//! KeyValue::new(semconv::resource::SERVICE_NAME, "my-service"),
//! KeyValue::new(semconv::resource::SERVICE_NAMESPACE, "my-namespace"),
//! ])))
//! .build();
//! ```

{% for attr in ctx | rejectattr("name", "in", params.excluded_attributes) %}
{% if attr is experimental %}
#[cfg(feature = "semconv_experimental")]
{% endif %}
{% if attr is deprecated %}
#[deprecated(note="{{ attr.deprecated.strip(" \n\"") }}")]
{% endif %}
pub use crate::attribute::{{ attr.name | screaming_snake_case }};

{% endfor %}
Loading
Loading