1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//! AWS X-Ray exporter for OpenTelemetry.
//!
//! This module provides functionality to export OpenTelemetry spans to AWS X-Ray,
//! converting them into X-Ray segment documents and transmitting them to the X-Ray service.
//!
//! # Architecture
//!
//! The exporter consists of three main components:
//!
//! - **[`XrayExporter`]**: The main exporter that implements [`SpanExporter`] and coordinates
//! the translation and export process
//! - **[`SegmentTranslator`]**: Converts OpenTelemetry spans into X-Ray segment documents
//! - **Client implementations**: Handle the actual transmission of segment documents to X-Ray
//! (e.g., `XrayDaemonClient`, `StdoutClient`)
//!
//! # Usage
//!
//! Basic setup with the X-Ray daemon client:
//!
//! **Note**: This example requires the `xray-daemon-client` feature.
//!
//! ```no_run
//! use opentelemetry_aws::{xray_exporter::{XrayExporter, daemon_client::XrayDaemonClient}, trace::XrayIdGenerator};
//! use opentelemetry_sdk::trace::SdkTracerProvider;
//! use opentelemetry::global;
//! # use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a client that sends to the X-Ray daemon on localhost:2000(udp)
//! let daemon_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 2000);
//! let client = XrayDaemonClient::new(daemon_addr)?;
//!
//! // Create the exporter
//! let exporter = XrayExporter::new(client);
//!
//! // Use with a tracer provider
//! let provider = SdkTracerProvider::builder()
//! .with_id_generator(XrayIdGenerator::default())
//! .with_batch_exporter(exporter)
//! .build();
//!
//! // Set it as the global provider
//! global::set_tracer_provider(provider);
//! # Ok(())
//! # }
//! ```
//!
//! With custom translator configuration:
//!
//! **Note**: This example requires the `xray-daemon-client` feature.
//!
//! ```no_run
//! use opentelemetry_aws::xray_exporter::{
//! XrayExporter,
//! daemon_client::XrayDaemonClient,
//! SegmentTranslator,
//! };
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let client = XrayDaemonClient::default();
//!
//! // Configure the translator
//! let translator = SegmentTranslator::new()
//! .with_indexed_attr("service.name".to_string())
//! .with_indexed_attr("http.method".to_string())
//! .with_log_group_name("/aws/lambda/my-function".to_string());
//!
//! let exporter = XrayExporter::new(client)
//! .with_translator(translator);
//! # Ok(())
//! # }
//! ```
//!
//! # Feature Flags
//!
//! This module requires the `xray-exporter` feature.
//! Several sub-modules and capabilities are gated behind additional feature flags:
//!
//! | Feature | Description |
//! |---------|-------------|
//! | `xray-exporter` | Enables this module |
//! | `xray-daemon-client` | Enables the [`daemon_client`] sub-module with [`XrayDaemonClient`] for sending segments to the X-Ray daemon over UDP |
//! | `xray-stdout-client` | Enables the [`stdout_client`] sub-module with [`StdoutClient`] for writing segments to stdout (useful for debugging) |
//! | `xray-subsegment-nesting` | Enables the use of subsegment nesting via [`SegmentTranslator::always_nest_subsegments`] |
//!
//! [`XrayDaemonClient`]: daemon_client::XrayDaemonClient
//! [`StdoutClient`]: stdout_client::StdoutClient
//! [`SpanExporter`]: opentelemetry_sdk::trace::SpanExporter
use ;
use Error;
use ;
pub use SegmentTranslator;
pub use ;
/// Trait for exporting X-Ray segment documents to a backend.
///
/// This trait abstracts the mechanism for transmitting segment documents to AWS X-Ray,
/// allowing different implementations such as UDP transmission to the X-Ray daemon,
/// stdout for debugging, or custom backends.
///
/// # Examples
///
/// Implementing a custom exporter:
///
/// ```
/// use opentelemetry_aws::xray_exporter::{SegmentDocumentExporter, SegmentDocument};
///
/// #[derive(Debug)]
/// struct CustomExporter;
/// # use core::fmt;
/// # #[derive(Debug)]
/// # struct CustomError;
/// # impl std::error::Error for CustomError {}
/// # impl fmt::Display for CustomError {
/// # fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// # write!(f, "Oh no, something bad went down")
/// # }
/// # }
///
/// impl SegmentDocumentExporter for CustomExporter {
/// type Error = CustomError; // Impl Error
/// async fn export_segment_documents(&self, batch: Vec<SegmentDocument<'_>>) -> Result<(), Self::Error> {
/// for document in batch {
/// // Custom export logic
/// println!("Exporting: {}", document.to_string());
/// }
/// Ok(())
/// }
/// }
/// ```
/// AWS X-Ray exporter for OpenTelemetry spans.
///
/// This exporter converts OpenTelemetry spans into AWS X-Ray segment documents
/// and transmits them using the provided client implementation. It implements
/// the [`SpanExporter`] trait from the OpenTelemetry SDK.
///
/// The exporter uses a [`SegmentTranslator`] to perform the conversion from
/// OpenTelemetry's data model to X-Ray's segment format, handling various
/// AWS-specific metadata and maintaining compatibility with X-Ray's requirements.
///
/// # Type Parameters
///
/// * `Client` - The client implementation used to transmit segment documents.
/// Must implement [`SegmentDocumentExporter`].
///
/// # Examples
///
/// Basic usage with the X-Ray daemon client:
///
/// ```no_run
/// use opentelemetry_aws::xray_exporter::{XrayExporter, daemon_client::XrayDaemonClient};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Default XRay daemon client export to localhost:2000(udp)
/// let exporter = XrayExporter::new(XrayDaemonClient::default());
/// # Ok(())
/// # }
/// ```
///
/// With customized translator:
///
/// ```no_run
/// use opentelemetry_aws::xray_exporter::{
/// XrayExporter,
/// daemon_client::XrayDaemonClient,
/// SegmentTranslator,
/// };
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = XrayDaemonClient::default();
///
/// let translator = SegmentTranslator::new()
/// .index_all_attrs();
///
/// let exporter = XrayExporter::new(client)
/// .with_translator(translator);
/// # Ok(())
/// # }
/// ```
///
/// [`SpanExporter`]: opentelemetry_sdk::trace::SpanExporter