Skip to content

Commit

Permalink
Use weaver for semantic convention codegen (open-telemetry#2098)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhongyang Wu <[email protected]>
Co-authored-by: Lalit Kumar Bhasin <[email protected]>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent 0f15d21 commit 161929d
Show file tree
Hide file tree
Showing 18 changed files with 4,139 additions and 1,508 deletions.
7 changes: 7 additions & 0 deletions opentelemetry-semantic-conventions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## vNext

### Changed

- Starting with this version, this crate will use Weaver for the generation of
the semantic conventions.
- **Breaking** Introduced a new feature `semconv_experimental` to enable experimental semantic conventions.
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.10.0

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 \
-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

//! # 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 }}
{% 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/templates/registry/rust/metric.rs.j2
// - 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 }}
/// ## 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
///
{% 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/resource.rs.j2
// - Run the script at scripts/generate-consts-from-spec.sh

//! # 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 %}
#[allow(deprecated)]
{% endif %}
pub use crate::attribute::{{ attr.name | screaming_snake_case }};

{% endfor %}
Loading

0 comments on commit 161929d

Please sign in to comment.