ap-relay 0.3.126

A simple activitypub relay
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::future::{Future, Ready, ready};

use actix_web::{
    ResponseError,
    body::MessageBody,
    dev::{Service, ServiceRequest, ServiceResponse, Transform},
    http::{Method, StatusCode, Version},
};
use opentelemetry::propagation::Extractor;
use tracing::Span;
use uuid::Uuid;

fn http_method_str(method: &Method) -> std::borrow::Cow<'static, str> {
    match method {
        &Method::OPTIONS => "OPTIONS".into(),
        &Method::GET => "GET".into(),
        &Method::POST => "POST".into(),
        &Method::PUT => "PUT".into(),
        &Method::DELETE => "DELETE".into(),
        &Method::HEAD => "HEAD".into(),
        &Method::TRACE => "TRACE".into(),
        &Method::CONNECT => "CONNECT".into(),
        &Method::PATCH => "PATCH".into(),
        other => other.to_string().into(),
    }
}

fn http_flavor(version: Version) -> std::borrow::Cow<'static, str> {
    match version {
        Version::HTTP_09 => "0.9".into(),
        Version::HTTP_10 => "1.0".into(),
        Version::HTTP_11 => "1.1".into(),
        Version::HTTP_2 => "2.0".into(),
        Version::HTTP_3 => "3.0".into(),
        other => format!("{other:?}").into(),
    }
}

fn http_scheme(scheme: &str) -> std::borrow::Cow<'static, str> {
    match scheme {
        "http" => "http".into(),
        "https" => "https".into(),
        other => other.to_string().into(),
    }
}

pub(crate) struct RequestHeaderCarrier<'a> {
    headers: &'a actix_web::http::header::HeaderMap,
}

impl<'a> RequestHeaderCarrier<'a> {
    pub(crate) fn new(headers: &'a actix_web::http::header::HeaderMap) -> Self {
        RequestHeaderCarrier { headers }
    }
}

impl Extractor for RequestHeaderCarrier<'_> {
    fn get(&self, key: &str) -> Option<&str> {
        self.headers.get(key).and_then(|v| v.to_str().ok())
    }

    fn keys(&self) -> Vec<&str> {
        self.headers.keys().map(|header| header.as_str()).collect()
    }
}

fn set_otel_parent(req: &ServiceRequest, span: &Span) {
    use opentelemetry::trace::TraceContextExt as _;
    use tracing_opentelemetry::OpenTelemetrySpanExt as _;

    let parent_context = opentelemetry::global::get_text_map_propagator(|propagator| {
        propagator.extract(&RequestHeaderCarrier::new(req.headers()))
    });
    let _ = span.set_parent(parent_context);
    // If we have a remote parent span, this will be the parent's trace identifier.
    // If not, it will be the newly generated trace identifier with this request as root span.
    let trace_id = {
        let id = span.context().span().span_context().trace_id();
        format!("{id:032x}")
    };

    span.record("trace_id", tracing::field::display(trace_id));
}

fn root_span(request: &ServiceRequest) -> Span {
    let user_agent = request
        .headers()
        .get("User-Agent")
        .map(|h| h.to_str().unwrap_or(""))
        .unwrap_or("");
    let http_route: std::borrow::Cow<'static, str> = request
        .match_pattern()
        .map(Into::into)
        .unwrap_or_else(|| "default".into());
    let http_method = http_method_str(request.method());
    let connection_info = request.connection_info();
    let request_id = Uuid::now_v7();

    let span = tracing::span!(
        tracing::Level::INFO,
        "HTTP request",
        http.method = %http_method,
        http.route = %http_route,
        http.flavor = %http_flavor(request.version()),
        http.scheme = %http_scheme(connection_info.scheme()),
        http.host = %connection_info.host(),
        http.client_ip = %request.connection_info().realip_remote_addr().unwrap_or(""),
        http.user_agent = %user_agent,
        http.target = %request.uri().path_and_query().map(|p| p.as_str()).unwrap_or(""),
        http.status_code = tracing::field::Empty,
        otel.name = %format!("{} {}", http_method, http_route),
        otel.kind = "server",
        otel.status_code = tracing::field::Empty,
        trace_id = tracing::field::Empty,
        request_id = %request_id,
        exception.message = tracing::field::Empty,
        // Not proper OpenTelemetry, but their terminology is fairly exception-centric
        exception.details = tracing::field::Empty,
    );

    std::mem::drop(connection_info);

    // Previously, this line was instrumented with an opentelemetry-specific feature
    // flag check. However, this resulted in the feature flags being resolved in the crate
    // which called `root_span!` as opposed to being resolved by this crate as expected.
    // Therefore, this function simply wraps an internal function with the feature flags
    // to ensure that the flags are resolved against this crate.
    set_otel_parent(request, &span);

    span
}

pub(crate) struct Log {
    info: bool,
}
pub(crate) struct LogMiddleware<S> {
    info: bool,
    inner: S,
}

impl Log {
    pub(crate) fn new(info: bool) -> Self {
        Self { info }
    }
}

#[derive(Debug)]
pub(crate) struct LogError {
    info: bool,
    span: Span,
    error: actix_web::Error,
}

pin_project_lite::pin_project! {
    pub(crate) struct LogFuture<F> {
        info: bool,

        span: Span,

        #[pin]
        inner: F,
    }
}

struct LogMeta {
    info: bool,
    status: StatusCode,
    span: Span,
}

pin_project_lite::pin_project! {
    pub(crate) struct LogBody<B> {
        meta: Option<LogMeta>,

        #[pin]
        inner: B,
    }
}

