1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14- import time
14+
1515from newrelic .core import attribute
1616from newrelic .core .attribute_filter import DST_SPAN_EVENTS , DST_TRANSACTION_SEGMENTS
1717
@@ -50,19 +50,13 @@ def get_trace_segment_params(self, settings, params=None):
5050 return _params
5151
5252 def span_event (self , settings , base_attrs = None , parent_guid = None , attr_class = dict , ct_exit_spans = None ):
53- if ct_exit_spans is None :
54- ct_exit_spans = {}
5553 i_attrs = (base_attrs and base_attrs .copy ()) or attr_class ()
5654 i_attrs ["type" ] = "Span"
5755 i_attrs ["name" ] = i_attrs .get ("name" ) or self .name
5856 i_attrs ["guid" ] = self .guid
5957 i_attrs ["timestamp" ] = int (self .start_time * 1000 )
6058 i_attrs ["duration" ] = self .duration
6159 i_attrs ["category" ] = i_attrs .get ("category" ) or "generic"
62- # TODO: limit intrinsic attributes but this likely requires changes in the pipeline.
63- #if settings.distributed_tracing.minimize_attributes.enabled:
64- # i_ct_attrs = {"type", "name", "guid", "parentId", "transaction.name", "traceId", "timestamp", "duration", "nr.entryPoint", "transactionId"}
65- # i_attrs = {key: value for key, value in i_attrs.items() if key in i_ct_attrs}
6660
6761 if parent_guid :
6862 i_attrs ["parentId" ] = parent_guid
@@ -71,51 +65,55 @@ def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dic
7165 self .agent_attributes , settings .attribute_filter , DST_SPAN_EVENTS , attr_class = attr_class
7266 )
7367 u_attrs = self .processed_user_attributes
74- if settings .distributed_tracing .unique_spans .enabled :
75- # ids is the list of span guids that share this unqiue exit span.
76- u_attrs ["nr.ids" ] = []
7768
7869 u_attrs = attribute .resolve_user_attributes (
7970 u_attrs , settings .attribute_filter , DST_SPAN_EVENTS , attr_class = attr_class
8071 )
72+ if not hasattr (self , "partial_granularity_sampled" ):
73+ # intrinsics, user attrs, agent attrs
74+ return [i_attrs , u_attrs , a_attrs ]
75+ else :
76+ if ct_exit_spans is None :
77+ ct_exit_spans = {}
8178
82- if settings . distributed_tracing . drop_inprocess_spans . enabled or settings .distributed_tracing .unique_spans . enabled :
79+ partial_granularity_type = settings .distributed_tracing .sampler . partial_granularity . type
8380 exit_span_attrs_present = attribute .SPAN_ENTITY_RELATIONSHIP_ATTRIBUTES & set (a_attrs )
84- # If this is the entry node, always return it.
85- if i_attrs .get ("nr.entryPoint" ):
86- return [ i_attrs , u_attrs , {}] if settings . distributed_tracing . minimize_attributes . enabled else [ i_attrs , u_attrs , a_attrs ]
87- # If this is the an LLM node, always return it.
88- if a_attrs . get ( "llm" ) or i_attrs [ "name" ]. startswith ( "Llm/" ) :
89- return [ i_attrs , u_attrs , { "llm" : True }] if settings . distributed_tracing . minimize_attributes . enabled else [i_attrs , u_attrs , a_attrs ]
81+ # If this is the entry node or an LLM span always return it.
82+ if i_attrs .get ("nr.entryPoint" ) or i_attrs [ "name" ]. startswith ( "Llm/" ) :
83+ if partial_granularity_type == "reduced" :
84+ return [ i_attrs , u_attrs , a_attrs ]
85+ else :
86+ return [i_attrs , {}, {} ]
9087 # If the span is not an exit span, skip it by returning None.
9188 if not exit_span_attrs_present :
9289 return None
93- # If the span is an exit span but unique spans is enabled, we need to check
94- # for uniqueness before returning it.
95- if settings .distributed_tracing .unique_spans .enabled :
90+ # If the span is an exit span and we are in reduced mode (meaning no attribute dropping),
91+ # just return the exit span as is.
92+ if partial_granularity_type == "reduced" :
93+ return [i_attrs , u_attrs , a_attrs ]
94+ else :
9695 a_minimized_attrs = attr_class ({key : a_attrs [key ] for key in exit_span_attrs_present })
96+ # If we are in essential mode return the span with minimized attributes.
97+ if partial_granularity_type == "essential" :
98+ return [i_attrs , {}, a_minimized_atts ]
99+ # If the span is an exit span but span compression (compact) is enabled, we need to check
100+ # for uniqueness before returning it.
97101 # Combine all the entity relationship attr values into a string to be
98102 # used as the hash to check for uniqueness.
99- # TODO: use attr value name rather than str casting for infinite tracing.
100103 span_attrs = "" .join ([str (a_minimized_attrs [key ]) for key in exit_span_attrs_present ])
101104 new_exit_span = span_attrs not in ct_exit_spans
102105 # If this is a new exit span, add it to the known ct_exit_spans and return it.
103106 if new_exit_span :
104- u_attrs ["nr.durations" ] = self .duration
105- ct_exit_spans [span_attrs ] = [u_attrs ]
106- return [i_attrs , u_attrs , a_minimized_attrs ] if settings .distributed_tracing .minimize_attributes .enabled else [i_attrs , u_attrs , a_attrs ]
107+ # ids is the list of span guids that share this unqiue exit span.
108+ a_minimized_attrs ["nr.ids" ] = []
109+ a_minimized_attrs ["nr.durations" ] = self .duration
110+ ct_exit_spans [span_attrs ] = [a_minimized_attrs ]
111+ return [i_attrs , {}, a_minimized_atts ]
107112 # If this is an exit span we've already seen, add it's guid to the list
108113 # of ids on the seen span and return None.
109- # For now add ids to user attributes list
110114 ct_exit_spans [span_attrs ][0 ]["nr.ids" ].append (self .guid )
111115 ct_exit_spans [span_attrs ][0 ]["nr.durations" ] += self .duration
112-
113116 return None
114- elif settings .distributed_tracing .minimize_attributes .enabled :
115- # Drop all non-entity relationship attributes from the span.
116- exit_span_attrs_present = attribute .SPAN_ENTITY_RELATIONSHIP_ATTRIBUTES & set (a_attrs )
117- a_attrs = attr_class ({key : a_attrs [key ] for key in exit_span_attrs_present })
118- return [i_attrs , u_attrs , a_attrs ]
119117
120118 def span_events (self , settings , base_attrs = None , parent_guid = None , attr_class = dict , ct_exit_spans = None ):
121119 span = self .span_event (settings , base_attrs = base_attrs , parent_guid = parent_guid , attr_class = attr_class , ct_exit_spans = ct_exit_spans )
0 commit comments