|
1 | 1 | use std::sync::Arc; |
2 | 2 |
|
3 | | -use async_trait::async_trait; |
4 | 3 | use http::{header::CONTENT_TYPE, Method}; |
5 | 4 | use opentelemetry::otel_debug; |
6 | 5 | use opentelemetry_sdk::export::logs::{LogBatch, LogExporter, ShutdownResult}; |
7 | 6 | use opentelemetry_sdk::logs::{LogError, LogResult}; |
8 | 7 |
|
9 | 8 | use super::OtlpHttpClient; |
10 | 9 |
|
11 | | -#[async_trait] |
12 | 10 | impl LogExporter for OtlpHttpClient { |
13 | | - async fn export(&self, batch: LogBatch<'_>) -> LogResult<()> { |
14 | | - let client = self |
15 | | - .client |
16 | | - .lock() |
17 | | - .map_err(|e| LogError::Other(e.to_string().into())) |
18 | | - .and_then(|g| match &*g { |
19 | | - Some(client) => Ok(Arc::clone(client)), |
20 | | - _ => Err(LogError::Other("exporter is already shut down".into())), |
21 | | - })?; |
22 | | - |
23 | | - let (body, content_type) = { self.build_logs_export_body(batch)? }; |
24 | | - let mut request = http::Request::builder() |
25 | | - .method(Method::POST) |
26 | | - .uri(&self.collector_endpoint) |
27 | | - .header(CONTENT_TYPE, content_type) |
28 | | - .body(body) |
29 | | - .map_err(|e| crate::Error::RequestFailed(Box::new(e)))?; |
30 | | - |
31 | | - for (k, v) in &self.headers { |
32 | | - request.headers_mut().insert(k.clone(), v.clone()); |
33 | | - } |
| 11 | + #[allow(clippy::manual_async_fn)] |
| 12 | + fn export( |
| 13 | + &self, |
| 14 | + batch: LogBatch<'_>, |
| 15 | + ) -> impl std::future::Future<Output = LogResult<()>> + Send { |
| 16 | + async move { |
| 17 | + let client = self |
| 18 | + .client |
| 19 | + .lock() |
| 20 | + .map_err(|e| LogError::Other(e.to_string().into())) |
| 21 | + .and_then(|g| match &*g { |
| 22 | + Some(client) => Ok(Arc::clone(client)), |
| 23 | + _ => Err(LogError::Other("exporter is already shut down".into())), |
| 24 | + })?; |
34 | 25 |
|
35 | | - let request_uri = request.uri().to_string(); |
36 | | - otel_debug!(name: "HttpLogsClient.CallingExport"); |
37 | | - let response = client.send(request).await?; |
38 | | - |
39 | | - if !response.status().is_success() { |
40 | | - let error = format!( |
41 | | - "OpenTelemetry logs export failed. Url: {}, Status Code: {}, Response: {:?}", |
42 | | - response.status().as_u16(), |
43 | | - request_uri, |
44 | | - response.body() |
45 | | - ); |
46 | | - return Err(LogError::Other(error.into())); |
47 | | - } |
| 26 | + let (body, content_type) = { self.build_logs_export_body(batch)? }; |
| 27 | + let mut request = http::Request::builder() |
| 28 | + .method(Method::POST) |
| 29 | + .uri(&self.collector_endpoint) |
| 30 | + .header(CONTENT_TYPE, content_type) |
| 31 | + .body(body) |
| 32 | + .map_err(|e| crate::Error::RequestFailed(Box::new(e)))?; |
48 | 33 |
|
49 | | - Ok(()) |
| 34 | + for (k, v) in &self.headers { |
| 35 | + request.headers_mut().insert(k.clone(), v.clone()); |
| 36 | + } |
| 37 | + |
| 38 | + let request_uri = request.uri().to_string(); |
| 39 | + otel_debug!(name: "HttpLogsClient.CallingExport"); |
| 40 | + let response = client.send(request).await?; |
| 41 | + |
| 42 | + if !response.status().is_success() { |
| 43 | + let error = format!( |
| 44 | + "OpenTelemetry logs export failed. Url: {}, Status Code: {}, Response: {:?}", |
| 45 | + response.status().as_u16(), |
| 46 | + request_uri, |
| 47 | + response.body() |
| 48 | + ); |
| 49 | + return Err(LogError::Other(error.into())); |
| 50 | + } |
| 51 | + |
| 52 | + Ok(()) |
| 53 | + } |
50 | 54 | } |
51 | 55 |
|
52 | 56 | fn shutdown(&mut self) -> ShutdownResult { |
|
0 commit comments