Skip to content

Commit

Permalink
address O nit feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianbarbu committed Aug 22, 2023
1 parent 82edc5d commit 5af99a9
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions logger/src/dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::{path::Path, str::FromStr, time::SystemTime};
use async_broadcast::{broadcast, Sender};
use async_trait::async_trait;
use chrono::NaiveDateTime;
use opentelemetry_proto::tonic::{
common::v1::{any_value, KeyValue},
trace::v1::{ResourceSpans, ScopeSpans, Span},
};
use opentelemetry_proto::tonic::trace::v1::{ResourceSpans, ScopeSpans, Span};
use prost_types::Timestamp;
use serde_json::Value;
use shuttle_common::{
Expand Down Expand Up @@ -180,14 +177,13 @@ impl Log {
schema_url: _,
} = resource_spans;

// TODO: we should get both of these attributes in the same function and avoid this clone.
let resource = resource?;
let shuttle_service_name = get_attribute(resource.clone().attributes, "service.name")?;

// Try to get the deployment_id from the resource attributes, this will be the case for the runtimes,
// they add the deployment_id to the otlp tracer config.
let mut fields = from_any_value_kv_to_serde_json_map(resource?.attributes);
let shuttle_service_name = fields.remove("service.name")?.as_str()?.to_string();
// TODO: should this be named "deployment.id" to conform to otlp standard?
let deployment_id = get_attribute(resource.attributes, "deployment_id");
let deployment_id = fields
.remove("deployment_id")?
.as_str()
.map(|inner| inner.to_string());

let logs = scope_spans
.into_iter()
Expand Down Expand Up @@ -221,8 +217,11 @@ impl Log {
deployment_id: Option<String>,
) -> Option<Vec<Self>> {
// If we didn't find the id in the resource span, check the inner spans.
let deployment_id =
deployment_id.or(get_attribute(span.attributes.clone(), "deployment_id"))?;
let mut span_fields = from_any_value_kv_to_serde_json_map(span.attributes);
let deployment_id = deployment_id.or(span_fields
.remove("deployment_id")?
.as_str()
.map(|inner| inner.to_string()))?;

let mut logs: Vec<Self> = span
.events
Expand All @@ -248,16 +247,15 @@ impl Log {

Some(Log {
shuttle_service_name: shuttle_service_name.to_string(),
deployment_id: deployment_id.to_string(),
deployment_id: deployment_id.clone(),
timestamp: DateTime::from_utc(naive, Utc),
level: level.as_str()?.parse().ok()?,
fields: Value::Object(fields),
})
})
.collect();

let mut fields = from_any_value_kv_to_serde_json_map(span.attributes);
fields.insert(
span_fields.insert(
MESSAGE_KEY.to_string(),
format!("[span] {}", span.name).into(),
);
Expand All @@ -277,7 +275,7 @@ impl Log {
),
// Span level doesn't exist so this info is not relevant.
level: LogLevel::Info,
fields: Value::Object(fields),
fields: Value::Object(span_fields),
});

Some(logs)
Expand All @@ -294,16 +292,3 @@ impl From<Log> for LogItem {
}
}
}

/// Get an attribute with the given key
fn get_attribute(attributes: Vec<KeyValue>, key: &str) -> Option<String> {
match attributes
.into_iter()
.find(|kv| kv.key == key)?
.value?
.value?
{
any_value::Value::StringValue(s) => Some(s),
_ => None,
}
}

0 comments on commit 5af99a9

Please sign in to comment.