Пример #1
0
    def export(
            self,
            events,  # type: recorder.EventsType
            start_time_ns,  # type: int
            end_time_ns,  # type: int
    ):
        # type: (...) -> pprof.pprof_ProfileType
        """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

        client = agent.get_connection(self.endpoint, self.timeout)
        self._upload(client, self.endpoint_path, body, headers)

        return profile
Пример #2
0
def test_get_connection():
    with pytest.raises(ValueError):
        agent.get_connection("bad://*****:*****@$@!//localhost:1234", timeout=1)

    with pytest.raises(ValueError):
        agent.get_connection("", timeout=1)