Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to export JSON traces over HTTP #3400

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import gzip
import logging
from typing_extensions import Literal
import zlib
from io import BytesIO
from os import environ
Expand All @@ -22,6 +23,8 @@

import backoff
import requests
from google.protobuf.json_format import MessageToJson


from opentelemetry.exporter.otlp.proto.common.trace_encoder import (
encode_spans,
Expand All @@ -40,11 +43,23 @@
)
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult
from opentelemetry.exporter.otlp.proto.http import (
_OTLP_HTTP_HEADERS,
Compression,
)
from ..version import __version__
from opentelemetry.util.re import parse_env_headers

_USER_AGENT = "OTel-OTLP-Exporter-Python/" + __version__
_PROTOBUF_CONTENT_TYPE = "application/x-protobuf"
_JSON_CONTENT_TYPE = "application/json"
_PROTOBUF_HEADERS = {
"Content-Type": _PROTOBUF_CONTENT_TYPE,
"User-Agent": _USER_AGENT,
}
_JSON_HEADERS = {
"Content-Type": _JSON_CONTENT_TYPE,
"User-Agent": _USER_AGENT,
}


_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,6 +94,7 @@ def __init__(
timeout: Optional[int] = None,
compression: Optional[Compression] = None,
session: Optional[requests.Session] = None,
format: Literal["json", "protobuf"] = "protobuf",
):
self._endpoint = endpoint or environ.get(
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
Expand All @@ -104,12 +120,16 @@ def __init__(
self._compression = compression or _compression_from_env()
self._session = session or requests.Session()
self._session.headers.update(self._headers)
self._session.headers.update(_OTLP_HTTP_HEADERS)
if format == "json":
self._session.headers.update(_JSON_HEADERS)
else:
self._session.headers.update(_PROTOBUF_HEADERS)
if self._compression is not Compression.NoCompression:
self._session.headers.update(
{"Content-Encoding": self._compression.value}
)
self._shutdown = False
self._format = format

def _export(self, serialized_data: str):
data = serialized_data
Expand Down Expand Up @@ -145,6 +165,11 @@ def export(self, spans) -> SpanExportResult:

serialized_data = encode_spans(spans).SerializeToString()

if self._format == "json":
serialized_data = MessageToJson(
serialized_data, use_integers_for_enums=True
)

for delay in _expo(max_value=self._MAX_RETRY_TIMEOUT):

if delay == self._MAX_RETRY_TIMEOUT:
Expand Down