Skip to content

Commit 195dea8

Browse files
authored
Few doc updates for opentelemetry (#2353)
1 parent ab332b0 commit 195dea8

File tree

2 files changed

+98
-47
lines changed

2 files changed

+98
-47
lines changed

examples/metrics-basic/Cargo.toml

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ license = "Apache-2.0"
66
publish = false
77

88
[dependencies]
9-
opentelemetry = { path = "../../opentelemetry", features = ["metrics", "otel_unstable"] }
9+
opentelemetry = { path = "../../opentelemetry", features = ["metrics"] }
1010
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["metrics", "rt-tokio"] }
1111
opentelemetry-stdout = { path = "../../opentelemetry-stdout", features = ["metrics"]}
1212
tokio = { workspace = true, features = ["full"] }
1313
serde_json = { workspace = true }
1414

15-
[features]
16-
default = ["otel_unstable"]
17-
otel_unstable = ["opentelemetry/otel_unstable"]

opentelemetry/src/lib.rs

+97-43
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,14 @@
55
//! [`API`]: https://opentelemetry.io/docs/specs/otel/overview/#api
66
//! [OpenTelemetry]: https://opentelemetry.io/docs/what-is-opentelemetry/
77
//!
8-
//! # Getting Started
9-
//!
10-
//! ```no_run
11-
//! # #[cfg(feature = "trace")]
12-
//! # {
13-
//! use opentelemetry::{global, trace::{TraceContextExt, Tracer}, Context };
14-
//!
15-
//! fn do_something() {
16-
//! let tracer = global::tracer("my_component");
17-
//! let _guard = Context::current_with_span(tracer.start("my_span")).attach();
18-
//! // do work tracked by the now current span
19-
//! }
20-
//! # }
21-
//! ```
22-
//!
23-
//! See the [examples] directory for different integration patterns.
24-
//!
25-
//! [examples]: https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples
26-
//!
27-
//! # Traces
8+
//! # Getting Started with Traces
289
//!
2910
//! The [`trace`] module includes types for tracking the progression of a single
3011
//! request while it is handled by services that make up an application. A trace
3112
//! is a tree of [`Span`]s which are objects that represent the work being done
3213
//! by individual services or components involved in a request as it flows
3314
//! through a system.
3415
//!
35-
//! ### Creating and exporting spans
36-
//!
3716
//! ```
3817
//! # #[cfg(feature = "trace")]
3918
//! # {
@@ -55,40 +34,111 @@
5534
//! # }
5635
//! ```
5736
//!
37+
//! See the [examples](https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples) directory for different integration patterns.
38+
//!
5839
//! See the [`trace`] module docs for more information on creating and managing
5940
//! spans.
6041
//!
6142
//! [`Span`]: crate::trace::Span
6243
//!
63-
//! # Metrics
64-
//!
65-
//!
66-
//! The [`metrics`] module includes types for recording measurements about a
67-
//! service at runtime.
68-
//!
69-
//! ### Creating instruments and recording measurements
70-
//!
44+
//! # Getting Started with Metrics
45+
//!
46+
//! The [`metrics`] module provides types for recording measurements about a
47+
//! service at runtime. Below are the key steps to report measurements using
48+
//! OpenTelemetry Metrics:
49+
//!
50+
//! 1. **Obtain a Meter:** Get a `Meter` from a `MeterProvider`.
51+
//! 2. **Create Instruments:** Use the `Meter` to create one or more instruments
52+
//! (e.g., counters, histograms).
53+
//! 3. **Record Measurements:** Use the instruments to record measurement values
54+
//! along with optional attributes.
55+
//!
56+
//! ## How Metrics work in OpenTelemetry
57+
//! In OpenTelemetry, raw measurements recorded using instruments are
58+
//! **aggregated in memory** to form metrics. These aggregated metrics are
59+
//! periodically exported by the [`opentelemetry_sdk`] at fixed intervals (e.g.,
60+
//! every 60 seconds) via exporters such as [`opentelemetry-stdout`] or
61+
//! [`opentelemetry-otlp`]. This reduces reporting overhead while ensuring
62+
//! up-to-date data. The aggregation strategy and export interval can be
63+
//! customized in the [`opentelemetry_sdk`] based on your use case.
64+
//!
65+
//! ## Choosing the Right Instrument
66+
//! Selecting the correct instrument is critical for accurately representing
67+
//! your metrics data:
68+
//!
69+
//! - Use **Counters** for values that only increase, such as the number of
70+
//! requests served or errors encountered.
71+
//! - Use **UpDownCounters** for values that can increase or decrease, such as
72+
//! the number of active connections, number of items in a queue etc.
73+
//! - **Gauges:** Use for values that can go up or down and represent the
74+
//! current state, such as CPU usage, temperature etc.
75+
//! - Use **Histograms** for measuring the distribution of a value, such as
76+
//! response times or payload sizes.
77+
//!
78+
//! ### Observable Instruments
79+
//!
80+
//! Counters, UpDownCounters, and Gauges have Observable variants that allow
81+
//! values to be reported through a callback function. Observable instruments
82+
//! are ideal when the metric value is managed elsewhere and needs to be
83+
//! observed by OpenTelemetry instrumentation. The callbacks are automatically
84+
//! invoked by the OpenTelemetry SDK before every export (e.g., every 60
85+
//! seconds).
86+
//!
87+
//! For example:
88+
//! - An **ObservableCounter** can monitor the number of page faults in a
89+
//! process as reported by the operating system.
90+
//! - An **ObservableUpDownCounter** can monitor the size of an in-memory queue
91+
//! by reporting the size using queue's len() method within the callback
92+
//! function.
93+
//! - An **ObservableGauge** can monitor the CPU temperature by using
94+
//! temperature sensor APIs within the callback function.
95+
//!
96+
//! For detailed guidance, refer to [OpenTelemetry Metrics API - Instrumentation
97+
//! Guidance](https://opentelemetry.io/docs/specs/otel/metrics/supplementary-guidelines/#instrument-selection).
98+
//!
99+
//! ## Best Practices
100+
//! - **Re-use Instruments:** Instruments are designed for
101+
//! reuse. Avoid creating new instruments repeatedly.
102+
//! - **Clone for Sharing:** If the same instrument needs to be used across
103+
//! multiple parts of your code, you can safely clone it to share.
104+
//!
105+
//! ## Example Usage
71106
//! ```
72-
//! # #[cfg(feature = "metrics")]
73-
//! # {
74107
//! use opentelemetry::{global, KeyValue};
75108
//!
76-
//! // get a meter from a provider
109+
//! // Get a meter from a provider.
77110
//! let meter = global::meter("my_service");
78111
//!
79-
//! // create an instrument
80-
//! let counter = meter.u64_counter("my_counter").build();
112+
//! // Create an instrument (in this case, a Counter).
113+
//! let counter = meter.u64_counter("request.count").build();
81114
//!
82-
//! // record a measurement
115+
//! // Record a measurement by passing the value and a set of attributes.
83116
//! counter.add(1, &[KeyValue::new("http.client_ip", "83.164.160.102")]);
84-
//! # }
117+
//!
118+
//! // Create an ObservableCounter and register a callback that reports the measurement.
119+
//! let _observable_counter = meter
120+
//! .u64_observable_counter("bytes_received")
121+
//! .with_callback(|observer| {
122+
//! observer.observe(
123+
//! 100,
124+
//! &[
125+
//! KeyValue::new("protocol", "udp"),
126+
//! ],
127+
//! )
128+
//! })
129+
//! .build();
85130
//! ```
86131
//!
132+
//! See the
133+
//! [examples](https://github.com/open-telemetry/opentelemetry-rust/tree/main/examples/metrics-basic)
134+
//! directory that show a runnable example with all type of instruments.
135+
//!
136+
//!
87137
//! See the [`metrics`] module docs for more information on creating and
88138
//! managing instruments.
89139
//!
90140
//!
91-
//! # Logs
141+
//! # Getting Started with Logs
92142
//!
93143
//! The [`logs`] module contains the Logs Bridge API. It is not intended to be
94144
//! called by application developers directly. It is provided for logging
@@ -102,23 +152,24 @@
102152
//! [`opentelemetry-appender-tracing`](https://crates.io/crates/opentelemetry-appender-tracing)
103153
//! crates.
104154
//!
105-
//! ## Crate Feature Flags
155+
//! # Crate Feature Flags
106156
//!
107157
//! The following core crate feature flags are available:
108158
//!
109159
//! * `trace`: Includes the trace API.
110160
//! * `metrics`: Includes the metrics API.
111161
//! * `logs`: Includes the logs bridge API.
162+
//! * `internal-logs`: Includes internal logging for the OpenTelemetry library via `tracing`.
112163
//!
113-
//! The default feature flags are ["trace", "metrics", "logs"]
164+
//! The default feature flags are ["trace", "metrics", "logs", "internal-logs"].
114165
//!
115166
//! The following feature flags provides additional configuration for `logs`:
116167
//! * `spec_unstable_logs_enabled`: Allow users to control the log level
117168
//!
118169
//! The following feature flags enable APIs defined in OpenTelemetry specification that is in experimental phase:
119-
//! * `otel_unstable`: Includes unstable APIs.
170+
//! * `otel_unstable`: Includes unstable APIs. There are no features behind this flag at the moment.
120171
//!
121-
//! ## Related Crates
172+
//! # Related Crates
122173
//!
123174
//! In addition to `opentelemetry`, the [`open-telemetry/opentelemetry-rust`]
124175
//! repository contains several additional crates designed to be used with the
@@ -133,6 +184,8 @@
133184
//! trace information from [`http`] headers.
134185
//! - [`opentelemetry-otlp`] exporter for sending telemetry in the
135186
//! OTLP format.
187+
//! - [`opentelemetry-stdout`] provides ability to output telemetry to stdout,
188+
//! primarily used for learning/debugging purposes.
136189
//! - [`opentelemetry-prometheus`] provides a pipeline and exporter for sending
137190
//! metrics information to [`Prometheus`].
138191
//! - [`opentelemetry-zipkin`] provides a pipeline and exporter for sending
@@ -148,14 +201,15 @@
148201
//! [`http`]: https://crates.io/crates/http
149202
//! [`open-telemetry/opentelemetry-rust`]: https://github.com/open-telemetry/opentelemetry-rust
150203
//! [`opentelemetry_sdk`]: https://crates.io/crates/opentelemetry_sdk
204+
//! [`opentelemetry-stdout`]: https://crates.io/crates/opentelemetry_stdout
151205
//! [`opentelemetry-http`]: https://crates.io/crates/opentelemetry-http
152206
//! [`opentelemetry-otlp`]: https://crates.io/crates/opentelemetry-otlp
153207
//! [`opentelemetry-prometheus`]: https://crates.io/crates/opentelemetry-prometheus
154208
//! [`opentelemetry-zipkin`]: https://crates.io/crates/opentelemetry-zipkin
155209
//! [`Prometheus`]: https://prometheus.io
156210
//! [`Zipkin`]: https://zipkin.io
157211
//!
158-
//! ## Supported Rust Versions
212+
//! # Supported Rust Versions
159213
//!
160214
//! OpenTelemetry is built against the latest stable release. The minimum
161215
//! supported version is 1.70. The current OpenTelemetry version is not

0 commit comments

Comments
 (0)