diff --git a/.changelog/5412.added b/.changelog/5412.added new file mode 100644 index 00000000000..62e35791f17 --- /dev/null +++ b/.changelog/5412.added @@ -0,0 +1 @@ +`opentelemetry-exporter-otlp-json-file`: add OTLP JSON file Docker tests diff --git a/tests/opentelemetry-docker-tests/tests/.gitignore b/tests/opentelemetry-docker-tests/tests/.gitignore new file mode 100644 index 00000000000..00f8578309a --- /dev/null +++ b/tests/opentelemetry-docker-tests/tests/.gitignore @@ -0,0 +1,4 @@ +# Directory the OTLP JSON File exporter writes to during docker tests, created at +# runtime and tailed by the collector's otlp_json_file receiver (see +# docker-compose.yml). +otlpexporter/otlp-file-data/ diff --git a/tests/opentelemetry-docker-tests/tests/collector-config.yaml b/tests/opentelemetry-docker-tests/tests/collector-config.yaml index dbd52364e17..1be73178a92 100644 --- a/tests/opentelemetry-docker-tests/tests/collector-config.yaml +++ b/tests/opentelemetry-docker-tests/tests/collector-config.yaml @@ -5,6 +5,18 @@ receivers: endpoint: 0.0.0.0:4317 http: endpoint: 0.0.0.0:4318 + otlp_json_file/traces: + include: [/otlp-file-data/traces/*.jsonl] + start_at: beginning + include_file_name: false + otlp_json_file/metrics: + include: [/otlp-file-data/metrics/*.jsonl] + start_at: beginning + include_file_name: false + otlp_json_file/logs: + include: [/otlp-file-data/logs/*.jsonl] + start_at: beginning + include_file_name: false exporters: otlphttp: @@ -13,11 +25,11 @@ exporters: service: pipelines: traces: - receivers: [otlp] + receivers: [otlp, otlp_json_file/traces] exporters: [otlphttp] metrics: - receivers: [otlp] + receivers: [otlp, otlp_json_file/metrics] exporters: [otlphttp] logs: - receivers: [otlp] + receivers: [otlp, otlp_json_file/logs] exporters: [otlphttp] diff --git a/tests/opentelemetry-docker-tests/tests/docker-compose.yml b/tests/opentelemetry-docker-tests/tests/docker-compose.yml index 52cc7074d4a..b686d2848a8 100644 --- a/tests/opentelemetry-docker-tests/tests/docker-compose.yml +++ b/tests/opentelemetry-docker-tests/tests/docker-compose.yml @@ -6,10 +6,11 @@ services: - "8888:8888" - "55678:55678" otcollector: - image: otel/opentelemetry-collector:0.149.0 + image: otel/opentelemetry-collector-contrib:0.156.0 command: ["--config=/etc/otelcol/collector-config.yaml"] volumes: - ./collector-config.yaml:/etc/otelcol/collector-config.yaml + - ./otlpexporter/otlp-file-data:/otlp-file-data ports: - "4317:4317" - "4318:4318" diff --git a/tests/opentelemetry-docker-tests/tests/otlpexporter/__init__.py b/tests/opentelemetry-docker-tests/tests/otlpexporter/__init__.py index 0976ac5b690..66c7864e970 100644 --- a/tests/opentelemetry-docker-tests/tests/otlpexporter/__init__.py +++ b/tests/opentelemetry-docker-tests/tests/otlpexporter/__init__.py @@ -3,8 +3,11 @@ from __future__ import annotations +from collections.abc import Callable from dataclasses import dataclass, field -from typing import Any, Generic, TypeVar +from pathlib import Path +from typing import Any, Generic, Literal, TypeVar +from uuid import uuid4 ExporterT = TypeVar("ExporterT") @@ -13,15 +16,27 @@ "x-another-header": "another-value", } +# Directory bind mounted into the collector container (see docker-compose.yml). +OTLP_FILE_DATA_DIR = Path(__file__).parent / "otlp-file-data" + @dataclass class ExporterConfig(Generic[ExporterT]): id: str exporter_class: type[ExporterT] kwargs: dict[str, Any] = field(default_factory=dict) + lazy_kwargs: dict[str, Callable[[], Any]] = field(default_factory=dict) def build(self) -> ExporterT: - return self.exporter_class(**self.kwargs) + extra = {key: factory() for key, factory in self.lazy_kwargs.items()} + return self.exporter_class(**self.kwargs, **extra) + + +def make_otlp_file(signal: Literal["traces", "metrics", "logs"]) -> str: + """Return a unique .jsonl path under the collector mounted directory.""" + directory = OTLP_FILE_DATA_DIR / signal + directory.mkdir(parents=True, exist_ok=True) + return str(directory / f"{uuid4().hex}.jsonl") def _attrs_to_dict(attributes) -> dict: diff --git a/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_logs_functional.py b/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_logs_functional.py index 02fc8f555b5..4d7b42e27d5 100644 --- a/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_logs_functional.py +++ b/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_logs_functional.py @@ -11,6 +11,9 @@ from inline_snapshot import snapshot from opentelemetry._logs import Logger, SeverityNumber +from opentelemetry.exporter.otlp.json.file._log_exporter import ( + FileLogExporter, +) from opentelemetry.exporter.otlp.proto.grpc._log_exporter import ( OTLPLogExporter as GRPCLogExporter, ) @@ -28,7 +31,7 @@ from opentelemetry.sdk.resources import Resource from opentelemetry.test._otlp_test_server import OtlpProtoTestServer -from . import CUSTOM_HEADERS, ExporterConfig, _attrs_to_dict +from . import CUSTOM_HEADERS, ExporterConfig, _attrs_to_dict, make_otlp_file LOG_EXPORTER_CONFIGS: list[ExporterConfig[LogRecordExporter]] = [ ExporterConfig( @@ -75,6 +78,11 @@ exporter_class=GRPCLogExporter, kwargs={"insecure": True, "headers": CUSTOM_HEADERS}, ), + ExporterConfig( + id="file", + exporter_class=FileLogExporter, + lazy_kwargs={"path": lambda: make_otlp_file("logs")}, + ), ] diff --git a/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_metrics_functional.py b/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_metrics_functional.py index 2691dd4f7af..2b92b21c70f 100644 --- a/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_metrics_functional.py +++ b/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_metrics_functional.py @@ -10,6 +10,9 @@ from grpc import Compression as GRPCCompression from inline_snapshot import snapshot +from opentelemetry.exporter.otlp.json.file.metric_exporter import ( + FileMetricExporter, +) from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import ( OTLPMetricExporter as GRPCMetricExporter, ) @@ -32,7 +35,7 @@ from opentelemetry.sdk.resources import Resource from opentelemetry.test._otlp_test_server import OtlpProtoTestServer -from . import CUSTOM_HEADERS, ExporterConfig, _attrs_to_dict +from . import CUSTOM_HEADERS, ExporterConfig, _attrs_to_dict, make_otlp_file METRIC_EXPORTER_CONFIGS: list[ExporterConfig[MetricExporter]] = [ ExporterConfig( @@ -79,6 +82,11 @@ exporter_class=GRPCMetricExporter, kwargs={"insecure": True, "headers": CUSTOM_HEADERS}, ), + ExporterConfig( + id="file", + exporter_class=FileMetricExporter, + lazy_kwargs={"path": lambda: make_otlp_file("metrics")}, + ), ] diff --git a/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_traces_functional.py b/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_traces_functional.py index 9eb8a2a5ec5..e917ad33e98 100644 --- a/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_traces_functional.py +++ b/tests/opentelemetry-docker-tests/tests/otlpexporter/test_otlp_traces_functional.py @@ -10,6 +10,9 @@ from grpc import Compression as GRPCCompression from inline_snapshot import snapshot +from opentelemetry.exporter.otlp.json.file.trace_exporter import ( + FileSpanExporter, +) from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( OTLPSpanExporter as GRPCSpanExporter, ) @@ -24,7 +27,7 @@ from opentelemetry.test._otlp_test_server import OtlpProtoTestServer from opentelemetry.trace import Link, SpanContext, StatusCode, TraceFlags -from . import CUSTOM_HEADERS, ExporterConfig, _attrs_to_dict +from . import CUSTOM_HEADERS, ExporterConfig, _attrs_to_dict, make_otlp_file TRACE_EXPORTER_CONFIGS: list[ExporterConfig[SpanExporter]] = [ ExporterConfig( @@ -71,6 +74,11 @@ exporter_class=GRPCSpanExporter, kwargs={"insecure": True, "headers": CUSTOM_HEADERS}, ), + ExporterConfig( + id="file", + exporter_class=FileSpanExporter, + lazy_kwargs={"path": lambda: make_otlp_file("traces")}, + ), ] diff --git a/tox.ini b/tox.ini index bbeb8b1ae1d..e60a1406f39 100644 --- a/tox.ini +++ b/tox.ini @@ -376,16 +376,25 @@ deps = otlpexporter: -e {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-grpc otlpexporter: -e {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-http otlpexporter: -e {toxinidir}/exporter/opentelemetry-exporter-otlp + otlpexporter: -e {toxinidir}/opentelemetry-proto-json + otlpexporter: -e {toxinidir}/exporter/opentelemetry-exporter-otlp-json-common + otlpexporter: -e {toxinidir}/exporter/opentelemetry-exporter-otlp-json-file opencensus: -e {toxinidir}/exporter/opentelemetry-exporter-opencensus allowlist_externals = docker + mkdir + rm changedir = tests/opentelemetry-docker-tests/tests commands_pre = + ; Remove stale .jsonl files so the collector doesn't replay old runs, then + ; recreate the dirs (user-owned) before docker creates the bind-mount source. + rm -rf otlpexporter/otlp-file-data + mkdir -p otlpexporter/otlp-file-data/traces otlpexporter/otlp-file-data/metrics otlpexporter/otlp-file-data/logs docker compose version docker compose up -d commands =