Skip to content

Commit

Permalink
put opentracing layer as an optional layer into the reporting plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
  • Loading branch information
bnjjj committed Feb 28, 2022
1 parent 4d4052a commit 340c8d0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion apollo-router-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ tower = { version = "0.4.12", features = ["full"] }
tower-service = "0.3.1"
tower-test = "0.4.0"
tracing = "0.1.31"
tracing-opentelemetry = "0.17.2"
typed-builder = "0.9.1"
url = "2.2.2"
urlencoding = "2.1.0"
Expand Down
1 change: 0 additions & 1 deletion apollo-router-core/src/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ pub mod cache;
pub mod deduplication;
pub mod forbid_http_get_mutations;
pub mod headers;
pub mod opentracing;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: apollo-router/src/configuration/mod.rs
assertion_line: 645
assertion_line: 474
expression: "&schema"

---
Expand Down Expand Up @@ -235,13 +235,6 @@ expression: "&schema"
}
]
},
"PropagationFormat": {
"type": "string",
"enum": [
"jaeger",
"zipkin_b3"
]
},
"RemoveConfig": {
"oneOf": [
{
Expand Down Expand Up @@ -318,11 +311,6 @@ expression: "&schema"
"required": [
"headers_remove"
]
},
{
"required": [
"opentracing"
]
}
],
"properties": {
Expand All @@ -337,9 +325,6 @@ expression: "&schema"
},
"headers_remove": {
"$ref": "#/definitions/RemoveConfig"
},
"opentracing": {
"$ref": "#/definitions/OpenTracingConfig"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions apollo-router/src/layers/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod hello;
pub mod opentracing;
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::fmt::Display;
use std::task::{Context, Poll};

use crate::layer::ConfigurableLayer;
use crate::{register_layer, services, SubgraphRequest};
use apollo_router_core::{ConfigurableLayer, SubgraphRequest};
use http::HeaderValue;
use opentelemetry::trace::TraceContextExt;
use schemars::JsonSchema;
Expand All @@ -12,11 +11,9 @@ use tracing::instrument::Instrumented;
use tracing::{span, Instrument, Level, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;

register_layer!("opentracing", OpenTracingLayer);

#[derive(Clone, JsonSchema, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
enum PropagationFormat {
pub enum PropagationFormat {
Jaeger,
ZipkinB3,
}
Expand All @@ -30,12 +27,13 @@ impl Display for PropagationFormat {
}
}

#[derive(Clone, JsonSchema, Deserialize)]
struct OpenTracingConfig {
#[derive(Clone, JsonSchema, Deserialize, Debug)]
pub struct OpenTracingConfig {
format: PropagationFormat,
}

struct OpenTracingLayer {
#[derive(Debug)]
pub struct OpenTracingLayer {
format: PropagationFormat,
}

Expand All @@ -59,7 +57,7 @@ impl<S> Layer<S> for OpenTracingLayer {
}
}

struct OpenTracingService<S> {
pub struct OpenTracingService<S> {
inner: S,
format: PropagationFormat,
}
Expand All @@ -70,7 +68,7 @@ where
{
type Response = S::Response;
type Error = S::Error;
type Future = Instrumented<<S as tower::Service<services::SubgraphRequest>>::Future>;
type Future = Instrumented<<S as tower::Service<SubgraphRequest>>::Future>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
Expand Down
31 changes: 30 additions & 1 deletion apollo-router/src/plugins/reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ use crate::apollo_telemetry::new_pipeline;
use crate::apollo_telemetry::SpaceportConfig;
use crate::apollo_telemetry::StudioGraph;
use crate::configuration::{default_service_name, default_service_namespace};
use crate::layers::opentracing::OpenTracingConfig;
use crate::layers::opentracing::OpenTracingLayer;
use crate::set_subscriber;
use crate::GLOBAL_ENV_FILTER;

use apollo_router_core::ConfigurableLayer;
use apollo_router_core::SubgraphRequest;
use apollo_router_core::SubgraphResponse;
use apollo_router_core::{register_plugin, Plugin};
use apollo_spaceport::server::ReportSpaceport;
use derivative::Derivative;
Expand All @@ -28,7 +34,9 @@ use std::net::SocketAddr;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::Arc;
use tower::BoxError;
use tower::util::BoxService;
use tower::Layer;
use tower::{BoxError, ServiceExt};
use tracing_subscriber::prelude::*;
use tracing_subscriber::EnvFilter;

Expand Down Expand Up @@ -159,6 +167,7 @@ impl std::error::Error for ReportingError {}
struct Reporting {
config: Conf,
tx: tokio::sync::mpsc::Sender<SpaceportConfig>,
opentracing_layer: Option<OpenTracingLayer>,
}

#[derive(Debug, Deserialize, JsonSchema)]
Expand All @@ -168,6 +177,8 @@ struct Conf {
pub graph: Option<StudioGraph>,

pub opentelemetry: Option<OpenTelemetry>,

pub opentracing: Option<OpenTracingConfig>,
}

fn studio_graph() -> Option<StudioGraph> {
Expand Down Expand Up @@ -254,11 +265,29 @@ impl Plugin for Reporting {
}
tracing::debug!("terminating spaceport loop");
});

let mut opentracing_layer = None;
if let Some(opentracing_conf) = &configuration.opentracing {
opentracing_layer = OpenTracingLayer::new(opentracing_conf.clone())?.into();
}

Ok(Reporting {
config: configuration,
tx,
opentracing_layer,
})
}

fn subgraph_service(
&mut self,
_name: &str,
service: BoxService<SubgraphRequest, SubgraphResponse, BoxError>,
) -> BoxService<SubgraphRequest, SubgraphResponse, BoxError> {
match &self.opentracing_layer {
Some(opentracing_layer) => opentracing_layer.layer(service).boxed(),
None => service,
}
}
}

impl Reporting {
Expand Down

0 comments on commit 340c8d0

Please sign in to comment.