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

Implement resource detection e2e scenario #234

Merged
merged 1 commit into from
Feb 23, 2023
Merged
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
2 changes: 1 addition & 1 deletion cloudbuild-e2e-cloud-run.yaml
Original file line number Diff line number Diff line change
@@ -32,5 +32,5 @@ steps:

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.9.0
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.16.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-python-e2e-test-server:${SHORT_SHA}
2 changes: 1 addition & 1 deletion cloudbuild-e2e-gce.yaml
Original file line number Diff line number Diff line change
@@ -34,5 +34,5 @@ steps:

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.9.0
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.16.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-python-e2e-test-server:${SHORT_SHA}
2 changes: 1 addition & 1 deletion cloudbuild-e2e-gke.yaml
Original file line number Diff line number Diff line change
@@ -32,5 +32,5 @@ steps:

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.9.0
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.16.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-python-e2e-test-server:${SHORT_SHA}
2 changes: 1 addition & 1 deletion cloudbuild-e2e-local.yaml
Original file line number Diff line number Diff line change
@@ -34,5 +34,5 @@ steps:

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.9.0
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.16.0
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-python-e2e-test-server:${SHORT_SHA}
47 changes: 43 additions & 4 deletions e2e-test-server/e2e_test_server/scenarios.py
Original file line number Diff line number Diff line change
@@ -14,13 +14,17 @@

import contextlib
import dataclasses
from typing import Callable, Iterator, Mapping
import os
from typing import Any, Callable, Iterator, Mapping

from google.rpc import code_pb2
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.propagators.cloud_trace_propagator import (
CloudTraceFormatPropagator,
)
from opentelemetry.resourcedetector.gcp_resource_detector._detector import (
GoogleCloudResourceDetector,
)
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.trace.sampling import ALWAYS_ON
@@ -44,17 +48,24 @@ class Response:


@contextlib.contextmanager
def _tracer_setup() -> Iterator[Tracer]:
def _tracer_setup(
tracer_provider_config: Mapping[str, Any] = {},
exporter_config: Mapping[str, Any] = {},
) -> Iterator[Tracer]:
"""\
Context manager with common setup for tracing endpoints

Yields a tracer (from a fresh SDK with new exporter) then finally flushes
spans created during the test after.
"""

tracer_provider = TracerProvider(sampler=ALWAYS_ON)
tracer_provider = TracerProvider(
sampler=ALWAYS_ON, **tracer_provider_config
)
tracer_provider.add_span_processor(
BatchSpanProcessor(CloudTraceSpanExporter(project_id=PROJECT_ID))
BatchSpanProcessor(
CloudTraceSpanExporter(project_id=PROJECT_ID, **exporter_config)
)
)
tracer = tracer_provider.get_tracer(INSTRUMENTING_MODULE_NAME)

@@ -124,6 +135,33 @@ def basic_propagator(request: Request) -> Response:
return Response(status_code=code_pb2.OK, headers={TRACE_ID: trace_id})


def detect_resource(request: Request) -> Response:
"""Create a trace with GCP resource detector"""

# Temporarily skip if on Cloud Run until it is implemented
if "K_CONFIGURATION" in os.environ:
return Response(
status_code=code_pb2.UNIMPLEMENTED,
data=b"Resource detection not yet implemented on Cloud Run",
)

with _tracer_setup(
tracer_provider_config={
"resource": GoogleCloudResourceDetector(
raise_on_error=True
).detect()
},
exporter_config={"resource_regex": r".*"},
) as tracer:
with tracer.start_span(
"resourceDetectionTrace",
attributes={TEST_ID: request.test_id},
) as span:
trace_id = format_trace_id(span.get_span_context().trace_id)

return Response(status_code=code_pb2.OK, headers={TRACE_ID: trace_id})


def not_implemented_handler(_: Request) -> Response:
return Response(status_code=str(code_pb2.UNIMPLEMENTED))

@@ -133,4 +171,5 @@ def not_implemented_handler(_: Request) -> Response:
"/basicTrace": basic_trace,
"/complexTrace": complex_trace,
"/basicPropagator": basic_propagator,
"/detectResource": detect_resource,
}
Original file line number Diff line number Diff line change
@@ -62,15 +62,16 @@ def get_metadata() -> Metadata:
Cached for the lifetime of the process.
"""
try:
all_metadata = requests.get(
res = requests.get(
f"{_GCP_METADATA_URL}",
params=_RECURSIVE_PARAMS,
headers=_GCP_METADATA_URL_HEADER,
timeout=_TIMEOUT_SEC,
).json()
)
res.raise_for_status()
all_metadata = res.json()
except requests.RequestException as err:
raise MetadataAccessException() from err

return all_metadata