11use relay_conventions:: name_for_op_and_attributes;
2- use relay_event_schema:: protocol:: Span ;
2+ use relay_event_schema:: protocol:: { Attributes , Span } ;
33use relay_protocol:: { Getter , GetterIter , Val } ;
44
55/// Constructs a name attribute for a span, following the rules defined in sentry-conventions.
@@ -19,6 +19,12 @@ pub fn name_for_span(span: &Span) -> Option<String> {
1919 ) )
2020}
2121
22+ /// Constructs a name attribute for a span, following the rules defined in sentry-conventions.
23+ pub fn name_for_attributes ( attributes : & Attributes ) -> Option < String > {
24+ let op = attributes. get_value ( "sentry.op" ) ?. as_str ( ) ?;
25+ Some ( name_for_op_and_attributes ( op, & AttributeGetter ( attributes) ) )
26+ }
27+
2228struct EmptyGetter { }
2329
2430impl Getter for EmptyGetter {
@@ -39,6 +45,18 @@ impl<'a, T: Getter> Getter for EscapedGetter<'a, T> {
3945 }
4046}
4147
48+ /// A custom getter for [`Attributes`] which only resolves values based on the attribute name.
49+ ///
50+ /// This [`Getter`] does not implement nested traversals, which is the behaviour required for
51+ /// [`name_for_op_and_attributes`].
52+ struct AttributeGetter < ' a > ( & ' a Attributes ) ;
53+
54+ impl < ' a > Getter for AttributeGetter < ' a > {
55+ fn get_value ( & self , path : & str ) -> Option < Val < ' _ > > {
56+ self . 0 . get_value ( path) . map ( |value| value. into ( ) )
57+ }
58+ }
59+
4260#[ cfg( test) ]
4361mod tests {
4462 use relay_event_schema:: protocol:: SpanData ;
@@ -47,7 +65,7 @@ mod tests {
4765 use super :: * ;
4866
4967 #[ test]
50- fn falls_back_to_op_when_no_templates_defined ( ) {
68+ fn test_span_falls_back_to_op_when_no_templates_defined ( ) {
5169 let span = Span {
5270 op : Annotated :: new ( "foo" . to_owned ( ) ) ,
5371 ..Default :: default ( )
@@ -56,22 +74,32 @@ mod tests {
5674 }
5775
5876 #[ test]
59- fn uses_the_first_matching_template ( ) {
77+ fn test_attributes_falls_back_to_op_when_no_templates_defined ( ) {
78+ let attributes = Attributes :: from ( [ (
79+ "sentry.op" . to_owned ( ) ,
80+ Annotated :: new ( "foo" . to_owned ( ) . into ( ) ) ,
81+ ) ] ) ;
82+
83+ assert_eq ! ( name_for_attributes( & attributes) , Some ( "foo" . to_owned( ) ) ) ;
84+ }
85+
86+ #[ test]
87+ fn test_span_uses_the_first_matching_template ( ) {
6088 let span = Span {
6189 op : Annotated :: new ( "db" . to_owned ( ) ) ,
6290 data : Annotated :: new ( SpanData {
6391 other : Object :: from ( [
6492 (
65- "db.query.summary" . into ( ) ,
66- Value :: String ( "SELECT users" . into ( ) ) . into ( ) ,
93+ "db.query.summary" . to_owned ( ) ,
94+ Value :: String ( "SELECT users" . to_owned ( ) ) . into ( ) ,
6795 ) ,
6896 (
69- "db.operation.name" . into ( ) ,
70- Value :: String ( "INSERT" . into ( ) ) . into ( ) ,
97+ "db.operation.name" . to_owned ( ) ,
98+ Value :: String ( "INSERT" . to_owned ( ) ) . into ( ) ,
7199 ) ,
72100 (
73- "db.collection.name" . into ( ) ,
74- Value :: String ( "widgets" . into ( ) ) . into ( ) ,
101+ "db.collection.name" . to_owned ( ) ,
102+ Value :: String ( "widgets" . to_owned ( ) ) . into ( ) ,
75103 ) ,
76104 ] ) ,
77105 ..Default :: default ( )
@@ -82,18 +110,45 @@ mod tests {
82110 }
83111
84112 #[ test]
85- fn uses_fallback_templates_when_data_is_missing ( ) {
113+ fn test_attributes_uses_the_first_matching_template ( ) {
114+ let attributes = Attributes :: from ( [
115+ (
116+ "sentry.op" . to_owned ( ) ,
117+ Annotated :: new ( "db" . to_owned ( ) . into ( ) ) ,
118+ ) ,
119+ (
120+ "db.query.summary" . to_owned ( ) ,
121+ Annotated :: new ( "SELECT users" . to_owned ( ) . into ( ) ) ,
122+ ) ,
123+ (
124+ "db.operation.name" . to_owned ( ) ,
125+ Annotated :: new ( "INSERT" . to_owned ( ) . into ( ) ) ,
126+ ) ,
127+ (
128+ "db.collection.name" . to_owned ( ) ,
129+ Annotated :: new ( "widgets" . to_owned ( ) . into ( ) ) ,
130+ ) ,
131+ ] ) ;
132+
133+ assert_eq ! (
134+ name_for_attributes( & attributes) ,
135+ Some ( "SELECT users" . to_owned( ) )
136+ ) ;
137+ }
138+
139+ #[ test]
140+ fn test_span_uses_fallback_templates_when_data_is_missing ( ) {
86141 let span = Span {
87142 op : Annotated :: new ( "db" . to_owned ( ) ) ,
88143 data : Annotated :: new ( SpanData {
89144 other : Object :: from ( [
90145 (
91- "db.operation.name" . into ( ) ,
92- Value :: String ( "INSERT" . into ( ) ) . into ( ) ,
146+ "db.operation.name" . to_owned ( ) ,
147+ Value :: String ( "INSERT" . to_owned ( ) ) . into ( ) ,
93148 ) ,
94149 (
95- "db.collection.name" . into ( ) ,
96- Value :: String ( "widgets" . into ( ) ) . into ( ) ,
150+ "db.collection.name" . to_owned ( ) ,
151+ Value :: String ( "widgets" . to_owned ( ) ) . into ( ) ,
97152 ) ,
98153 ] ) ,
99154 ..Default :: default ( )
@@ -104,11 +159,47 @@ mod tests {
104159 }
105160
106161 #[ test]
107- fn falls_back_to_hardcoded_name_when_nothing_matches ( ) {
162+ fn test_attributes_uses_fallback_templates_when_data_is_missing ( ) {
163+ let attributes = Attributes :: from ( [
164+ (
165+ "sentry.op" . to_owned ( ) ,
166+ Annotated :: new ( "db" . to_owned ( ) . into ( ) ) ,
167+ ) ,
168+ (
169+ "db.operation.name" . to_owned ( ) ,
170+ Annotated :: new ( "INSERT" . to_owned ( ) . into ( ) ) ,
171+ ) ,
172+ (
173+ "db.collection.name" . to_owned ( ) ,
174+ Annotated :: new ( "widgets" . to_owned ( ) . into ( ) ) ,
175+ ) ,
176+ ] ) ;
177+
178+ assert_eq ! (
179+ name_for_attributes( & attributes) ,
180+ Some ( "INSERT widgets" . to_owned( ) )
181+ ) ;
182+ }
183+
184+ #[ test]
185+ fn test_span_falls_back_to_hardcoded_name_when_nothing_matches ( ) {
108186 let span = Span {
109187 op : Annotated :: new ( "db" . to_owned ( ) ) ,
110188 ..Default :: default ( )
111189 } ;
112190 assert_eq ! ( name_for_span( & span) , Some ( "Database operation" . to_owned( ) ) ) ;
113191 }
192+
193+ #[ test]
194+ fn test_attributes_falls_back_to_hardcoded_name_when_nothing_matches ( ) {
195+ let attributes = Attributes :: from ( [ (
196+ "sentry.op" . to_owned ( ) ,
197+ Annotated :: new ( "db" . to_owned ( ) . into ( ) ) ,
198+ ) ] ) ;
199+
200+ assert_eq ! (
201+ name_for_attributes( & attributes) ,
202+ Some ( "Database operation" . to_owned( ) )
203+ ) ;
204+ }
114205}
0 commit comments