Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add count transactions toward root project. ([#1734](https://github.com/getsentry/relay/pull/1734))
- Add or remove the profile ID on the transaction's profiling context. ([#1801](https://github.com/getsentry/relay/pull/1801))
- Implement a new sampling algorithm with factors and multi-matching. ([#1790](https://github.com/getsentry/relay/pull/1790)
- Add Cloud Resource context. ([#1854](https://github.com/getsentry/relay/pull/1854))

**Bug Fixes**:

Expand Down
106 changes: 106 additions & 0 deletions relay-general/src/protocol/contexts/cloud_resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::types::{Annotated, Object, Value};

/// Cloud Resource Context.
///
/// This context describes the cloud resource the event originated from.
///
/// Example:
///
/// ```json
/// "cloud_resource": {
/// "cloud.account.id": "499517922981",
/// "cloud.provider": "aws",
/// "cloud.platform": "aws_ec2",
/// "cloud.region": "us-east-1",
/// "cloud.vavailability_zone": "us-east-1e",
/// "host.id": "i-07d3301208fe0a55a",
/// "host.type": "t2.large"
/// }
/// ```
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[cfg_attr(feature = "jsonschema", derive(JsonSchema))]
pub struct CloudResourceContext {
/// The cloud account ID the resource is assigned to.
#[metastructure(pii = "maybe")]
#[metastructure(field = "cloud.account.id")]
pub cloud_account_id: Annotated<String>,

/// Name of the cloud provider.
#[metastructure(field = "cloud.provider")]
pub cloud_provider: Annotated<String>,

/// The cloud platform in use.
/// The prefix of the service SHOULD match the one specified in cloud_provider.
#[metastructure(field = "cloud.platform")]
pub cloud_platform: Annotated<String>,

/// The geographical region the resource is running.
#[metastructure(field = "cloud.region")]
pub cloud_region: Annotated<String>,

/// The zone where the resource is running.
#[metastructure(field = "cloud.availability_zone")]
pub cloud_availability_zone: Annotated<String>,

/// Unique host ID.
#[metastructure(pii = "maybe")]
#[metastructure(field = "host.id")]
pub host_id: Annotated<String>,

/// Machine type of the host.
#[metastructure(field = "host.type")]
pub host_type: Annotated<String>,

/// Additional arbitrary fields for forwards compatibility.
#[metastructure(additional_properties, retain = "true", pii = "maybe")]
pub other: Object<Value>,
}

impl CloudResourceContext {
/// The key under which a runtime context is generally stored (in `Contexts`).
pub fn default_key() -> &'static str {
"cloud_resource"
}
}

#[cfg(test)]
mod tests {
use crate::protocol::Context;

use super::*;

#[test]
pub(crate) fn test_cloud_resource_context_roundtrip() {
let json = r#"{
"cloud.account.id": "499517922981",
"cloud.provider": "aws",
"cloud.platform": "aws_ec2",
"cloud.region": "us-east-1",
"cloud.availability_zone": "us-east-1e",
"host.id": "i-07d3301208fe0a55a",
"host.type": "t2.large",
"other": "value",
"type": "cloudresource"
}"#;
let context = Annotated::new(Context::CloudResource(Box::new(CloudResourceContext {
cloud_account_id: Annotated::new("499517922981".into()),
cloud_provider: Annotated::new("aws".into()),
cloud_platform: Annotated::new("aws_ec2".into()),
cloud_region: Annotated::new("us-east-1".into()),
cloud_availability_zone: Annotated::new("us-east-1e".into()),
host_id: Annotated::new("i-07d3301208fe0a55a".into()),
host_type: Annotated::new("t2.large".into()),
other: {
let mut map = Object::new();
map.insert(
"other".to_string(),
Annotated::new(Value::String("value".to_string())),
);
map
},
})));

assert_eq!(context, Annotated::from_json(json).unwrap());
assert_eq!(json, context.to_json_pretty().unwrap());
}
}
7 changes: 6 additions & 1 deletion relay-general/src/protocol/contexts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ mod trace;
pub use trace::*;
mod otel;
pub use otel::*;
mod cloud_resource;
pub use cloud_resource::*;

use crate::types::{Annotated, FromValue, Object, Value};
use crate::user_agent::{ClientHints, RawUserAgentInfo};
Expand Down Expand Up @@ -58,8 +60,10 @@ pub enum Context {
Reprocessing(Box<ReprocessingContext>),
/// Response information.
Response(Box<ResponseContext>),
/// OpenTelemetry information
/// OpenTelemetry information.
Otel(Box<OtelContext>),
/// Cloud resource information.
CloudResource(Box<CloudResourceContext>),
/// Additional arbitrary fields for forwards compatibility.
#[metastructure(fallback_variant)]
Other(#[metastructure(pii = "true")] Object<Value>),
Expand All @@ -83,6 +87,7 @@ impl Context {
Context::Monitor(_) => Some(MonitorContext::default_key()),
Context::Response(_) => Some(ResponseContext::default_key()),
Context::Otel(_) => Some(OtelContext::default_key()),
Context::CloudResource(_) => Some(CloudResourceContext::default_key()),
Context::Other(_) => None,
}
}
Expand Down
70 changes: 70 additions & 0 deletions relay-general/tests/snapshots/test_fixtures__event_schema.snap
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,73 @@ expression: "relay_general::protocol::event_json_schema()"
}
]
},
"CloudResourceContext": {
"description": " Cloud Resource Context.\n\n This context describes the cloud resource the event originated from.\n\n Example:\n\n ```json\n \"cloud_resource\": {\n \"cloud.account.id\": \"499517922981\",\n \"cloud.provider\": \"aws\",\n \"cloud.platform\": \"aws_ec2\",\n \"cloud.region\": \"us-east-1\",\n \"cloud.vavailability_zone\": \"us-east-1e\",\n \"host.id\": \"i-07d3301208fe0a55a\",\n \"host.type\": \"t2.large\"\n }\n ```",
"anyOf": [
{
"type": "object",
"properties": {
"cloud.account.id": {
"description": " The cloud account ID the resource is assigned to.",
"default": null,
"type": [
"string",
"null"
]
},
"cloud.availability_zone": {
"description": " The zone where the resource is running.",
"default": null,
"type": [
"string",
"null"
]
},
"cloud.platform": {
"description": " The cloud platform in use.\n The prefix of the service SHOULD match the one specified in cloud_provider.",
"default": null,
"type": [
"string",
"null"
]
},
"cloud.provider": {
"description": " Name of the cloud provider.",
"default": null,
"type": [
"string",
"null"
]
},
"cloud.region": {
"description": " The geographical region the resource is running.",
"default": null,
"type": [
"string",
"null"
]
},
"host.id": {
"description": " Unique host ID.",
"default": null,
"type": [
"string",
"null"
]
},
"host.type": {
"description": " Machine type of the host.",
"default": null,
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
}
]
},
"CodeId": {
"type": "string"
},
Expand Down Expand Up @@ -839,6 +906,9 @@ expression: "relay_general::protocol::event_json_schema()"
{
"$ref": "#/definitions/OtelContext"
},
{
"$ref": "#/definitions/CloudResourceContext"
},
{
"type": "object",
"additionalProperties": true
Expand Down