def export(self, events, start_time_ns, end_time_ns): """Export events to an HTTP endpoint. :param events: The event dictionary from a `ddtrace.profiling.recorder.Recorder`. :param start_time_ns: The start time of recording. :param end_time_ns: The end time of recording. """ if not self.endpoint: raise InvalidEndpoint("Endpoint is empty") common_headers = { "DD-API-KEY": self.api_key.encode(), } profile = super(PprofHTTPExporter, self).export(events, start_time_ns, end_time_ns) s = six.BytesIO() with gzip.GzipFile(fileobj=s, mode="wb") as gz: gz.write(profile.SerializeToString()) fields = { "runtime-id": RUNTIME_ID, "recording-start": (datetime.datetime.utcfromtimestamp( start_time_ns / 1e9).replace(microsecond=0).isoformat() + "Z").encode(), "recording-end": (datetime.datetime.utcfromtimestamp( end_time_ns / 1e9).replace(microsecond=0).isoformat() + "Z").encode(), "runtime": PYTHON_IMPLEMENTATION, "format": b"pprof", "type": b"cpu+alloc+exceptions", "chunk-data": s.getvalue(), } service_name = self.service_name or os.path.basename( profile.string_table[profile.mapping[0].filename]) content_type, body = self._encode_multipart_formdata( fields, tags=self._get_tags(service_name), ) headers = common_headers.copy() headers["Content-Type"] = content_type # urllib uses `POST` if `data` is supplied (Python 2 version does not handle `method` kwarg) req = request.Request(self.endpoint, data=body, headers=headers) try: request.urlopen(req, timeout=self.timeout) except (error.HTTPError, error.URLError, http_client.HTTPException, socket.timeout) as e: raise UploadFailed(e)
def export(self, events, start_time_ns, end_time_ns): """Export events to an HTTP endpoint. :param events: The event dictionary from a `ddtrace.profiling.recorder.Recorder`. :param start_time_ns: The start time of recording. :param end_time_ns: The end time of recording. """ common_headers = { "DD-API-KEY": self.api_key.encode(), } profile = super(PprofHTTPExporter, self).export(events, start_time_ns, end_time_ns) s = six.BytesIO() with gzip.GzipFile(fileobj=s, mode="wb") as gz: gz.write(profile.SerializeToString()) fields = { "runtime-id": runtime.get_runtime_id().encode("ascii"), "recording-start": (datetime.datetime.utcfromtimestamp( start_time_ns / 1e9).replace(microsecond=0).isoformat() + "Z").encode(), "recording-end": (datetime.datetime.utcfromtimestamp( end_time_ns / 1e9).replace(microsecond=0).isoformat() + "Z").encode(), "runtime": PYTHON_IMPLEMENTATION, "format": b"pprof", "type": b"cpu+alloc+exceptions", "chunk-data": s.getvalue(), } service = self.service or os.path.basename( profile.string_table[profile.mapping[0].filename]) content_type, body = self._encode_multipart_formdata( fields, tags=self._get_tags(service), ) headers = common_headers.copy() headers["Content-Type"] = content_type # urllib uses `POST` if `data` is supplied (Python 2 version does not handle `method` kwarg) req = request.Request(self.endpoint, data=body, headers=headers) retry = tenacity.Retrying( # Retry after 1s, 2s, 4s, 8s with some randomness wait=tenacity.wait_random_exponential(multiplier=0.5), stop=tenacity.stop_after_delay(self.max_retry_delay), retry=tenacity.retry_if_exception_type( (error.HTTPError, error.URLError, http_client.HTTPException, OSError, IOError)), ) try: retry(request.urlopen, req, timeout=self.timeout) except tenacity.RetryError as e: raise UploadFailed(e.last_attempt.exception())