-
Notifications
You must be signed in to change notification settings - Fork 123
docs: add observability samples #5448
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||||||
| // Copyright 2026 Google LLC | ||||||||||
| // | ||||||||||
| // 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 | ||||||||||
| // | ||||||||||
| // https://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. | ||||||||||
|
|
||||||||||
| // [START rust_observability_logging] ANCHOR: rust_observability_logging | ||||||||||
| use google_cloud_secretmanager_v1::client::SecretManagerService; | ||||||||||
| use tracing_subscriber::layer::SubscriberExt; | ||||||||||
| use tracing_subscriber::util::SubscriberInitExt; | ||||||||||
|
|
||||||||||
| pub async fn sample() -> anyhow::Result<()> { | ||||||||||
| // Output `WARN` logs for failed logical client requests, and `DEBUG` logs | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe:
Suggested change
I am not sure if a casual reader of this would understand what "logical client request" means. The surrounding text may need to clarify this. |
||||||||||
| // for failed low-level RPC attempts from the client library crate. | ||||||||||
| let filter = tracing_subscriber::EnvFilter::new("warn,google_cloud_secretmanager=debug"); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The crate name (and target) is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since targets are still hardcoded to |
||||||||||
|
|
||||||||||
| tracing_subscriber::registry() | ||||||||||
| .with(filter) | ||||||||||
| .with(tracing_subscriber::fmt::layer().json()) | ||||||||||
| .init(); | ||||||||||
|
|
||||||||||
| let _client = SecretManagerService::builder() | ||||||||||
| .with_tracing() | ||||||||||
| .build() | ||||||||||
| .await?; | ||||||||||
|
|
||||||||||
| Ok(()) | ||||||||||
| } | ||||||||||
| // [END rust_observability_logging] ANCHOR_END: rust_observability_logging | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| pub mod logging; | ||
| pub mod tracing; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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. | ||
|
|
||
| // [START rust_observability_tracing] ANCHOR: rust_observability_tracing | ||
| use google_cloud_secretmanager_v1::client::SecretManagerService; | ||
| use tracing_subscriber::Registry; | ||
| use tracing_subscriber::layer::SubscriberExt; | ||
|
|
||
| pub async fn sample() -> anyhow::Result<()> { | ||
| use opentelemetry::trace::TracerProvider as _; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: group with the other use declarations. |
||
| let exporter = opentelemetry_otlp::SpanExporter::builder() | ||
| .with_tonic() | ||
| .build()?; | ||
| let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder() | ||
| .with_batch_exporter(exporter) | ||
| .build(); | ||
| let tracer = provider.tracer("example"); | ||
|
|
||
| // Create a tracing layer that sends data to an OpenTelemetry Collector running on localhost. | ||
| let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); | ||
|
|
||
| // Register the subscriber globally | ||
| let subscriber = Registry::default().with(telemetry); | ||
| tracing::subscriber::set_global_default(subscriber)?; | ||
|
|
||
| let _client = SecretManagerService::builder() | ||
| .with_tracing() | ||
| .build() | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| // [END rust_observability_tracing] ANCHOR_END: rust_observability_tracing | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have plans to use these in the mdBook, right? We can skip the
ANCHOR:andANCHOR_ENDif that is the case.