-
Notifications
You must be signed in to change notification settings - Fork 178
feat: allow OTLP exporting by extending existing logger configs #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prestwich
wants to merge
3
commits into
flashbots:develop
Choose a base branch
from
prestwich:prestwich/otlp
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| use opentelemetry::{trace::TracerProvider, KeyValue}; | ||
| use opentelemetry_sdk::{trace::SdkTracerProvider, Resource}; | ||
| use opentelemetry_semantic_conventions::{ | ||
| resource::{DEPLOYMENT_ENVIRONMENT_NAME, SERVICE_NAME, SERVICE_VERSION}, | ||
| SCHEMA_URL, | ||
| }; | ||
| use tracing_subscriber::Layer; | ||
|
|
||
| /// Drop guard for the OTLP exporter. This will shutdown the exporter when | ||
| /// dropped, and generally should be held for the lifetime of the `main` | ||
| /// function. | ||
| /// | ||
| /// The guard may be used to produce a [`tracing`] layer that can be added to | ||
| /// an existing [`tracing::Subscriber`]. | ||
| #[derive(Debug)] | ||
| pub struct OtlpGuard(SdkTracerProvider); | ||
|
|
||
| impl OtlpGuard { | ||
| /// Get a tracer from the provider. | ||
| fn tracer(&self, s: &'static str) -> opentelemetry_sdk::trace::Tracer { | ||
| self.0.tracer(s) | ||
| } | ||
|
|
||
| /// Create a filtered tracing layer. | ||
| pub fn layer<S>(&self) -> impl Layer<S> | ||
| where | ||
| S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>, | ||
| { | ||
| let tracer = self.tracer("tracing-otel-subscriber"); | ||
| tracing_opentelemetry::layer().with_tracer(tracer) | ||
| } | ||
| } | ||
|
|
||
| /// Configuration for the OTLP system. Currently this allows configuring the | ||
| /// deployment environment name. | ||
| /// | ||
| /// OTEL and OTLP are configured primarily via environment variables. The | ||
| /// standard variables are as follows: | ||
| /// | ||
| /// - `OTEL_TRACES_EXPORTER` - Set the exporter to be used. Generally should be | ||
| /// set to `otlp`. Other options include `none`, `zipkin`, etc. See the | ||
| /// [relevant documentation] for more details. | ||
| /// | ||
| /// - `OTEL_EXPORTER_OTLP_ENDPOINT` - Set to the URL endpoint to which to send | ||
| /// OTLP data. If not set, traces will not be exported. This will typically | ||
| /// be a URL ending in port 4317 (grpc) or 4318 (http). | ||
| /// | ||
| /// - `OTEL_EXPORTER_OTLP_PROTOCOL` - Specifies the OTLP transport protocol to | ||
| /// be used for all telemetry data. Acceptable values are `grpc`, | ||
| /// `http/protobuf`, and `http/json`. | ||
| /// | ||
| /// For advanced exporter configuration via environment variables, see the | ||
| /// [OTLP documentation]. | ||
| /// | ||
| /// [relevant documentation]: https://opentelemetry.io/docs/instrumentation/js/exporters/#environment-variables | ||
| /// [OTLP documentation]: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/ | ||
| #[derive(Default, PartialEq, Eq, Clone, Debug, serde::Deserialize)] | ||
| #[non_exhaustive] | ||
| pub struct OtlpConfig { | ||
| /// The name of the deployment environment (a.k.a deployment tier), e.g. | ||
| /// "production", "staging". [See documentation here.] | ||
| /// | ||
| /// [See documentation here.]: https://opentelemetry.io/docs/specs/semconv/resource/deployment-environment/ | ||
| pub otlp_environment: String, | ||
| } | ||
|
|
||
| impl OtlpConfig { | ||
| /// Default OTEL configuration for development. | ||
| pub fn dev() -> Self { | ||
| Self { | ||
| otlp_environment: "development".to_owned(), | ||
| } | ||
| } | ||
|
|
||
| /// Instantiate a new OTEL configuration, with default values. | ||
| pub fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| /// Set the environment name. | ||
| pub fn with_environment(mut self, name: impl Into<String>) -> Self { | ||
| self.otlp_environment = name.into(); | ||
| self | ||
| } | ||
|
|
||
| fn resource(&self) -> Resource { | ||
| Resource::builder() | ||
| .with_schema_url( | ||
| [ | ||
| KeyValue::new(SERVICE_NAME, env!("CARGO_PKG_NAME")), | ||
| KeyValue::new(SERVICE_VERSION, env!("CARGO_PKG_VERSION")), | ||
| KeyValue::new(DEPLOYMENT_ENVIRONMENT_NAME, self.otlp_environment.clone()), | ||
| ], | ||
| SCHEMA_URL, | ||
| ) | ||
| .build() | ||
| } | ||
|
|
||
| /// Instantiate a new OTEL provider, with a simple batch exporter, and | ||
| /// start relevant tasks. Return a guard that will shut down the provider | ||
| /// and exporter when dropped. | ||
| pub fn provider(&self) -> OtlpGuard { | ||
| let exporter = opentelemetry_otlp::SpanExporter::builder() | ||
| .with_http() | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let provider = SdkTracerProvider::builder() | ||
| // Customize sampling strategy | ||
| // If export trace to AWS X-Ray, you can use XrayIdGenerator | ||
| .with_resource(self.resource()) | ||
| .with_batch_exporter(exporter) | ||
| .build(); | ||
|
|
||
| OtlpGuard(provider) | ||
| } | ||
|
|
||
| /// Create a new OTEL provider, returning both the guard and a tracing | ||
| /// layer that can be added to a subscriber. | ||
| pub fn into_guard_and_layer<S>(self) -> (OtlpGuard, impl Layer<S>) | ||
| where | ||
| S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>, | ||
| { | ||
| let guard = self.provider(); | ||
| let layer = guard.layer(); | ||
| (guard, layer) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type 'Format' is ambiguous and should be fully qualified as 'Format<Full, SystemTime>' to match the pattern used in the 'builder()' method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i go the other way. including default type args you don't have to is overly verbose