Skip to content

Commit

Permalink
Implement resource detection e2e scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
aabmass committed Feb 22, 2023
1 parent 04d0db6 commit 4a43330
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cloudbuild-e2e-cloud-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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}
50 changes: 46 additions & 4 deletions e2e-test-server/e2e_test_server/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -124,6 +135,36 @@ 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:
propagator = CloudTraceFormatPropagator()
context = propagator.extract(request.headers)
with tracer.start_span(
"resourceDetectionTrace",
attributes={TEST_ID: request.test_id},
context=context,
) 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))

Expand All @@ -133,4 +174,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
Expand Up @@ -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


Expand Down

0 comments on commit 4a43330

Please sign in to comment.