Skip to content

Commit

Permalink
fix: opentelemetry 0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Mar 8, 2024
1 parent 2e80d9e commit a3c5846
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ sessions-memory = "0.6"

# http
headers = "0.4"
http = "1"
http = "1.1"
http-body = "1"
http-body-util = "0.1"
hyper = { version = "1.2", features = ["server"] }
Expand All @@ -101,12 +101,12 @@ tokio-tungstenite = "0.21"
tokio-util = "0.7"

# OpenTelemetry
opentelemetry = { version = "0.21", default-features = false }
opentelemetry_sdk = { version = "0.21", default-features = false }
opentelemetry-prometheus = { version = "0.14", features = [
opentelemetry = { version = "0.22", default-features = false }
opentelemetry_sdk = { version = "0.22", default-features = false }
opentelemetry-prometheus = { version = "0.15", features = [
"prometheus-encoding",
] }
opentelemetry-semantic-conventions = { version = "0.13" }
opentelemetry-semantic-conventions = { version = "0.14" }
prometheus = "0.13"

# Tracing
Expand Down
4 changes: 2 additions & 2 deletions examples/otel/metrics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio::net::TcpListener;

use opentelemetry::{global, KeyValue};
use opentelemetry_sdk::{
metrics::{self, Aggregation, Instrument, MeterProvider, Stream},
metrics::{self, Aggregation, Instrument, MeterProviderBuilder, Stream},
Resource,
};

Expand Down Expand Up @@ -45,7 +45,7 @@ async fn main() -> Result<()> {
.unwrap(),
)
};
let provider = MeterProvider::builder()
let provider = MeterProviderBuilder::default()
.with_reader(exporter)
.with_resource(Resource::new([KeyValue::new("service.name", "viz")]))
.with_view(controller)
Expand Down
2 changes: 1 addition & 1 deletion examples/otel/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ viz = { workspace = true, features = ["otel-tracing"] }

tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
opentelemetry.workspace = true
opentelemetry-otlp = { version = "0.15", default-features = false, features = ["trace", "http-proto"] }
opentelemetry_sdk = { workspace = true, features = ["trace", "rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.21", features = ["rt-tokio-current-thread"]}
5 changes: 3 additions & 2 deletions examples/otel/tracing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use viz::{middleware::otel, serve, Request, Result, Router};

fn init_tracer() -> Tracer {
global::set_text_map_propagator(TraceContextPropagator::new());
opentelemetry_jaeger::new_agent_pipeline()
.with_service_name("viz")
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(opentelemetry_otlp::new_exporter().http())
.install_batch(TokioCurrentThread)
.unwrap()
}
Expand Down
27 changes: 18 additions & 9 deletions viz-core/src/middleware/otel/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ where
.map(|resp| {
active_requests.add(-1, &attributes);

attributes.push(HTTP_RESPONSE_STATUS_CODE.i64(i64::from(resp.status().as_u16())));
attributes.push(KeyValue::new(
HTTP_RESPONSE_STATUS_CODE,
i64::from(resp.status().as_u16()),
));

Check warning on line 130 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L127-L130

Added lines #L127 - L130 were not covered by tests

response_size.record(resp.content_length().unwrap_or(0), &attributes);

Expand All @@ -143,37 +146,43 @@ where
fn build_attributes(req: &Request, http_route: &str) -> Vec<KeyValue> {
let mut attributes = Vec::with_capacity(5);
// <https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md#http-server>
attributes.push(HTTP_ROUTE.string(http_route.to_string()));
attributes.push(KeyValue::new(HTTP_ROUTE, http_route.to_string()));

Check warning on line 149 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L149

Added line #L149 was not covered by tests

// <https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/docs/http/http-spans.md#common-attributes>
attributes.push(HTTP_REQUEST_METHOD.string(req.method().to_string()));
attributes.push(NETWORK_PROTOCOL_VERSION.string(format!("{:?}", req.version())));
attributes.push(KeyValue::new(HTTP_REQUEST_METHOD, req.method().to_string()));
attributes.push(KeyValue::new(
NETWORK_PROTOCOL_VERSION,
format!("{:?}", req.version()),
));

Check warning on line 156 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L152-L156

Added lines #L152 - L156 were not covered by tests

let remote_addr = req.remote_addr();
if let Some(remote_addr) = remote_addr {
attributes.push(CLIENT_ADDRESS.string(remote_addr.to_string()));
attributes.push(KeyValue::new(CLIENT_ADDRESS, remote_addr.to_string()));

Check warning on line 160 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L160

Added line #L160 was not covered by tests
}
if let Some(realip) = req.realip().map(|value| value.0).filter(|realip| {
remote_addr
.map(SocketAddr::ip)
.map_or(true, |remoteip| &remoteip != realip)
}) {
attributes.push(CLIENT_SOCKET_ADDRESS.string(realip.to_string()));
attributes.push(KeyValue::new(CLIENT_SOCKET_ADDRESS, realip.to_string()));

Check warning on line 167 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L167

Added line #L167 was not covered by tests
}

let uri = req.uri();
if let Some(host) = uri.host() {
attributes.push(SERVER_ADDRESS.string(host.to_string()));
attributes.push(KeyValue::new(SERVER_ADDRESS, host.to_string()));

Check warning on line 172 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L172

Added line #L172 was not covered by tests
}
if let Some(port) = uri
.port_u16()
.map(i64::from)
.filter(|port| *port != 80 && *port != 443)
{
attributes.push(SERVER_PORT.i64(port));
attributes.push(KeyValue::new(SERVER_PORT, port));

Check warning on line 179 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L179

Added line #L179 was not covered by tests
}

attributes.push(URL_SCHEME.string(uri.scheme().unwrap_or(&Scheme::HTTP).to_string()));
attributes.push(KeyValue::new(
URL_SCHEME,
uri.scheme().unwrap_or(&Scheme::HTTP).to_string(),
));

Check warning on line 185 in viz-core/src/middleware/otel/metrics.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/metrics.rs#L182-L185

Added lines #L182 - L185 were not covered by tests

attributes
}
17 changes: 9 additions & 8 deletions viz-core/src/middleware/otel/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ where
Ok(resp) => {
let resp = resp.into_response();
span.add_event("request.completed".to_string(), vec![]);
span.set_attribute(
HTTP_RESPONSE_STATUS_CODE.i64(i64::from(resp.status().as_u16())),
);
span.set_attribute(KeyValue::new(
HTTP_RESPONSE_STATUS_CODE,
i64::from(resp.status().as_u16()),
));

Check warning on line 103 in viz-core/src/middleware/otel/tracing.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L100-L103

Added lines #L100 - L103 were not covered by tests
if let Some(content_length) = resp.content_length() {
span.set_attribute(
HTTP_RESPONSE_BODY_SIZE
.i64(i64::try_from(content_length).unwrap_or(i64::MAX)),
);
span.set_attribute(KeyValue::new(
HTTP_RESPONSE_BODY_SIZE,
i64::try_from(content_length).unwrap_or(i64::MAX),
));

Check warning on line 108 in viz-core/src/middleware/otel/tracing.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L105-L108

Added lines #L105 - L108 were not covered by tests
}
if resp.status().is_server_error() {
span.set_status(Status::error(
Expand All @@ -120,7 +121,7 @@ where
Err(err) => {
span.add_event(
"request.error".to_string(),
vec![EXCEPTION_MESSAGE.string(err.to_string())],
vec![KeyValue::new(EXCEPTION_MESSAGE, err.to_string())],

Check warning on line 124 in viz-core/src/middleware/otel/tracing.rs

View check run for this annotation

Codecov / codecov/patch

viz-core/src/middleware/otel/tracing.rs#L124

Added line #L124 was not covered by tests
);
span.set_status(Status::error(err.to_string()));
span.end();
Expand Down

0 comments on commit a3c5846

Please sign in to comment.