Skip to content

Commit 557a020

Browse files
committed
Almost working
1 parent 17f8275 commit 557a020

File tree

5 files changed

+57
-36
lines changed

5 files changed

+57
-36
lines changed

newrelic/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ def _process_configuration(section):
400400
_process_setting(section, "distributed_tracing.enabled", "getboolean", None)
401401
_process_setting(section, "distributed_tracing.exclude_newrelic_header", "getboolean", None)
402402
_process_setting(section, "core_tracing.drop_inprocess_spans", "getboolean", None)
403+
_process_setting(section, "core_tracing.enabled", "getboolean", None)
403404
_process_setting(section, "span_events.enabled", "getboolean", None)
404405
_process_setting(section, "span_events.max_samples_stored", "getint", None)
405406
_process_setting(section, "span_events.attributes.enabled", "getboolean", None)

newrelic/core/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ def default_otlp_host(host):
820820

821821
_settings.distributed_tracing.enabled = _environ_as_bool("NEW_RELIC_DISTRIBUTED_TRACING_ENABLED", default=True)
822822
_settings.core_tracing.drop_inprocess_spans = _environ_as_bool("NEW_RELIC_CORE_TRACING_DROP_INPROCESS_SPANS", default=False)
823+
_settings.core_tracing.enabled = _environ_as_bool("NEW_RELIC_CORE_TRACING_ENABLED", default=True)
823824
_settings.distributed_tracing.exclude_newrelic_header = False
824825
_settings.span_events.enabled = _environ_as_bool("NEW_RELIC_SPAN_EVENTS_ENABLED", default=True)
825826
_settings.span_events.attributes.enabled = True

newrelic/core/node_mixin.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
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-
14+
import time
1515
from newrelic.core import attribute
1616
from newrelic.core.attribute_filter import DST_SPAN_EVENTS, DST_TRANSACTION_SEGMENTS
1717

@@ -70,69 +70,90 @@ def span_event(self, settings, base_attrs=None, parent_guid=None, attr_class=dic
7070
)
7171