impl<S, B> Transform<S, ServiceRequest> for Log
where
    B: MessageBody,
    S: Service<ServiceRequest, Response = ServiceResponse<B>>,
    S::Future: 'static,
    S::Error: Into<actix_web::Error>,
{
    type Response = ServiceResponse<LogBody<B>>;
    type Error = actix_web::Error;
    type InitError = ();
    type Transform = LogMiddleware<S>;
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ready(Ok(LogMiddleware {
            info: self.info,
            inner: service,
        }))
    }
}

impl<S, B> Service<ServiceRequest> for LogMiddleware<S>
where
    B: MessageBody,
    S: Service<ServiceRequest, Response = ServiceResponse<B>>,
    S::Future: 'static,
    S::Error: Into<actix_web::Error>,
{
    type Response = ServiceResponse<LogBody<B>>;
    type Error = actix_web::Error;
    type Future = LogFuture<S::Future>;

    fn poll_ready(
        &self,
        ctx: &mut core::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(ctx).map(|res| {
            res.map_err(|e| {
                LogError {
                    info: self.info,
                    error: e.into(),
                    span: Span::none(),
                }
                .into()
            })
        })
    }

    fn call(&self, req: ServiceRequest) -> Self::Future {
        let span = root_span(&req);

        LogFuture {
            info: self.info,
            inner: self.inner.call(req),
            span,
        }
    }
}

impl<F, B, E> Future for LogFuture<F>
where
    B: MessageBody,
    F: Future<Output = Result<ServiceResponse<B>, E>>,
    E: Into<actix_web::Error>,
{
    type Output = Result<ServiceResponse<LogBody<B>>, actix_web::Error>;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        let info = self.info;
        let span = self.span.clone();
        let this = self.project();

        span.in_scope(|| {
            let span = Span::current();

            std::task::Poll::Ready(match std::task::ready!(this.inner.poll(cx)) {
                Ok(response) => {
                    let status = response.status();

                    if let Some(error) = response.response().error() {
                        handle_error(&span, status, error.as_response_error());
                    } else {
                        let code: i32 = response.response().status().as_u16().into();
                        span.record("http.status_code", code);
                        span.record("otel.status_code", "OK");
                    }

                    let log_meta = LogMeta { info, status, span };

                    let meta = if response.response().body().size().is_eof() {
                        log_meta.emit();
                        None
                    } else {
                        Some(log_meta)
                    };

                    Ok(response.map_body(|_, inner| LogBody { meta, inner }))
                }
                Err(e) => {
                    let error = e.into();

                    handle_error(
                        &span,
                        error.as_response_error().status_code(),
                        error.as_response_error(),
                    );

                    Err(LogError { info, error, span }.into())
                }
            })
        })
    }
}

impl<B> MessageBody for LogBody<B>
where
    B: MessageBody,
{
    type Error = B::Error;

    fn size(&self) -> actix_web::body::BodySize {
        self.inner.size()
    }

    fn poll_next(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Result<actix_web::web::Bytes, Self::Error>>> {
        let this = self.project();

        let span = if let Some(meta) = &this.meta {
            meta.span.clone()
        } else {
            Span::none()
        };

        let opt = std::task::ready!(span.in_scope(|| this.inner.poll_next(cx)));

        if opt.is_none()
            && let Some(meta) = this.meta.take()
        {
            meta.emit();
        }

        std::task::Poll::Ready(opt)
    }
}

impl std::fmt::Display for LogError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("Log error wrapper")
    }
}

impl std::error::Error for LogError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.error.source()
    }
}

impl ResponseError for LogError {
    fn status_code(&self) -> actix_web::http::StatusCode {
        self.error.as_response_error().status_code()
    }

    fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
        let response = self.error.error_response();
        let status = response.status();

        let meta = LogMeta {
            info: self.info,
            status,
            span: self.span.clone(),
        };

        if response.body().size().is_eof() {
            meta.emit();
            response
        } else {
            response.map_body(|_, inner| {
                LogBody {
                    meta: Some(meta),
                    inner,
                }
                .boxed()
            })
        }
    }
}

impl LogMeta {
    fn emit(self) {
        let span = self.span;

        span.in_scope(|| {
            if self.status.is_server_error() {
                tracing::error!("server error");
            } else if self.status.is_client_error() {
                tracing::warn!("client error");
            } else if self.status == actix_web::http::StatusCode::NOT_MODIFIED && self.info {
                tracing::info!("completed");
            } else if self.status == actix_web::http::StatusCode::NOT_MODIFIED {
                tracing::debug!("completed");
            } else if self.status.is_redirection() && self.info {
                tracing::info!("redirected");
            } else if self.status.is_redirection() {
                tracing::debug!("redirected");
            } else if self.info {
                tracing::info!("completed");
            } else {
                tracing::debug!("completed");
            }
        })
    }
}

fn handle_error(span: &Span, status_code: StatusCode, response_error: &dyn ResponseError) {
    // pre-formatting errors is a workaround for https://github.com/tokio-rs/tracing/issues/1565
    let display = format!("{response_error}");
    let debug = format!("{response_error:?}");
    span.record("exception.message", tracing::field::display(display));
    span.record("exception.details", tracing::field::display(debug));
    let code: i32 = status_code.as_u16().into();

    span.record("http.status_code", code);

    if status_code.is_client_error() {
        span.record("otel.status_code", "OK");
    } else {
        span.record("otel.status_code", "ERROR");
    }
}