Skip to content

Commit 7725524

Browse files
committed
RSCBC-12: Implement tracing and metrics in public API
1 parent 185e39b commit 7725524

35 files changed

Lines changed: 4089 additions & 385 deletions

sdk/couchbase/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ authors = [
1717
bytes = "1.11.1"
1818
chrono = "0.4.42"
1919
futures = "0.3.31"
20+
hdrhistogram = "7.5.4"
2021
http = "1.3.1"
2122
lazy_static = "1.5.0"
2223
log = "0.4.28"
2324
serde = "1.0"
24-
serde_json = "1.0"
25+
serde_json = { version = "1.0", features = ["preserve_order"] }
2526
uuid = { version = "1.18.1", features = ["v4"] }
2627
webpki-roots = "1.0.3"
2728

@@ -32,6 +33,8 @@ rustls-pemfile = { version = "2.2", optional = true }
3233
tokio = { version = "1.47" }
3334
tokio-native-tls = { version = "0.3", optional = true }
3435
tokio-rustls = { version = "0.26.4", optional = true }
36+
tracing = "0.1.44"
37+
tracing-subscriber = "0.3.22"
3538

3639
[dev-dependencies]
3740
chrono = "0.4"

sdk/couchbase/src/bucket.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,45 @@
1515
* * limitations under the License.
1616
*
1717
*/
18-
1918
use crate::clients::bucket_client::BucketClient;
2019
use crate::clients::collections_mgmt_client::CollectionsMgmtClient;
2120
use crate::clients::diagnostics_client::DiagnosticsClient;
21+
use crate::clients::tracing_client::TracingClient;
2222
use crate::collection::Collection;
2323
use crate::error;
2424
use crate::management::collections::collection_manager::CollectionManager;
2525
use crate::options::diagnostic_options::{PingOptions, WaitUntilReadyOptions};
2626
use crate::results::diagnostics::PingReport;
2727
use crate::scope::Scope;
28+
use crate::tracing::Keyspace;
29+
use couchbase_core::create_span;
30+
use tracing::Instrument;
2831

2932
#[derive(Clone)]
3033
pub struct Bucket {
3134
client: BucketClient,
3235
collections_mgmt_client: CollectionsMgmtClient,
3336
diagnostics_client: DiagnosticsClient,
37+
tracing_client: TracingClient,
3438
}
3539

3640
impl Bucket {
3741
pub(crate) fn new(client: BucketClient) -> Self {
3842
let collections_mgmt_client = client.collections_management_client();
3943
let diagnostics_client = client.diagnostics_client();
44+
let tracing_client = client.tracing_client();
4045

4146
Self {
4247
client,
4348
collections_mgmt_client,
4449
diagnostics_client,
50+
tracing_client,
51+
}
52+
}
53+
54+
fn keyspace(&self) -> Keyspace<'_> {
55+
Keyspace::Bucket {
56+
bucket: self.client.name(),
4557
}
4658
}
4759

@@ -68,15 +80,35 @@ impl Bucket {
6880
}
6981

7082
pub async fn ping(&self, opts: impl Into<Option<PingOptions>>) -> error::Result<PingReport> {
71-
let opts = opts.into().unwrap_or_default();
72-
self.diagnostics_client.ping(opts).await
83+
let keyspace = self.keyspace();
84+
let ctx = self
85+
.tracing_client
86+
.begin_operation(None, keyspace, create_span!("ping"))
87+
.await;
88+
let result = self
89+
.diagnostics_client
90+
.ping(opts.into().unwrap_or_default())
91+
.instrument(ctx.span().clone())
92+
.await;
93+
ctx.end_operation(result.as_ref().err());
94+
result
7395
}
7496

7597
pub async fn wait_until_ready(
7698
&self,
7799
opts: impl Into<Option<WaitUntilReadyOptions>>,
78100
) -> error::Result<()> {
79-
let opts = opts.into().unwrap_or_default();
80-
self.diagnostics_client.wait_until_ready(opts).await
101+
let keyspace = self.keyspace();
102+
let ctx = self
103+
.tracing_client
104+
.begin_operation(None, keyspace, create_span!("wait_until_ready"))
105+
.await;
106+
let result = self
107+
.diagnostics_client
108+
.wait_until_ready(opts.into().unwrap_or_default())
109+
.instrument(ctx.span().clone())
110+
.await;
111+
ctx.end_operation(result.as_ref().err());
112+
result
81113
}
82114
}

