Skip to main content

google_cloud_trace_v2/
client.rs

1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Code generated by sidekick. DO NOT EDIT.
16#![allow(rustdoc::redundant_explicit_links)]
17#![allow(rustdoc::broken_intra_doc_links)]
18
19/// Implements a client for the Cloud Trace API.
20///
21/// # Example
22/// ```
23/// # use google_cloud_trace_v2::client::TraceService;
24/// async fn sample(
25/// ) -> anyhow::Result<()> {
26///     let client = TraceService::builder().build().await?;
27///     client.batch_write_spans()
28///         /* set fields */
29///         .send().await?;
30///     Ok(())
31/// }
32/// ```
33///
34/// # Service Description
35///
36/// Service for collecting and viewing traces and spans within a trace.
37///
38/// A trace is a collection of spans corresponding to a single
39/// operation or a set of operations in an application.
40///
41/// A span is an individual timed event which forms a node of the trace tree.
42/// A single trace can contain spans from multiple services.
43///
44/// # Configuration
45///
46/// To configure `TraceService` use the `with_*` methods in the type returned
47/// by [builder()][TraceService::builder]. The default configuration should
48/// work for most applications. Common configuration changes include
49///
50/// * [with_endpoint()]: by default this client uses the global default endpoint
51///   (`https://cloudtrace.googleapis.com`). Applications using regional
52///   endpoints or running in restricted networks (e.g. a network configured
53//    with [Private Google Access with VPC Service Controls]) may want to
54///   override this default.
55/// * [with_credentials()]: by default this client uses
56///   [Application Default Credentials]. Applications using custom
57///   authentication may need to override this default.
58///
59/// [with_endpoint()]: super::builder::trace_service::ClientBuilder::with_endpoint
60/// [with_credentials()]: super::builder::trace_service::ClientBuilder::with_credentials
61/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
62/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
63///
64/// # Pooling and Cloning
65///
66/// `TraceService` holds a connection pool internally, it is advised to
67/// create one and reuse it. You do not need to wrap `TraceService` in
68/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
69/// already uses an `Arc` internally.
70#[derive(Clone, Debug)]
71pub struct TraceService {
72    inner: std::sync::Arc<dyn super::stub::dynamic::TraceService>,
73}
74
75impl TraceService {
76    /// Returns a builder for [TraceService].
77    ///
78    /// ```
79    /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
80    /// # use google_cloud_trace_v2::client::TraceService;
81    /// let client = TraceService::builder().build().await?;
82    /// # Ok(()) }
83    /// ```
84    pub fn builder() -> super::builder::trace_service::ClientBuilder {
85        crate::new_client_builder(super::builder::trace_service::client::Factory)
86    }
87
88    /// Creates a new client from the provided stub.
89    ///
90    /// The most common case for calling this function is in tests mocking the
91    /// client's behavior.
92    pub fn from_stub<T>(stub: impl Into<std::sync::Arc<T>>) -> Self
93    where
94        T: super::stub::TraceService + 'static,
95    {
96        Self { inner: stub.into() }
97    }
98
99    pub(crate) async fn new(
100        config: gaxi::options::ClientConfig,
101    ) -> crate::ClientBuilderResult<Self> {
102        let inner = Self::build_inner(config).await?;
103        Ok(Self { inner })
104    }
105
106    async fn build_inner(
107        conf: gaxi::options::ClientConfig,
108    ) -> crate::ClientBuilderResult<std::sync::Arc<dyn super::stub::dynamic::TraceService>> {
109        if gaxi::options::tracing_enabled(&conf) {
110            return Ok(std::sync::Arc::new(Self::build_with_tracing(conf).await?));
111        }
112        Ok(std::sync::Arc::new(Self::build_transport(conf).await?))
113    }
114
115    async fn build_transport(
116        conf: gaxi::options::ClientConfig,
117    ) -> crate::ClientBuilderResult<impl super::stub::TraceService> {
118        super::transport::TraceService::new(conf).await
119    }
120
121    async fn build_with_tracing(
122        conf: gaxi::options::ClientConfig,
123    ) -> crate::ClientBuilderResult<impl super::stub::TraceService> {
124        Self::build_transport(conf)
125            .await
126            .map(super::tracing::TraceService::new)
127    }
128
129    /// Batch writes new spans to new or existing traces. You cannot update
130    /// existing spans.
131    ///
132    /// # Example
133    /// ```
134    /// # use google_cloud_trace_v2::client::TraceService;
135    /// use google_cloud_trace_v2::Result;
136    /// async fn sample(
137    ///    client: &TraceService
138    /// ) -> Result<()> {
139    ///     client.batch_write_spans()
140    ///         /* set fields */
141    ///         .send().await?;
142    ///     Ok(())
143    /// }
144    /// ```
145    pub fn batch_write_spans(&self) -> super::builder::trace_service::BatchWriteSpans {
146        super::builder::trace_service::BatchWriteSpans::new(self.inner.clone())
147    }
148
149    /// Creates a new span.
150    ///
151    /// # Example
152    /// ```
153    /// # use google_cloud_trace_v2::client::TraceService;
154    /// use google_cloud_trace_v2::Result;
155    /// async fn sample(
156    ///    client: &TraceService
157    /// ) -> Result<()> {
158    ///     let response = client.create_span()
159    ///         /* set fields */
160    ///         .send().await?;
161    ///     println!("response {:?}", response);
162    ///     Ok(())
163    /// }
164    /// ```
165    pub fn create_span(&self) -> super::builder::trace_service::CreateSpan {
166        super::builder::trace_service::CreateSpan::new(self.inner.clone())
167    }
168}