コード例 #1
0
    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)
コード例 #2
0
ファイル: http.py プロジェクト: KyleJamesWalker/dd-trace-py
    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 self.api_key:
            headers = {
                "DD-API-KEY": self.api_key.encode(),
            }
        else:
            headers = {}

        if self._container_info and self._container_info.container_id:
            headers["Datadog-Container-Id"] = self._container_info.container_id

        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["Content-Type"] = content_type

        parsed = urlparse.urlparse(self.endpoint)
        if parsed.scheme == "https":
            client = http_client.HTTPSConnection(parsed.hostname, parsed.port, timeout=self.timeout)
        elif parsed.scheme == "http":
            client = http_client.HTTPConnection(parsed.hostname, parsed.port, timeout=self.timeout)
        elif parsed.scheme == "unix":
            client = uds.UDSHTTPConnection(parsed.path, False, parsed.hostname, parsed.port, timeout=self.timeout)
        else:
            raise ValueError("Unknown connection scheme %s" % parsed.scheme)

        self._upload(client, self.endpoint_path, body, headers)
コード例 #3
0
    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())
コード例 #4
0
    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")
        parsed = urlparse.urlparse(self.endpoint)
        if parsed.scheme == "https":
            client_class = http_client.HTTPSConnection
        else:
            client_class = http_client.HTTPConnection
        if ":" in parsed.netloc:
            host, port = parsed.netloc.split(":", 1)
        else:
            host, port = parsed.netloc, None
        client = client_class(host, port, timeout=self.timeout)

        common_headers = {
            "DD-API-KEY": self.api_key.encode(),
        }

        exceptions = []
        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(),
        }
        if "DD_SERVICE_NAME" in os.environ:
            service_name = os.environ.get("DD_SERVICE_NAME")
        elif "DATADOG_SERVICE_NAME" in os.environ:
            service_name = os.environ.get("DATADOG_SERVICE_NAME")
        else:
            service_name = 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
        try:
            client.request("POST", parsed.path, body=body, headers=headers)
        except (OSError, IOError, http_client.CannotSendRequest) as e:
            exceptions.append(e)
        else:
            try:
                response = client.getresponse()
                content = response.read()  # have to read to not fail!
            except (OSError, IOError, http_client.BadStatusLine) as e:
                exceptions.append(e)
            else:
                if not 200 <= response.status < 400:
                    exceptions.append(RequestFailed(response, content))

        if exceptions:
            raise UploadFailed(exceptions)