From a8e59505112a8c5f1e645ed54dec248ffe6c339d Mon Sep 17 00:00:00 2001 From: manuelgdlvh Date: Sun, 28 Jul 2024 15:17:02 +0200 Subject: [PATCH 1/3] Add a counted procedural macro to simplify handling count metrics --- Cargo.toml | 2 ++ opentelemetry/Cargo.toml | 9 +++++- opentelemetry/src/macros.rs | 58 +++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 opentelemetry/src/macros.rs diff --git a/Cargo.toml b/Cargo.toml index eb33ddfe42..49ca089e86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,3 +48,5 @@ tracing = { version = "0.1", default-features = false } tracing-core = { version = "0.1", default-features = false } tracing-subscriber = { version = "0.3", default-features = false } url = { version = "2.5", default-features = false } +syn = { version = "2.0.72", fedefault-features = false, features = ["full"] } + diff --git a/opentelemetry/Cargo.toml b/opentelemetry/Cargo.toml index cde1e0832c..8ea5c49641 100644 --- a/opentelemetry/Cargo.toml +++ b/opentelemetry/Cargo.toml @@ -26,6 +26,8 @@ futures-sink = "0.3" once_cell = { workspace = true } pin-project-lite = { workspace = true, optional = true } thiserror = { workspace = true } +syn = { workspace = true } + [target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies] js-sys = "0.3.63" @@ -40,10 +42,15 @@ logs_level_enabled = ["logs"] otel_unstable = [] [dev-dependencies] -opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["logs_level_enabled"]} # for documentation tests +opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["logs_level_enabled"] } # for documentation tests criterion = { workspace = true } rand = { workspace = true } +[lib] +name = "macros" +proc-macro = true +path = "src/macros.rs" + [[bench]] name = "metrics" harness = false diff --git a/opentelemetry/src/macros.rs b/opentelemetry/src/macros.rs new file mode 100644 index 0000000000..2317d782e1 --- /dev/null +++ b/opentelemetry/src/macros.rs @@ -0,0 +1,58 @@ +use proc_macro::TokenStream; +use syn::{Block, ItemFn, LitStr, parse_macro_input}; +use syn::__private::ToTokens; + +// Default Values +const DEFAULT_METER_PROVIDER_NAME: &'static str = "default_meter_provider"; +const DEFAULT_DESCRIPTION: &'static str = "Empty description!"; + +// Attributes +const METER_PROVIDER_NAME_ATTR_NAME: &'static str = "meter_provider"; +const NAME_ATTR_NAME: &'static str = "name"; +const DESCRIPTION_ATTR_NAME: &'static str = "description"; + +// Messages +const ATTR_ERROR_MESSAGE: &'static str = "unsupported attribute for counted macro!"; + + +#[proc_macro_attribute] +pub fn counted(attr: TokenStream, item: TokenStream) -> TokenStream { + let mut input = parse_macro_input!(item as ItemFn); + + let mut name = format!("fn_{}_count", input.sig.ident.to_string()); + let mut meter_provider = DEFAULT_METER_PROVIDER_NAME.to_string(); + let mut description = DEFAULT_DESCRIPTION.to_string(); + let parser = syn::meta::parser(|meta| { + if meta.path.is_ident(NAME_ATTR_NAME) { + let value = meta.value()?.parse::()?; + name = value.value(); + return Ok(()); + } else if meta.path.is_ident(METER_PROVIDER_NAME_ATTR_NAME) { + let value = meta.value()?.parse::()?; + meter_provider = value.value(); + return Ok(()); + } else if meta.path.is_ident(DESCRIPTION_ATTR_NAME) { + let value = meta.value()?.parse::()?; + description = value.value(); + return Ok(()); + } + + Err(meta.error(ATTR_ERROR_MESSAGE)) + }); + + parse_macro_input!(attr with parser); + + let mut counted_code_block = syn::parse_str::(&format!(r#" + {{ + static COUNTER: std::sync::LazyLock> = std::sync::LazyLock::new(|| {{ + let meter = opentelemetry::global::meter("{}"); + meter.u64_counter("{}").with_description("{}").init() + }}); + COUNTER.add(1, &[]); + }} + "#, meter_provider, name, description)).unwrap(); + + counted_code_block.stmts.extend_from_slice(&*input.block.stmts); + input.block.stmts = counted_code_block.stmts; + TokenStream::from(input.into_token_stream()) +} From 08540cc745bdb217b75959fb9980f878bbf8018c Mon Sep 17 00:00:00 2001 From: manuelgdlvh Date: Sun, 28 Jul 2024 15:46:21 +0200 Subject: [PATCH 2/3] Add fix to syn crate declaration --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 49ca089e86..67de24c508 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,5 +48,5 @@ tracing = { version = "0.1", default-features = false } tracing-core = { version = "0.1", default-features = false } tracing-subscriber = { version = "0.3", default-features = false } url = { version = "2.5", default-features = false } -syn = { version = "2.0.72", fedefault-features = false, features = ["full"] } +syn = { version = "2.0.72", default-features = false, features = ["full"] } From 6c651c307e3c28202fb1c6822ec492148cafadec Mon Sep 17 00:00:00 2001 From: manuelgdlvh Date: Sun, 28 Jul 2024 23:04:39 +0200 Subject: [PATCH 3/3] Add features and attributes to counted macro --- Cargo.toml | 2 +- opentelemetry-macros/CHANGELOG.md | 693 ++++++++++++++++++++ opentelemetry-macros/Cargo.toml | 32 + opentelemetry-macros/LICENSE | 201 ++++++ opentelemetry-macros/README.md | 144 ++++ opentelemetry-macros/src/lib.rs | 22 + opentelemetry-macros/src/metrics.rs | 1 + opentelemetry-macros/src/metrics/counted.rs | 111 ++++ opentelemetry/Cargo.toml | 6 - opentelemetry/src/macros.rs | 58 -- 10 files changed, 1205 insertions(+), 65 deletions(-) create mode 100644 opentelemetry-macros/CHANGELOG.md create mode 100644 opentelemetry-macros/Cargo.toml create mode 100644 opentelemetry-macros/LICENSE create mode 100644 opentelemetry-macros/README.md create mode 100644 opentelemetry-macros/src/lib.rs create mode 100644 opentelemetry-macros/src/metrics.rs create mode 100644 opentelemetry-macros/src/metrics/counted.rs delete mode 100644 opentelemetry/src/macros.rs diff --git a/Cargo.toml b/Cargo.toml index 67de24c508..e7e08a5a85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,5 +48,5 @@ tracing = { version = "0.1", default-features = false } tracing-core = { version = "0.1", default-features = false } tracing-subscriber = { version = "0.3", default-features = false } url = { version = "2.5", default-features = false } -syn = { version = "2.0.72", default-features = false, features = ["full"] } +syn = { version = "2.0.72", features = ["full"] } diff --git a/opentelemetry-macros/CHANGELOG.md b/opentelemetry-macros/CHANGELOG.md new file mode 100644 index 0000000000..b51ea9fc04 --- /dev/null +++ b/opentelemetry-macros/CHANGELOG.md @@ -0,0 +1,693 @@ +# Changelog + +## vNext + +## v0.24.0 + +- Add "metrics", "logs" to default features. With this, default feature list is + "trace", "metrics" and "logs". +- When "metrics" feature is enabled, `KeyValue` implements `PartialEq`, `Eq`, + `PartialOrder`, `Order`, `Hash`. This is meant to be used for metrics + aggregation purposes only. +- Removed `Unit` struct for specifying Instrument units. Unit is treated as an + opaque string. Migration: Replace `.with_unit(Unit::new("myunit"))` with + `.with_unit("myunit")`. + +- [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) Introduced the `LogRecord::set_target()` method in the log bridge API. +This method allows appenders to set the target/component emitting the logs. + +## v0.23.0 + +### Added + +- [#1640](https://github.com/open-telemetry/opentelemetry-rust/pull/1640) Add `PropagationError` +- [#1701](https://github.com/open-telemetry/opentelemetry-rust/pull/1701) `Gauge` no longer requires `otel-unstable` feature flag, as OpenTelemetry specification for `Gauge` instrument is stable. + +### Removed + +- Remove `urlencoding` crate dependency. [#1613](https://github.com/open-telemetry/opentelemetry-rust/pull/1613) +- Remove global providers for Logs [$1691](https://github.com/open-telemetry/opentelemetry-rust/pull/1691) + LoggerProviders are not meant for end users to get loggers from. It is only required for the log bridges. + Below global constructs for the logs are removed from API: + - opentelemetry::global::logger + - opentelemetry::global::set_logger_provider + - opentelemetry::global::shutdown_logger_provider + - opentelemetry::global::logger_provider + - opentelemetry::global::GlobalLoggerProvider + - opentelemetry::global::ObjectSafeLoggerProvider + For creating appenders using Logging bridge API, refer to the opentelemetry-tracing-appender [example](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-appender-tracing/examples/basic.rs) + +### Changed + +- **BREAKING** Moving LogRecord implementation to the SDK. [1702](https://github.com/open-telemetry/opentelemetry-rust/pull/1702). + - Relocated `LogRecord` struct to SDK. + - Introduced the `LogRecord` trait in the API for populating log records. This trait is implemented by the SDK. + This is the breaking change for the authors of Log Appenders. Refer to the [opentelemetry-appender-tracing](https://github.com/open-telemetry/opentelemetry-rust/tree/main/opentelemetry-appender-tracing) for more details. + +- Deprecate `versioned_logger()` in favor of `logger_builder()` [1567](https://github.com/open-telemetry/opentelemetry-rust/pull/1567). + +Before: + +```rust +let logger = provider.versioned_logger( + "my-logger-name", + Some(env!("CARGO_PKG_VERSION")), + Some("https://opentelemetry.io/schema/1.0.0"), + Some(vec![KeyValue::new("key", "value")]), +); +``` + +After: + +```rust +let logger = provider + .logger_builder("my-logger-name") + .with_version(env!("CARGO_PKG_VERSION")) + .with_schema_url("https://opentelemetry.io/schema/1.0.0") + .with_attributes(vec![KeyValue::new("key", "value")]) + .build(); +``` + +- Deprecate `versioned_tracer()` in favor of `tracer_builder()` [1567](https://github.com/open-telemetry/opentelemetry-rust/pull/1567). + +Before: + +```rust +let tracer = provider.versioned_tracer( + "my-tracer-name", + Some(env!("CARGO_PKG_VERSION")), + Some("https://opentelemetry.io/schema/1.0.0"), + Some(vec![KeyValue::new("key", "value")]), +); +``` + +After: + +```rust +let tracer = provider + .tracer_builder("my-tracer-name") + .with_version(env!("CARGO_PKG_VERSION")) + .with_schema_url("https://opentelemetry.io/schema/1.0.0") + .with_attributes(vec![KeyValue::new("key", "value")]) + .build(); +``` + +## v0.22.0 + +### Added + +- [#1410](https://github.com/open-telemetry/opentelemetry-rust/pull/1410) Add experimental synchronous gauge. This is behind the feature flag, and can be enabled by enabling the feature `otel_unstable` for opentelemetry crate. + +- [#1410](https://github.com/open-telemetry/opentelemetry-rust/pull/1410) Guidelines to add new unstable/experimental features. + +### Changed + +- Modified `AnyValue.Map` to be backed by `HashMap` instead of custom `OrderMap`, +which internally used `IndexMap`. There was no requirement to maintain the order +of entries, so moving from `IndexMap` to `HashMap` offers slight performance +gains, and avoids `IndexMap` dependency. This affects `body` and `attributes` of +`LogRecord`. +[#1353](https://github.com/open-telemetry/opentelemetry-rust/pull/1353) +- Add `TextMapCompositePropagator` [#1373](https://github.com/open-telemetry/opentelemetry-rust/pull/1373) +- Turned off events for `NoopLogger` to save on operations + [#1455](https://github.com/open-telemetry/opentelemetry-rust/pull/1455) + +### Removed + +- Removed `OrderMap` type as there was no requirement to use this over regular +`HashMap`. +[#1353](https://github.com/open-telemetry/opentelemetry-rust/pull/1353) +- Remove API for Creating Histograms with signed integers. [#1371](https://github.com/open-telemetry/opentelemetry-rust/pull/1371) +- Remove `global::shutdown_meter_provider`, use `SdkMeterProvider::shutdown` + directly instead [#1412](https://github.com/open-telemetry/opentelemetry-rust/pull/1412). + +## [v0.21.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.20.0...v0.21.0) + +This release should been seen as 1.0-rc4 following 1.0-rc3 in v0.20.0. Refer to CHANGELOG.md in individual creates for details on changes made in different creates. + +### Changed + +- Bump MSRV to 1.65 [#1318](https://github.com/open-telemetry/opentelemetry-rust/pull/1318) +- Bump MSRV to 1.64 [#1203](https://github.com/open-telemetry/opentelemetry-rust/pull/1203) +- `opentelemetry` crate now only carries the API types [#1186](https://github.com/open-telemetry/opentelemetry-rust/issues/1186). Use the `opentelemetry_sdk` crate for the SDK types. +- `trace::noop::NoopSpan` no longer implements `Default` and instead exposes + a `const DEFAULT` value. [#1270](https://github.com/open-telemetry/opentelemetry-rust/pull/1270) +- Updated crate documentation and examples. + [#1256](https://github.com/open-telemetry/opentelemetry-rust/issues/1256) +- **Breaking** `SpanBuilder` attributes changed from `OrderMap` to + `Vec` and `with_attributes_map` method is removed from `SpanBuilder`. + This implies that OpenTelemetry API will no longer perform + de-dup of attribute Keys. + [#1293](https://github.com/open-telemetry/opentelemetry-rust/issues/1293). + Please share [feedback + here](https://github.com/open-telemetry/opentelemetry-rust/issues/1300), if + you are affected. + +## [v0.20.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.19.0...v0.20.0) +This release should been seen as 1.0-rc3 following 1.0-rc2 in v0.19.0. Refer to CHANGELOG.md in individual creates for details on changes made in different creates. + +### Added + +- Add `new` method to `BoxedTracer` #1009 +- Add js-sys as dependency for api crate when building wasm targets #1078 +- Create tracer using a shared instrumentation library #1129 +- Add `Context::map_current` #1140 +- Add unit/doc tests for metrics #1213 +- Add `opentelemetry::sdk::logs::config()` for parity with `opentelemetry::sdk::trace::config()` (#1197) + +### Changed + +- `OtelString::Owned` carries `Box` instead of `String` #1096 + +### Removed + +- Drop include_trace_context parameter from Logs API/SDK. [#1133](https://github.com/open-telemetry/opentelemetry-rust/issues/1133) +- Synchronous instruments no longer accepts `Context` while reporting + measurements. [#1076](https://github.com/open-telemetry/opentelemetry-rust/pull/1076). +- Fallible conversions from `futures-channel` error types to `LogError` and + `TraceError` removed. + [#1201](https://github.com/open-telemetry/opentelemetry-rust/issues/1201) + +### Fixed + +- Fix `SpanRef::set_attributes` mutability requirement. [#1038](https://github.com/open-telemetry/opentelemetry-rust/pull/1038) +- Move OrderMap module to root of otel-api crate. [#1061](https://github.com/open-telemetry/opentelemetry-rust/pull/1061) +- Use the browser-only js-sys workaround only when actually targeting a browser #1008 + +## [v0.19.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.18.0...v0.19.0) +This release should been seen as 1.0-rc2 following 1.0-rc1 in v0.18.0. Refer to CHANGELOG.md in individual creates for details on changes made in different creates. + +### Added +- Add `WithContext` to public api [#893](https://github.com/open-telemetry/opentelemetry-rust/pull/893). +- Add support for instrumentation scope attributes [#1021](https://github.com/open-telemetry/opentelemetry-rust/pull/1021). + +### Changed +- Implement `Display` on `Baggage` [#921](https://github.com/open-telemetry/opentelemetry-rust/pull/921). +- Bump MSRV to 1.57 [#953](https://github.com/open-telemetry/opentelemetry-rust/pull/953). +- Update dependencies and bump MSRV to 1.60 [#969](https://github.com/open-telemetry/opentelemetry-rust/pull/969). + +## [v0.18.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.17.0...v0.18.0) + +This release is the first beta release of the `trace` API and SDK. If no other +breaking changes are necessary, the next release will be 1.0. The `metrics` API +and SDK are still unstable. + +### Added + +- Pull sampling probability from `OTEL_TRACES_SAMPLER_ARG` in default sdk config #737 +- Add `schema_url` to `Tracer` #743 +- Add `schema_url` to `Resource` #775 +- Add `Span::set_attributes` #638 +- Support concurrent exports #781 +- Add jaeger remote sampler #797 +- Allow Custom Samplers #833 +- Add `SpanExporter::force_flush` and default implementation #845 + +### Changed + +- Deprecate metrics `ValueRecorder` in favor of `Histogram` #728 +- Move `IdGenerator` to SDK, rename to `RandomIdGenerator` #742 +- `meter_with_version` accepts optional parameter for `version` and `schema_url` #752 +- Unify `Event` and `Link` access patterns #757 +- move `SpanKind` display format impl to jaeger crate #758 +- make `TraceStateError` private #755 +- rename `Span::record_exception` to `Span::record_error` #756 +- Replace `StatusCode` and `message` with `Status` #760 +- Move `TracerProvider::force_flush` to SDK #658 +- Switch to static resource references #790 +- Allow O(1) get operations for `SpanBuilder::attributes` [breaking] #799 +- Allow ref counted keys and values #821 +- Bump MSRV from 1.49 to 1.55 #811 +- bump MSRV to 1.56 #866 +- Update metrics API and SDK for latest spec #819 +- Switch to `pin-project-lite` #830 + +### Fixed + +- Update dashmap to avoid soundness hole #818 +- Perform sampling as explained in the specification #839 +- Remove internal message queue between exporter and exporting tasks #848 +- Fix span processor exporting unsampled spans #871 + +### Removed + +- Remove `serialize` feature #738 +- Remove `StatusCode::as_str` #741 +- Remove `Tracer::with_span` #746 + +## [v0.17.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.16.0...v0.17.0) + +### Changed + +- Implement `Serialize` & `Deserialize` for `Sampler`, `SpanLimits` #622, #626 +- Allow `&'static str` and `string` in span methods #654 +- Allow `String` data in instrumentation library. #670 +- Remove `std::fmt::Debug` and `'static` requirements from `TracerProvider`, + `Tracer`, and `Span` #664 +- Remove unused `Tracer::invalid` method #683 +- Split `TracerProvider::tracer` and `TracerProvider::versioned_tracer` methods #682 +- Reduce dependency on `futures` crate #684 +- Switch to parent context references #687 +- Spec-compliant trace and span ids #689 +- Optimize span creation internals #693 +- Add instrumentation library to `ShouldSample` parameters #695 + +### Fixed + +- Fix default resource detection for tracer provider #641 +- Detect `service.name` from `OTEL_SERVICE_NAME` and `OTEL_RESOURCE_ATTRIBUTES` #662 +- Fix `TraceState::valid_key` crashes #665 + +## [v0.16.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.15.0...v0.16.0) + +### Changed + +- Add default resource in `TracerProvider` #571 +- Rename `get_tracer` to `tracer` #586 +- Extract `trace::noop` module and update docs #587 +- Add `Hash` impl for span context and allow spans to clone and export current state #592 +- Enforce span status code's order #593 +- Make `SpanRef` public #600 +- Make `SpanProcessor::on_start` take a mutable span #601 +- Renamed `label` to `attribute` to align with otel specification #609 + +### Performance + +- Small performance boost for `Resource::get` #579 + +## [v0.15.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.14.0...v0.15.0) + +### Added + +- More resource detectors #573 + +### Changed + +- Expose the Error type to allow users to set custom error handlers #551 +- Allow users to use different channels based on runtime in batch span processor #560 +- Move `Unit` into `metrics` module #564 +- Update trace flags to match spec #565 + +### Fixed + +- Fix debug loop, add notes for `#[tokio::test]` #552 +- `TraceState` cannot insert new key-value pairs #567 + +## [v0.14.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.13.0...v0.14.0) + +## Added + +- Adding a dynamic dispatch to Aggregator Selector #497 +- Add `global::force_flush_tracer_provider` #512 +- Add config `max_attributes_per_event` and `max_attributes_per_link` #521 +- Add dropped attribute counts to events and links #529 + +## Changed + +- Remove unnecessary clone in `Key` type #491 +- Remove `#[must_use]` from `set_tracer_provider` #501 +- Rename remaining usage of `default_sampler` to `sampler` #509 +- Use current span for SDK-less context propagation #510 +- Always export span batch when limit reached #519 +- Rename message events to events #530 +- Update resource merge behaviour #537 +- Ignore links with invalid context #538 + +## Removed + +- Remove remote span context #508 +- Remove metrics quantiles #525 + +# Fixed + +- Allow users to use custom export kind selector #526 + +## Performance + +- Improve simple span processor performance #502 +- Local span perf improvements #505 +- Reduce string allocations where possible #506 + +## [v0.13.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.12.0...v0.13.0) + +Upgrade note: exporter pipelines do not return an uninstall guard as of #444, +use `opentelemetry::global::shutdown_tracer_provider` explicitly instead. + +## Changed + +- Pull configrations from environment variables by default when creating BatchSpanProcessor #445 +- Convert doc links to intra-doc #466 +- Switch to Cow for event names #471 +- Use API to configure async runtime instead of features #481 +- Rename trace config with_default_sampler to with_sampler #482 + +## Removed + +- Removed tracer provider guard #444 +- Removed `from_env` and use environment variables to initialize the configurations by default #459 + +## [v0.12.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.11.2...v0.12.0) + +## Added + +- Instrumentation library support #402 +- Batch observer support #429 +- `with_unit` methods in metrics #431 +- Clone trait for noop tracer/tracer provider/span #479 +- Abstracted traits for different runtimes #480 + +## Changed + +- Dependencies updates #410 +- Add `Send`, `Sync` to `AsyncInstrument` in metrics #422 +- Add `Send`, `Sync` to `InstrumentCore` in metrics #423 +- Replace regex with custom logic #411 +- Update tokio to v1 #421 + +## Removed + +- Moved `http` dependencies into a new opentelemetry-http crate #415 +- Remove `tonic` dependency #414 + +## [v0.11.2](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.11.1...v0.11.2) + +# Fixed + +- Fix possible deadlock when dropping metric instruments #407 + +## [v0.11.1](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.11.0...v0.11.1) + +# Fixed + +- Fix remote implicit builder context sampling #405 + +## [v0.11.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.10.0...v0.11.0) + +## Added + +- Add `force_flush` method to span processors #358 +- Add timeout for `force_flush` and `shutdown` #362 + +## Changed + +- Implement Display trait for Key and Value types #353 +- Remove Option from Array values #359 +- Update `ShouldSample`'s parent parameter to be `Context` #368 +- Consolidate error types in `trace` module into `TraceError` #371 +- Add `#[must_use]` to uninstall structs #372 +- Move 3rd party propagators and merge exporter into `sdk::export` #375 +- Add instrumentation version to instrument config #392 +- Use instrumentation library in metrics #393 +- `start_from_context` renamed to `start_with_context` #399 +- Removed `build_with_context` as full context is now stored in builder #399 +- SpanBuilder's `with_parent` renamed to `with_parent_context` #399 + +# Fixed + +- Fix parent based sampling in tracer #354 +- StatusCode enum value ordering #377 +- Counter adding the delta from last collection #395 +- `HistogramAggregator` returning sum vs count #398 + +## [v0.10.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.9.1...v0.10.0) + +## Added + +- Add support for baggage metadata #287 + +## Changed + +- Remove `api` prefix from modules #305 +- Move `mark_as_active_span` and `get_active_span` functions into trace module #310 +- Revert renaming of `SpanContext` to `SpanReference` #299 +- Default trace propagator is now a no-op #329 +- Return references to span contexts instead of clones #325 +- Update exporter errors to be `Box` #284 +- Rename `GenericProvider` to `GenericTracerProvider` #313 +- Reduce `SpanStatus` enum to `Ok`, `Error`, and `Unset` variants #315 +- update B3 propagator to more closely match spec #319 +- Export missing pub global trace types #313 +- Ensure kv array values are homogeneous #333 +- Implement `Display` trait for `Key` and `Value` types #353 +- Move `SpanProcessor` trait into `sdk` module #334 +- Ensure `is_recording` is `false` and span is no-op after `end` #341 +- Move binary propagator and base64 format to contrib #343 +- Ensure metrics noop types go through constructors #345 +- Change `ExportResult` to use `std::result::Result` #347 +- Change `SpanExporter::export` to take `&mut self` instead of `&self` #350 +- Add MSRV 1.42.0 #296 + +## Fixed + +- Fix parent based sampling #354 + +## Removed + +- Remove support for `u64` and `bytes` kv values #323 +- Remove kv value conversion from `&str` #332 + +## [v0.9.1](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.9.0...v0.9.1) + +## Added + +- Allow metric instruments to be cloned #280 + +### Fixed + +- Fix single threaded runtime tokio feature bug #278 + +## [v0.9.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.8.0...v0.9.0) + +## Added + +- Add resource detector #174 +- Add `fields` method to TextMapFormat #178 +- Add support for `tracestate` in `TraceContextPropagator` #191 +- Propagate valid span context in noop tracer #197 +- Add end_with_timestamp method for trace span #199 +- Add ID methods for hex and byte array formatting #200 +- Add AWS X-Ray ID Generator #201 +- AWS X-Ray Trace Context Propagator #202 +- Add instrumentation library information to spans #207 +- Add keys method to extractors #209 +- Add `TraceState` to `SpanContext` #217 +- Add `from_env` config option for `BatchSpanProcessor` #228 +- Add pipeline uninstall mechanism to shut down trace pipelines #229 + +### Changed + +- Re-write metrics sdk to be spec compliant #179 +- Rename `Sampler::Probability` to `Sampler::TraceIdRatioBased` #188 +- Rename `HTTPTextPropagator` to `TextMapPropagator` #192 +- Ensure extractors are case insensitive #193 +- Rename `Provider` to `TracerProvider` #206 +- Rename `CorrelationContext` into `Baggage` #208 +- Pipeline builder for stdout trace exporter #224 +- Switch to async exporters #232 +- Allow `ShouldSample` implementation to modify trace state #237 +- Ensure context guard is `!Send` #239 +- Ensure trace noop structs use `new` constructor #240 +- Switch to w3c `baggage` header #246 +- Move trace module imports from `api` to `api::trace` #255 +- Update `tonic` feature to use version `0.3.x` #258 +- Update exporters to receive owned span data #264 +- Move propagators to `sdk::propagation` #266 +- Rename SpanContext to SpanReference #270 +- Rename `SamplingDecision`'s `NotRecord`, `Record` and `RecordAndSampled` to + `Drop` `RecordOnly` and `RecordAndSample` #247 + +## [v0.8.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.7.0...v0.8.0) + +## Added + +- Add custom span processors to `Provider::Builder` #166 + +### Changed + +- Separate `Carrier` into `Injector` and `Extractor` #164 +- Change the default sampler to be `ParentOrElse(AlwaysOn)` #163 +- Move the `Sampler` interface to the SDK #169 + +## [v0.7.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.6.0...v0.7.0) + +### Added + +- New `ParentOrElse` sampler for fallback logic if parent is not sampled. #128 +- Attributes can now have array values #146 +- Added `record_exception` and `record_exception_with_stacktrace` methods to `Span` #152 + +### Changed + +- Update sampler types #128 + - `Always` is now `AlwaysOn`. `Never` is now `AlwaysOff`. `Probability` now ignores parent + sampled state. +- `base64` and `binary_propagator` have been moved to `experimental` module. #134 +- `Correlation-Context` header has been updated to `otcorrelations` #145 +- `B3Propagator` has been updated to more closely follow the spec #148 + +## [v0.6.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.5.0...v0.6.0) + +### Added + +- Add `http` and `tonic` features to impl `Carrier` for common types. + +### Changed + +- Removed `span_id` from sampling parameters when implementing custom samplers. + +### Fixed + +- Make `Context` `Send + Sync` in #127 + +## [v0.5.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.4.0...v0.5.0) + +### Added + +- Derive `Clone` for `B3Propagator`, `SamplingResult`, and `SpanBuilder` +- Ability to configure the span id / trace id generator +- impl `From` for common `Key` and `Value` types +- Add global `tracer` method +- Add `Resource` API +- Add `Context` API +- Add `Correlations` API +- Add `HttpTextCompositePropagator` for composing `HttpTextPropagator`s +- Add `GlobalPropagator` for globally configuring a propagator +- Add `TraceContextExt` to provide methods for working with trace data in a context +- Expose `EvictedQueue` constructor + +### Changed + +- Ensure that impls of `Span` are `Send` and `Sync` when used in `global` +- Changed `Key` and `Value` method signatures to remove `Cow` references +- Tracer's `start` now uses the implicit current context instead of an explicit span context. + `start_with_context` may be used to specify a context if desired. +- `with_span` now accepts a span for naming consistency and managing the active state of a more + complex span (likely produced by a builder), and the previous functionality that accepts a + `&str` has been renamed to `in_span`, both of which now yield a context to the provided closure. +- Tracer's `get_active_span` now accepts a closure +- The `Instrument` trait has been renamed to `FutureExt` to avoid clashing with metric instruments, + and instead accepts contexts via `with_context`. +- Span's `get_context` method has been renamed to `span_context` to avoid ambiguity. +- `HttpTextPropagators` inject the current context instead of an explicit span context. The context + can be specified with `inject_context`. +- `SpanData`'s `context` has been renamed to `span_context` + +### Fixed + +- Update the probability sampler to match the spec +- Rename `Traceparent` header to `traceparent` + +### Removed + +- `TracerGenerics` methods have been folded in to the `Tracer` trait so it is longer needed +- Tracer's `mark_span_as_inactive` has been removed +- Exporters no longer require an `as_any` method +- Span's `mark_as_active`, `mark_as_inactive`, and `as_any` have been removed + +## [v0.4.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.3.0...v0.4.0) + +### Added + +- New async batch span processor +- New stdout exporter +- Add `trace_id` to `SpanBuilder` + +### Changed + +- Add `attributes` to `Event`s. +- Update `Span`'s `add_event` and `add_event_with_timestamp` to accept attributes. +- Record log fields in jaeger exporter +- Properly export span kind in jaeger exporter +- Add support for `Link`s +- Add `status_message` to `Span` and `SpanData` +- Rename `SpanStatus` to `StatusCode` +- Update `EvictedQueue` internals from LIFO to FIFO +- Switch span attributes to `EvictedHashMap` + +### Fixed + +- Call `shutdown` correctly when span processors and exporters are dropped + +## [v0.3.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.2.0...v0.3.0) + +### Added + +- New Base64 propagator +- New SpanBuilder api +- Zipkin Exporter crate + +### Changed + +- Switch to `SpanId` and `TraceId` from `u64` and `u128` +- Remove `&mut self` requirements for `Span` API + +### Fixed + +- circular Tracer debug impl + +## [v0.2.0](https://github.com/open-telemetry/opentelemetry-rust/compare/b5918251cc07f9f6957434ccddc35306f68bd791..v0.2.0) + +### Added + +- Make trace and metrics features optional +- ExportResult as specified in the specification +- Add Futures compatibility API +- Added serde serialise support to SpanData +- Separate OpenTelemetry Jaeger crate + +### Changed + +- Rename HttpTraceContextPropagator to TraceContextPropagator +- Rename HttpB3Propagator to B3Propagator +- Switch to Apache 2 license +- Resolve agent addresses to allow non-static IP +- Remove tracer name prefix from span name + +### Removed + +- Remove add_link from spans + +## [v0.1.5](https://github.com/jtescher/opentelemetry-rust/compare/v0.1.4...v0.1.5) + +### Added + +- trace-context propagator + +### Changed + +- Prometheus API cleanup + +## [v0.1.4](https://github.com/jtescher/opentelemetry-rust/compare/v0.1.3...v0.1.4) + +### Added + +- Parent option for default sampler + +### Fixed + +- SDK tracer default span id + +## [v0.1.3](https://github.com/jtescher/opentelemetry-rust/compare/v0.1.2...v0.1.3) + +### Changed + +- Ensure spans are always send and sync +- Allow static lifetimes for span names +- Improve KeyValue ergonomics + +## [v0.1.2](https://github.com/jtescher/opentelemetry-rust/compare/v0.1.1...v0.1.2) + +### Added + +- Implement global provider + +## [v0.1.1](https://github.com/jtescher/opentelemetry-rust/compare/v0.1.0...v0.1.1) + +### Added + +- Documentation and API cleanup +- Tracking of active spans via thread local span stack + +## [v0.1.0](https://github.com/jtescher/opentelemetry-rust/commit/ea368ea965aa035f46728d75e1be3b096b6cd6ec) + +Initial debug alpha diff --git a/opentelemetry-macros/Cargo.toml b/opentelemetry-macros/Cargo.toml new file mode 100644 index 0000000000..3624e0679e --- /dev/null +++ b/opentelemetry-macros/Cargo.toml @@ -0,0 +1,32 @@ +[package] +name = "opentelemetry-macros" +version = "0.24.0" +description = "OpenTelemetry Macros for Rust" +homepage = "https://github.com/open-telemetry/opentelemetry-rust" +repository = "https://github.com/open-telemetry/opentelemetry-rust" +readme = "README.md" +categories = [ + "development-tools::debugging", + "development-tools::profiling", + "api-bindings", + "asynchronous", +] +keywords = ["opentelemetry", "logging", "tracing", "metrics", "async"] +license = "Apache-2.0" +edition = "2021" +rust-version = "1.65" + +[features] +default = ["metrics"] +metrics = [] + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[dependencies] +opentelemetry = { version = "0.24", path = "../opentelemetry" } +syn = { workspace = true } + +[lib] +proc-macro = true diff --git a/opentelemetry-macros/LICENSE b/opentelemetry-macros/LICENSE new file mode 100644 index 0000000000..261eeb9e9f --- /dev/null +++ b/opentelemetry-macros/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/opentelemetry-macros/README.md b/opentelemetry-macros/README.md new file mode 100644 index 0000000000..6aacb4925a --- /dev/null +++ b/opentelemetry-macros/README.md @@ -0,0 +1,144 @@ +# OpenTelemetry Rust API + +![OpenTelemetry — An observability framework for cloud-native software.][splash] + +[splash]: https://raw.githubusercontent.com/open-telemetry/opentelemetry-rust/main/assets/logo-text.png + +This crate contains the [OpenTelemetry](https://opentelemetry.io/) API for Rust. + +[![Crates.io: opentelemetry](https://img.shields.io/crates/v/opentelemetry.svg)](https://crates.io/crates/opentelemetry) +[![Documentation](https://docs.rs/opentelemetry/badge.svg)](https://docs.rs/opentelemetry) +[![LICENSE](https://img.shields.io/crates/l/opentelemetry)](./LICENSE) +[![GitHub Actions CI](https://github.com/open-telemetry/opentelemetry-rust/workflows/CI/badge.svg)](https://github.com/open-telemetry/opentelemetry-rust/actions?query=workflow%3ACI+branch%3Amain) +[![codecov](https://codecov.io/gh/open-telemetry/opentelemetry-rust/branch/main/graph/badge.svg)](https://codecov.io/gh/open-telemetry/opentelemetry-rust) +[![Slack](https://img.shields.io/badge/slack-@cncf/otel/rust-brightgreen.svg?logo=slack)](https://cloud-native.slack.com/archives/C03GDP0H023) + +## Overview + +OpenTelemetry is an Observability framework and toolkit designed to create and +manage telemetry data such as traces, metrics, and logs. OpenTelemetry is +vendor- and tool-agnostic, meaning that it can be used with a broad variety of +Observability backends, including open source tools like [Jaeger] and +[Prometheus], as well as commercial offerings. + +OpenTelemetry is *not* an observability backend like Jaeger, Prometheus, or other +commercial vendors. OpenTelemetry is focused on the generation, collection, +management, and export of telemetry. A major goal of OpenTelemetry is that you +can easily instrument your applications or systems, no matter their language, +infrastructure, or runtime environment. Crucially, the storage and visualization +of telemetry is intentionally left to other tools. + +*Compiler support: [requires `rustc` 1.65+][msrv]* + +[Prometheus]: https://prometheus.io +[Jaeger]: https://www.jaegertracing.io +[msrv]: #supported-rust-versions + +### What does this crate contain? + +This crate is basic foundation for integrating OpenTelemetry into libraries and +applications, encompassing several aspects of OpenTelemetry, such as context +management and propagation, baggage, logging, tracing, and metrics. It follows +the [OpenTelemetry +specification](https://github.com/open-telemetry/opentelemetry-specification). +Here's a breakdown of its components: + +- **[Context + API](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md):** + Provides a way to manage and propagate context, which is essential for keeping + track of trace execution across asynchronous tasks. +- **[Propagators + API](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md):** + Defines how context can be shared across process boundaries, ensuring + continuity across microservices or distributed systems. +- **[Baggage + API](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/baggage/api.md):** + Allows for the attachment of metadata (baggage) to telemetry, which can be + used for sharing application-specific information across service boundaries. +- **[Logs Bridge + API](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/bridge-api.md):** + Allows to bridge existing logging mechanisms with OpenTelemetry logging. This + is **NOT** meant for end users to call, instead it is meant to enable writing + bridges/appenders for existing logging mechanisms such as + [log](https://crates.io/crates/log) or + [tracing](https://crates.io/crates/tracing). +- **[Tracing + API](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md):** + Offers a set of primitives to produce distributed traces to understand the + flow of a request across system boundaries. +- **[Metrics + API](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md):** + Offers a set of primitives to produce measurements of operational metrics like + latency, throughput, or error rates. + +This crate serves as a facade or no-op implementation, meaning it defines the +traits for instrumentation but does not itself implement the processing or +exporting of telemetry data. This separation of concerns allows library authors +to depend on the API crate without tying themselves to a specific +implementation. + +Actual implementation and the heavy lifting of telemetry data collection, +processing, and exporting are delegated to the +[opentelemetry-sdk](https://crates.io/crates/opentelemetry-sdk) crate and +various exporter crates such as +[opentelemetry-otlp](https://crates.io/crates/opentelemetry-otlp). This +architecture ensures that the final application can light up the instrumentation +by integrating an SDK implementation. + +Library authors are recommended to depend on this crate *only*. This approach is +also aligned with the design philosophy of existing telemetry solutions in the +Rust ecosystem, like `tracing` or `log`, where these crates only offer a facade +and the actual functionality is enabled through additional crates. + +### Related crates + +Unless you are a library author, you will almost always need to use additional +crates along with this. Given this crate has no-op implementation only, an +OpenTelemetry SDK is always required. +[opentelemetry-sdk](https://crates.io/crates/opentelemetry-sdk) is the official +SDK implemented by OpenTelemetry itself, though it is possible to use a +different sdk. + +Additionally one or more exporters are also required to export telemetry to a +destination. OpenTelemetry provides the following exporters: + +- **[opentelemetry-stdout](https://crates.io/crates/opentelemetry-stdout):** + Prints telemetry to stdout, primarily used for learning/debugging purposes. +- **[opentelemetry-otlp](https://crates.io/crates/opentelemetry-otlp):** Exports + telemetry (logs, metrics and traces) in the [OTLP + format](https://github.com/open-telemetry/opentelemetry-specification/tree/main/specification/protocol) + to an endpoint accepting OTLP. This could be the [OTel + Collector](https://github.com/open-telemetry/opentelemetry-collector), + telemetry backends like [Jaeger](https://www.jaegertracing.io/), + [Prometheus](https://prometheus.io/docs/prometheus/latest/feature_flags/#otlp-receiver) + or [vendor specific endpoints](https://opentelemetry.io/ecosystem/vendors/). +- **[opentelemetry-zipkin](https://crates.io/crates/opentelemetry-zipkin):** + Exports telemetry (traces only) to Zipkin following [OpenTelemetry to Zipkin + specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/zipkin.md) +- **[opentelemetry-prometheus](https://crates.io/crates/opentelemetry-prometheus):** + Exports telemetry (metrics only) to Prometheus following [OpenTelemetry to + Prometheus + specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk_exporters/prometheus.md) + +OpenTelemetry Rust also has a [contrib +repo](https://github.com/open-telemetry/opentelemetry-rust-contrib), where +additional exporters could be found. Check [OpenTelemetry +Registry](https://opentelemetry.io/ecosystem/registry/?language=rust) for +additional exporters and other related components as well. + +## Getting started + +See [docs](https://docs.rs/opentelemetry). + +## Supported Rust Versions + +OpenTelemetry is built against the latest stable release. The minimum supported +version is 1.65. The current OpenTelemetry version is not guaranteed to build +on Rust versions earlier than the minimum supported version. + +The current stable Rust compiler and the three most recent minor versions +before it will always be supported. For example, if the current stable compiler +version is 1.49, the minimum supported version will not be increased past 1.46, +three minor versions prior. Increasing the minimum supported compiler version +is not considered a semver breaking change as long as doing so complies with +this policy. diff --git a/opentelemetry-macros/src/lib.rs b/opentelemetry-macros/src/lib.rs new file mode 100644 index 0000000000..d4d2b58784 --- /dev/null +++ b/opentelemetry-macros/src/lib.rs @@ -0,0 +1,22 @@ +use proc_macro::TokenStream; + +use syn::{ItemFn, parse_macro_input}; + +use crate::metrics::counted::CountedBuilder; + + +#[cfg(feature = "metrics")] +mod metrics; + +#[proc_macro_attribute] +#[cfg(feature = "metrics")] +pub fn counted(attr: TokenStream, item: TokenStream) -> TokenStream { + let mut builder = match CountedBuilder::from(attr, parse_macro_input!(item as ItemFn)) { + Ok(value) => { value } + Err(err) => { + return err; + } + }; + + builder.process() +} diff --git a/opentelemetry-macros/src/metrics.rs b/opentelemetry-macros/src/metrics.rs new file mode 100644 index 0000000000..d22891b035 --- /dev/null +++ b/opentelemetry-macros/src/metrics.rs @@ -0,0 +1 @@ +pub mod counted; diff --git a/opentelemetry-macros/src/metrics/counted.rs b/opentelemetry-macros/src/metrics/counted.rs new file mode 100644 index 0000000000..6767c57914 --- /dev/null +++ b/opentelemetry-macros/src/metrics/counted.rs @@ -0,0 +1,111 @@ +use proc_macro::TokenStream; +use std::sync::Mutex; + +use syn::{Block, ItemFn, LitStr}; +use syn::__private::ToTokens; +use syn::parse::Parser; + +pub struct CountedBuilder { + name: String, + description: String, + meter_provider: String, + labels: String, + item_fn: Option, +} + +impl CountedBuilder { + pub fn from(attr: TokenStream, item_fn: ItemFn) -> Result { + const DEFAULT_METER_PROVIDER_NAME: &'static str = "default_meter_provider"; + const DEFAULT_DESCRIPTION: &'static str = "Empty description!"; + const METER_PROVIDER_NAME_ATTR_NAME: &'static str = "meter_provider"; + const NAME_ATTR_NAME: &'static str = "name"; + const DESCRIPTION_ATTR_NAME: &'static str = "description"; + const LABELS_ATTR_NAME: &'static str = "labels"; + const ATTR_ERROR_MESSAGE: &'static str = "unsupported attribute for counted macro!"; + + let mut name = format!("fn_{}_count", item_fn.sig.ident.to_string()); + let mut meter_provider = DEFAULT_METER_PROVIDER_NAME.to_string(); + let mut description = DEFAULT_DESCRIPTION.to_string(); + let mut labels: Vec = vec![]; + let parser = syn::meta::parser(|meta| { + if meta.path.is_ident(NAME_ATTR_NAME) { + let value = meta.value()?.parse::()?; + name = value.value(); + return Ok(()); + } else if meta.path.is_ident(METER_PROVIDER_NAME_ATTR_NAME) { + let value = meta.value()?.parse::()?; + meter_provider = value.value(); + return Ok(()); + } else if meta.path.is_ident(DESCRIPTION_ATTR_NAME) { + let value = meta.value()?.parse::()?; + description = value.value(); + return Ok(()); + } else if meta.path.is_ident(LABELS_ATTR_NAME) { + let value = meta.value()?.parse::()?; + let raw_labels = value.value(); + labels = raw_labels.split(",").into_iter().map(|v| v.trim().to_string()).collect(); + if labels.len() % 2 != 0 { + panic!("invalid arguments provided in labels attribute! (must be provided list of key-value)"); + } + return Ok(()); + } + + Err(meta.error(ATTR_ERROR_MESSAGE)) + }); + + if let Err(err) = parser.parse(attr) { + return Err(TokenStream::from(err.to_compile_error())); + } + + let mut labels_as_str = String::new(); + for chunk in labels.chunks(2) { + if let [key, value] = chunk { + if !labels_as_str.is_empty() { + labels_as_str.push_str(", "); + } + labels_as_str.push_str(&format!(r#"KeyValue::new("{}", "{}")"#, key, value)); + } + } + + Ok(Self { + name, + description, + meter_provider, + labels: labels_as_str, + item_fn: Some(item_fn), + }) + } + fn check_metric_name(&self) { + static METRIC_NAMES: std::sync::LazyLock>> = std::sync::LazyLock::new(|| { + Mutex::new(Vec::new()) + }); + + let mut vec_lock = METRIC_NAMES.lock().unwrap(); + if vec_lock.contains(&self.name.as_str().to_string()) { + panic!("detected metric name duplication!"); + } + vec_lock.push(self.name.clone()) + } + fn build_code_block(&self) -> Block { + syn::parse_str::(&format!(r#" + {{ + static COUNTER: std::sync::LazyLock> = std::sync::LazyLock::new(|| {{ + let meter = opentelemetry::global::meter("{}"); + meter.u64_counter("{}").with_description("{}").init() + }}); + COUNTER.add(1, &[{}]); + }} + "#, self.meter_provider, self.name, self.description, self.labels)).unwrap() + } + + pub fn process(&mut self) -> TokenStream { + self.check_metric_name(); + let mut code_block = self.build_code_block(); + + let mut item_fn = self.item_fn.take().unwrap(); + + code_block.stmts.extend_from_slice(&*item_fn.block.stmts); + item_fn.block.stmts = code_block.stmts; + TokenStream::from(item_fn.into_token_stream()) + } +} diff --git a/opentelemetry/Cargo.toml b/opentelemetry/Cargo.toml index 8ea5c49641..4845b64b79 100644 --- a/opentelemetry/Cargo.toml +++ b/opentelemetry/Cargo.toml @@ -26,7 +26,6 @@ futures-sink = "0.3" once_cell = { workspace = true } pin-project-lite = { workspace = true, optional = true } thiserror = { workspace = true } -syn = { workspace = true } [target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies] @@ -46,11 +45,6 @@ opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["logs_level_ena criterion = { workspace = true } rand = { workspace = true } -[lib] -name = "macros" -proc-macro = true -path = "src/macros.rs" - [[bench]] name = "metrics" harness = false diff --git a/opentelemetry/src/macros.rs b/opentelemetry/src/macros.rs deleted file mode 100644 index 2317d782e1..0000000000 --- a/opentelemetry/src/macros.rs +++ /dev/null @@ -1,58 +0,0 @@ -use proc_macro::TokenStream; -use syn::{Block, ItemFn, LitStr, parse_macro_input}; -use syn::__private::ToTokens; - -// Default Values -const DEFAULT_METER_PROVIDER_NAME: &'static str = "default_meter_provider"; -const DEFAULT_DESCRIPTION: &'static str = "Empty description!"; - -// Attributes -const METER_PROVIDER_NAME_ATTR_NAME: &'static str = "meter_provider"; -const NAME_ATTR_NAME: &'static str = "name"; -const DESCRIPTION_ATTR_NAME: &'static str = "description"; - -// Messages -const ATTR_ERROR_MESSAGE: &'static str = "unsupported attribute for counted macro!"; - - -#[proc_macro_attribute] -pub fn counted(attr: TokenStream, item: TokenStream) -> TokenStream { - let mut input = parse_macro_input!(item as ItemFn); - - let mut name = format!("fn_{}_count", input.sig.ident.to_string()); - let mut meter_provider = DEFAULT_METER_PROVIDER_NAME.to_string(); - let mut description = DEFAULT_DESCRIPTION.to_string(); - let parser = syn::meta::parser(|meta| { - if meta.path.is_ident(NAME_ATTR_NAME) { - let value = meta.value()?.parse::()?; - name = value.value(); - return Ok(()); - } else if meta.path.is_ident(METER_PROVIDER_NAME_ATTR_NAME) { - let value = meta.value()?.parse::()?; - meter_provider = value.value(); - return Ok(()); - } else if meta.path.is_ident(DESCRIPTION_ATTR_NAME) { - let value = meta.value()?.parse::()?; - description = value.value(); - return Ok(()); - } - - Err(meta.error(ATTR_ERROR_MESSAGE)) - }); - - parse_macro_input!(attr with parser); - - let mut counted_code_block = syn::parse_str::(&format!(r#" - {{ - static COUNTER: std::sync::LazyLock> = std::sync::LazyLock::new(|| {{ - let meter = opentelemetry::global::meter("{}"); - meter.u64_counter("{}").with_description("{}").init() - }}); - COUNTER.add(1, &[]); - }} - "#, meter_provider, name, description)).unwrap(); - - counted_code_block.stmts.extend_from_slice(&*input.block.stmts); - input.block.stmts = counted_code_block.stmts; - TokenStream::from(input.into_token_stream()) -}