mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-06-06 22:00:59 +03:00
OTEL: remove custom Header{Injector,Extractor} implementations
This commit is contained in:
parent
bf31c4b548
commit
ee9a01ef40
@ -38,6 +38,7 @@ use mas_tower::{
|
|||||||
KV,
|
KV,
|
||||||
};
|
};
|
||||||
use opentelemetry::{Key, KeyValue};
|
use opentelemetry::{Key, KeyValue};
|
||||||
|
use opentelemetry_http::HeaderExtractor;
|
||||||
use opentelemetry_semantic_conventions::trace::{
|
use opentelemetry_semantic_conventions::trace::{
|
||||||
HTTP_REQUEST_METHOD, HTTP_RESPONSE_STATUS_CODE, HTTP_ROUTE, NETWORK_PROTOCOL_NAME,
|
HTTP_REQUEST_METHOD, HTTP_RESPONSE_STATUS_CODE, HTTP_ROUTE, NETWORK_PROTOCOL_NAME,
|
||||||
NETWORK_PROTOCOL_VERSION, URL_PATH, URL_QUERY, URL_SCHEME, USER_AGENT_ORIGINAL,
|
NETWORK_PROTOCOL_VERSION, URL_PATH, URL_QUERY, URL_SCHEME, USER_AGENT_ORIGINAL,
|
||||||
@ -53,22 +54,6 @@ use crate::app_state::AppState;
|
|||||||
|
|
||||||
const MAS_LISTENER_NAME: Key = Key::from_static_str("mas.listener.name");
|
const MAS_LISTENER_NAME: Key = Key::from_static_str("mas.listener.name");
|
||||||
|
|
||||||
/// Same as the extractor from opentelemetry-http, but using http@1
|
|
||||||
struct HeaderExtractor<'a>(pub &'a http::HeaderMap);
|
|
||||||
|
|
||||||
impl<'a> opentelemetry::propagation::Extractor for HeaderExtractor<'a> {
|
|
||||||
fn get(&self, key: &str) -> Option<&str> {
|
|
||||||
self.0.get(key).and_then(|value| value.to_str().ok())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn keys(&self) -> Vec<&str> {
|
|
||||||
self.0
|
|
||||||
.keys()
|
|
||||||
.map(http::HeaderName::as_str)
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn otel_http_method<B>(request: &Request<B>) -> &'static str {
|
fn otel_http_method<B>(request: &Request<B>) -> &'static str {
|
||||||
match request.method() {
|
match request.method() {
|
||||||
|
@ -14,25 +14,11 @@
|
|||||||
|
|
||||||
use http::Request;
|
use http::Request;
|
||||||
use opentelemetry::propagation::Injector;
|
use opentelemetry::propagation::Injector;
|
||||||
|
use opentelemetry_http::HeaderInjector;
|
||||||
use tower::{Layer, Service};
|
use tower::{Layer, Service};
|
||||||
use tracing::Span;
|
use tracing::Span;
|
||||||
use tracing_opentelemetry::OpenTelemetrySpanExt;
|
use tracing_opentelemetry::OpenTelemetrySpanExt;
|
||||||
|
|
||||||
/// Same as the one in opentelemetry-http, but updated for the http@1 upgrade
|
|
||||||
pub struct HeaderInjector<'a>(pub &'a mut http::HeaderMap);
|
|
||||||
|
|
||||||
impl<'a> Injector for HeaderInjector<'a> {
|
|
||||||
/// Set a key and value in the [`http::HeaderMap`]. Does nothing if the key
|
|
||||||
/// or value are not valid inputs.
|
|
||||||
fn set(&mut self, key: &str, value: String) {
|
|
||||||
if let Ok(name) = http::header::HeaderName::from_bytes(key.as_bytes()) {
|
|
||||||
if let Ok(val) = http::header::HeaderValue::from_str(&value) {
|
|
||||||
self.0.insert(name, val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A trait to get an [`Injector`] from a request.
|
/// A trait to get an [`Injector`] from a request.
|
||||||
trait AsInjector {
|
trait AsInjector {
|
||||||
type Injector<'a>: Injector
|
type Injector<'a>: Injector
|
||||||
|
Loading…
x
Reference in New Issue
Block a user