sdk/couchbase/src/clients/bucket_client.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::clients::diagnostics_client::{
2626
use crate::clients::scope_client::{
2727
Couchbase2ScopeClient, CouchbaseScopeClient, ScopeClient, ScopeClientBackend,
2828
};
29+
use crate::clients::tracing_client::{CouchbaseTracingClient, TracingClient, TracingClientBackend};
2930
use crate::retry::RetryStrategy;
3031
use std::sync::Arc;
3132

@@ -84,6 +85,17 @@ impl BucketClient {
8485
}
8586
}
8687
}
88+
89+
pub fn tracing_client(&self) -> TracingClient {
90+
match &self.backend {
91+
BucketClientBackend::CouchbaseBucketBackend(backend) => TracingClient::new(
92+
TracingClientBackend::CouchbaseTracingClientBackend(backend.tracing_client()),
93+
),
94+
BucketClientBackend::Couchbase2BucketBackend(_) => {
95+
unimplemented!()
96+
}
97+
}
98+
}
8799
}
88100

89101
#[derive(Clone)]
@@ -141,6 +153,10 @@ impl CouchbaseBucketClient {
141153
self.default_retry_strategy.clone(),
142154
)
143155
}
156+
157+
pub fn tracing_client(&self) -> CouchbaseTracingClient {
158+
CouchbaseTracingClient::new(self.agent_provider.clone())
159+
}
144160
}
145161

146162
#[derive(Clone)]

sdk/couchbase/src/clients/bucket_mgmt_client.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818

1919
use crate::clients::agent_provider::CouchbaseAgentProvider;
20+
use crate::clients::collections_mgmt_client::CollectionsMgmtClientBackend;
21+
use crate::clients::tracing_client::{CouchbaseTracingClient, TracingClient, TracingClientBackend};
2022
use crate::error;
2123
use crate::management::buckets::bucket_settings::BucketSettings;
2224
use crate::options::bucket_mgmt_options::{
@@ -124,6 +126,21 @@ impl BucketMgmtClient {
124126
}
125127
}
126128
}
129+
130+
pub fn tracing_client(&self) -> TracingClient {
131+
match &self.backend {
132+
BucketMgmtClientBackend::CouchbaseBucketMgmtClientBackend(client) => {
133+
let tracing_client = client.tracing_client();
134+
135+
TracingClient::new(TracingClientBackend::CouchbaseTracingClientBackend(
136+
tracing_client,
137+
))
138+
}
139+
BucketMgmtClientBackend::Couchbase2BucketMgmtClientBackend(client) => {
140+
unimplemented!()
141+
}
142+
}
143+
}
127144
}
128145

129146
pub(crate) enum BucketMgmtClientBackend {
@@ -395,6 +412,10 @@ impl CouchbaseBucketMgmtClient {
395412

396413
Ok(())
397414
}
415+
416+
pub fn tracing_client(&self) -> CouchbaseTracingClient {
417+
CouchbaseTracingClient::new(self.agent_provider.clone())
418+
}
398419
}
399420

400421
pub(crate) struct Couchbase2BucketMgmtClient {}

sdk/couchbase/src/clients/cluster_client.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use std::sync::Arc;
4646
use std::time::Duration;
4747

4848
use crate::authenticator::Authenticator;
49+
use crate::clients::tracing_client::{CouchbaseTracingClient, TracingClient, TracingClientBackend};
4950
#[cfg(feature = "unstable-dns-options")]
5051
use std::mem::take;
5152