7272
# intrinsics, user attrs, agent attrs
73+
74+
exit_span_attrs_present = attribute.SPAN_ENTITY_RELATIONSHIP_ATTRIBUTES & set(a_attrs)
7375
if settings.core_tracing.drop_inprocess_spans or settings.core_tracing.enabled:
74-
if not parent_guid:
75-
return [i_attrs, u_attrs, a_attrs]
76-
set_inprocess_attrs = attribute.SPAN_ENTITY_RELATIONSHIP_ATTRIBUTES
77-
set_a_attrs = set(a_attrs)
78-
exit_span_attrs_present = set_inprocess_attrs & set_a_attrs
76+
if self.__class__.__name__ == "RootNode":
77+
return [i_attrs, u_attrs, a_attrs, exit_span_attrs_present]
7978
if not exit_span_attrs_present:
8079
return None
8180
if settings.core_tracing.enabled:
82-
a_attrs["ids"] = [] # This is the list of span guids that share this unqiue exit span.
83-
84-
set_inprocess_attrs = set(attribute.SPAN_ENTITY_RELATIONSHIP_ATTRIBUTES)
85-
set_a_attrs = set(a_attrs)
86-
exit_span_attrs_present = set_inprocess_attrs & set_a_attrs
87-
return [i_attrs, u_attrs, a_attrs, exit_span_attrs_present]
88-
89-
def span_events(self, settings, base_attrs=None, parent_guid=None, attr_class=dict, ct_exit_spans=None, ct_processing_time=0):
90-
if settings.core_tracing.drop_inprocess_spans:
91-
ct_exit_spans = ct_exit_spans or {}
92-
span = self.span_event(settings, base_attrs=base_attrs, parent_guid=parent_guid, attr_class=attr_class)
9381
start_time = time.time()
82+
i_attrs["ids"] = [] # This is the list of span guids that share this unqiue exit span.
9483
parent_id = parent_guid
9584
if span: # span will be None if the span is an inprocess span.
96-
yield span
97-
parent_id = self.guid
98-
for child in self.children:
99-
for event in child.span_events( # noqa: UP028
100-
settings, base_attrs=base_attrs, parent_guid=parent_id, attr_class=attr_class, ct_exit_spans=ct_exit_spans, ct_processing_time=ct_processing_time
101-
):
102-
if event:
103-
yield event
104-
elif settings.core_tracing.enabled:
105-
ct_exit_spans = ct_exit_spans or {}
85+
span_attrs = "".join([span[2][key] for key in exit_spans])
86+
new_exit_span = span_attrs not in ct_exit_spans
87+
if parent_id: # If this is not the entry span.
88+
if new_exit_span:
89+
ct_exit_spans[span_attrs] = span
90+
else:
91+
ct_exit_spans[span_attrs][0]["ids"].append(span[0]["guid"])
92+
ct_processing_time += (time.time() - start_time)
93+
#print(f"parent {span[0]['traceId']} {parent_id} {span[0]['name']} {new_exit_span}")
94+
if not parent_id or new_exit_span:
95+
#print(f"{span}")
96+
import traceback
97+
traceback.print_stack()
98+
yield span
99+
return [i_attrs, u_attrs, a_attrs, exit_span_attrs_present]
100+
101+
def span_events(self, settings, base_attrs=None, parent_guid=None, attr_class=dict, ct_exit_spans=None, ct_processing_time=0):
102+
if settings.core_tracing.enabled:
103+
if ct_exit_spans is None:
104+
ct_exit_spans = {}
106105
span = self.span_event(settings, base_attrs=base_attrs, parent_guid=parent_guid, attr_class=attr_class)
107106
start_time = time.time()
108107
parent_id = parent_guid
109108
if span: # span will be None if the span is an inprocess span.
110-
span_attrs = "".join([value for key, value in span[2].items() if key in attribute.SPAN_ENTITY_RELATIONSHIP_ATTRIBUTES])
109+
span_attrs = "".join([span[2][key] for key in span[3]])
111110
new_exit_span = span_attrs not in ct_exit_spans
112111
if parent_id: # If this is not the entry span.
113112
if new_exit_span:
114113
ct_exit_spans[span_attrs] = span
115114
else:
116-
ct_exit_spans[span_attrs][2]["ids"] = event[2]["guid"]
115+
ct_exit_spans[span_attrs][0]["ids"].append(span[0]["guid"])
117116
ct_processing_time += (time.time() - start_time)
117+
#print(f"parent {span[0]['traceId']} {parent_id} {span[0]['name']} {new_exit_span}")
118118
if not parent_id or new_exit_span:
119+
#print(f"{span}")
120+
import traceback
121+
traceback.print_stack()
119122
yield span
120-
parent_id = self.guid
123+
parent_id = self.guid
121124
for child in self.children:
122125
for event in child.span_events( # noqa: UP028
123126
settings, base_attrs=base_attrs, parent_guid=parent_id, attr_class=attr_class, ct_exit_spans=ct_exit_spans, ct_processing_time=ct_processing_time
124127
):
125128
if event:
126129
start_time = time.time()
127-
span_attrs = "".join([value for key, value in event[2].items() if key in attribute.SPAN_ENTITY_RELATIONSHIP_ATTRIBUTES])
130+
span_attrs = "".join([event[2][key] for key in event[3]])
128131
new_exit_span = span_attrs not in ct_exit_spans
129132
if new_exit_span:
130133
ct_exit_spans[span_attrs] = event
131134
else:
132-
ct_exit_spans[span_attrs][2]["ids"] = event[2]["guid"]
135+
ct_exit_spans[span_attrs][0]["ids"].append(event[0]["guid"])
133136
ct_processing_time += (time.time() - start_time)
137+
#print(f"child {event[0]['traceId']} {parent_id} {event[0]['name']} {new_exit_span}")
134138
if new_exit_span:
139+
#print(f"{event}")
140+
import traceback
141+
traceback.print_stack()
135142
yield event
143+
elif settings.core_tracing.drop_inprocess_spans:
144+
ct_exit_spans = ct_exit_spans or {}
145+
span = self.span_event(settings, base_attrs=base_attrs, parent_guid=parent_guid, attr_class=attr_class)
146+
start_time = time.time()
147+
parent_id = parent_guid
148+
if span: # span will be None if the span is an inprocess span.
149+
yield span
150+
parent_id = self.guid
151+
for child in self.children:
152+
for event in child.span_events( # noqa: UP028
153+
settings, base_attrs=base_attrs, parent_guid=parent_id, attr_class=attr_class, ct_exit_spans=ct_exit_spans, ct_processing_time=ct_processing_time
154+
):
155+
if event:
156+
yield event
136157
else:
137158
yield self.span_event(settings, base_attrs=base_attrs, parent_guid=parent_guid, attr_class=attr_class)
138159

newrelic/core/stats_engine.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,7 @@ def record_transaction(self, transaction):
12701270
elif transaction.sampled:
12711271
ct_processing_time = 0
12721272
for event in transaction.span_events(self.__settings, ct_processing_time=ct_processing_time):
1273+
print(event)
12731274
self._span_events.add(event, priority=transaction.priority)
12741275
self._span_events.ct_processing_time += ct_processing_time
12751276

newrelic/core/transaction_node.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,7 @@ def _add_call_count(source, target):
622622

623623
def span_protos(self, settings, ct_process_time=0):
624624
for span in self.span_events(settings, attr_class=SpanProtoAttrs, ct_process_time=ct_process_time):
625-
if len(span) > 3:
626-
yield Span(trace_id=self.trace_id, intrinsics=span[0], user_attributes=span[1], agent_attributes=span[2]), span[3]
627-
else:
628-
yield Span(trace_id=self.trace_id, intrinsics=span[0], user_attributes=span[1], agent_attributes=span[2])
625+
yield Span(trace_id=self.trace_id, intrinsics=span[0], user_attributes=span[1], agent_attributes=span[2]), span[3]
629626

630627
def span_events(self, settings, attr_class=dict, ct_processing_time=0):
631628
base_attrs = attr_class(

0 commit comments

Comments
 (0)