Skip to content

Commit

Permalink
Save stdout and stderr of a subprocess that sends Zipkin spans to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
cattibrie committed Aug 12, 2019
1 parent 69c9dae commit a55af72
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/python/pants/reporting/zipkin_reporter.py
Expand Up @@ -6,7 +6,6 @@
import subprocess
import time

import requests
from py_zipkin import Encoding, Kind, get_default_tracer
from py_zipkin.encoding import Span, get_encoder
from py_zipkin.exception import ZipkinError
Expand All @@ -27,8 +26,9 @@


class HTTPTransportHandler(BaseTransportHandler):
def __init__(self, endpoint):
def __init__(self, endpoint, zipkin_spans_dir):
self.endpoint = endpoint
self.zipkin_spans_dir = zipkin_spans_dir

def get_max_payload_bytes(self):
return None
Expand All @@ -40,9 +40,13 @@ def send(self, file_path):
'-H', '"Content-Type: application/json"',
'--data', '@' + file_path,
self.endpoint]
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
file_path_to_stdout_stderr = os.path.join(self.zipkin_spans_dir, 'std_out_err_output')
p = subprocess.Popen(args, stdin=subprocess.DEVNULL, stdout=open(file_path_to_stdout_stderr, 'w'), stderr=subprocess.STDOUT, close_fds=False)

logger.debug("Sending spans to Zipkin server from pid: {}".format(p.pid))
logger.debug(
"stdout and stderr for pid {} are located at '{}'".format(p.pid, file_path_to_stdout_stderr)
)

except Exception as err:
logger.error("Failed to post the payload to zipkin server. Error {}".format(err))
Expand All @@ -68,14 +72,15 @@ def __init__(self, run_tracker, settings, endpoint, trace_id, parent_id, sample_
"""
super().__init__(run_tracker, settings)
# Create a transport handler
self.handler = HTTPTransportHandler(endpoint)
self.trace_id = trace_id
self.parent_id = parent_id
self.sample_rate = float(sample_rate)
self.endpoint = endpoint
self.tracer = get_default_tracer()
self.run_tracker = run_tracker
self.service_name_prefix = service_name_prefix
self.zipkin_spans_dir = os.path.join(self.run_tracker.run_info_dir, 'zipkin')
self.handler = HTTPTransportHandler(endpoint, self.zipkin_spans_dir)

def start_workunit(self, workunit):
"""Implementation of Reporter callback."""
Expand Down Expand Up @@ -149,9 +154,8 @@ def start_workunit(self, workunit):
span.start_timestamp = workunit.start_time
if first_span and span.zipkin_attrs.is_sampled:
span.logging_context.start_timestamp = workunit.start_time
if span.encoding in (Encoding.V1_JSON, Encoding.V2_JSON):
span.logging_context.emit_spans = emit_spans.__get__(span.logging_context, ZipkinLoggingContext)
span.logging_context.zipkin_spans_dir = os.path.join(self.run_tracker.run_info_dir, 'zipkin')
span.logging_context.emit_spans = emit_spans.__get__(span.logging_context, ZipkinLoggingContext)
span.logging_context.zipkin_spans_dir = self.zipkin_spans_dir
span.logging_context.encoding = span.encoding
workunit.zipkin_span = span

Expand Down

0 comments on commit a55af72

Please sign in to comment.