@@ -97,6 +98,21 @@ impl ClusterClient {
9798
Ok(ClusterClient { backend })
9899
}
99100

101+
pub fn tracing_client(&self) -> TracingClient {
102+
match &self.backend {
103+
ClusterClientBackend::CouchbaseClusterBackend(backend) => {
104+
let tracing_client = backend.tracing_client();
105+
106+
TracingClient::new(TracingClientBackend::CouchbaseTracingClientBackend(
107+
tracing_client,
108+
))
109+
}
110+
ClusterClientBackend::Couchbase2ClusterBackend(_) => {
111+
unimplemented!()
112+
}
113+
}
114+
}
115+
100116
pub fn bucket_client(&self, name: String) -> BucketClient {
101117
match &self.backend {
102118
ClusterClientBackend::CouchbaseClusterBackend(backend) => {
@@ -353,6 +369,12 @@ impl CouchbaseClusterBackend {
353369
)
354370
}
355371

372+
fn tracing_client(&self) -> CouchbaseTracingClient {
373+
let agent = self.agent_manager.get_cluster_agent();
374+
375+
CouchbaseTracingClient::new(CouchbaseAgentProvider::with_agent(agent.clone()))
376+
}
377+
356378
pub async fn set_authenticator(&self, authenticator: Authenticator) -> error::Result<()> {
357379
if authenticator.to_string() != self.original_authenticator.to_string() {
358380
return Err(error::Error::invalid_argument(

sdk/couchbase/src/clients/collection_client.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::clients::query_index_mgmt_client::{
2323
CouchbaseQueryIndexMgmtClient, QueryIndexKeyspace, QueryIndexMgmtClient,
2424
QueryIndexMgmtClientBackend,
2525
};
26+
use crate::clients::tracing_client::{CouchbaseTracingClient, TracingClient, TracingClientBackend};
2627
use crate::retry::RetryStrategy;
2728
use std::sync::Arc;
2829

@@ -43,13 +44,46 @@ impl CollectionClient {
4344
}
4445
}
4546

47+
pub fn bucket_name(&self) -> &str {
48+
match &self.backend {
49+
CollectionClientBackend::CouchbaseCollectionBackend(client) => {
50+
client.bucket_name.as_str()
51+
}
52+
CollectionClientBackend::Couchbase2CollectionBackend(client) => {
53+
client.bucket_name.as_str()
54+
}
55+
}
56+
}
57+
58+
pub fn scope_name(&self) -> &str {
59+
match &self.backend {
60+
CollectionClientBackend::CouchbaseCollectionBackend(client) => {
61+
client.scope_name.as_str()
62+
}
63+
CollectionClientBackend::Couchbase2CollectionBackend(client) => {
64+
client.scope_name.as_str()
65+
}
66+
}
67+
}
68+
4669
pub fn core_kv_client(&self) -> CoreKvClient {
4770
match &self.backend {
4871
CollectionClientBackend::CouchbaseCollectionBackend(client) => client.core_kv_client(),
4972
CollectionClientBackend::Couchbase2CollectionBackend(client) => client.core_kv_client(),
5073
}
5174
}
5275

76+
pub fn tracing_client(&self) -> TracingClient {
77+
match &self.backend {
78+
CollectionClientBackend::CouchbaseCollectionBackend(client) => TracingClient::new(
79+
TracingClientBackend::CouchbaseTracingClientBackend(client.tracing_client()),
80+
),
81+
CollectionClientBackend::Couchbase2CollectionBackend(_) => {
82+
unimplemented!()
83+
}
84+
}
85+
}
86+
5387
pub fn query_index_management_client(&self) -> QueryIndexMgmtClient {
5488
match &self.backend {
5589
CollectionClientBackend::CouchbaseCollectionBackend(client) => {
@@ -135,12 +169,16 @@ impl CouchbaseCollectionClient {
135169
self.default_retry_strategy.clone(),
136170
)
137171
}
172+
173+
pub fn tracing_client(&self) -> CouchbaseTracingClient {
174+
CouchbaseTracingClient::new(self.agent_provider.clone())
175+
}
138176
}
139177

140178
#[derive(Clone)]
141179
pub(crate) struct Couchbase2CollectionClient {
142-
// bucket_name: String,
143-
// scope_name: String,
180+
bucket_name: String,
181+
scope_name: String,
144182
name: String,
145183
}
146184

sdk/couchbase/src/clients/collections_mgmt_client.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
use crate::clients::agent_provider::CouchbaseAgentProvider;
20+
use crate::clients::tracing_client::{CouchbaseTracingClient, TracingClient, TracingClientBackend};
2021
use crate::error;
2122
use crate::error::FeatureNotAvailableErrorKind;
2223
use crate::management::collections::collection_settings::{
@@ -140,6 +141,32 @@ impl CollectionsMgmtClient {
140141
}
141142
}
142143
}
144+
145+
pub fn bucket_name(&self) -> &str {
146+
match &self.backend {
147+
CollectionsMgmtClientBackend::CouchbaseCollectionsMgmtClientBackend(client) => {
148+
client.bucket_name()
149+
}
150+
CollectionsMgmtClientBackend::Couchbase2CollectionsMgmtClientBackend(_) => {
151+
unimplemented!()
152+
}
153+
}
154+
}
155+
156+
pub fn tracing_client(&self) -> TracingClient {
157+
match &self.backend {
158+
CollectionsMgmtClientBackend::CouchbaseCollectionsMgmtClientBackend(client) => {
159+
let tracing_client = client.tracing_client();
160+
161+
TracingClient::new(TracingClientBackend::CouchbaseTracingClientBackend(
162+
tracing_client,
163+
))
164+
}
165+
CollectionsMgmtClientBackend::Couchbase2CollectionsMgmtClientBackend(_) => {
166+
unimplemented!()
167+
}
168+
}
169+
}
143170
}
144171

145172
#[derive(Clone)]
@@ -371,6 +398,14 @@ impl CouchbaseCollectionsMgmtClient {
371398

372399
Ok(scopes)
373400
}
401+
402+
pub fn bucket_name(&self) -> &str {
403+
&self.bucket_name
404+
}
405+
406+
pub fn tracing_client(&self) -> CouchbaseTracingClient {
407+
CouchbaseTracingClient::new(self.agent_provider.clone())
408+
}
374409
}
375410

376411
#[derive(Clone)]

sdk/couchbase/src/clients/core_kv_client.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,33 @@ impl CoreKvClient {
3636
Self { backend }
3737
}
3838

39+
pub fn collection_name(&self) -> &str {
40+
match &self.backend {
41+
CoreKvClientBackend::CouchbaseCoreKvClientBackend(client) => client.collection_name(),
42+
CoreKvClientBackend::Couchbase2CoreKvClientBackend(client) => {
43+
unimplemented!()
44+
}
45+
}
46+
}
47+
48+
pub fn scope_name(&self) -> &str {
49+
match &self.backend {
50+
CoreKvClientBackend::CouchbaseCoreKvClientBackend(client) => client.scope_name(),
51+
CoreKvClientBackend::Couchbase2CoreKvClientBackend(client) => {
52+
unimplemented!()
53+
}
54+
}
55+
}
56+
57+
pub fn bucket_name(&self) -> &str {
58+
match &self.backend {
59+
CoreKvClientBackend::CouchbaseCoreKvClientBackend(client) => client.bucket_name(),
60+
CoreKvClientBackend::Couchbase2CoreKvClientBackend(client) => {
61+
unimplemented!()
62+
}
63+
}
64+
}
65+
3966
pub async fn upsert(
4067
&self,
4168
id: &str,

0 commit comments

Comments
 (0)