Skip to content

Commit

Permalink
use google.rpc.Code and change constants
Browse files Browse the repository at this point in the history
  • Loading branch information
aabmass committed May 11, 2021
1 parent 2773a11 commit 2ece059
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ steps:
- push
- gcr.io/$PROJECT_ID/opentelemetry-operations-python-e2e-test-server:$SHORT_SHA

- name: gcr.io/opentelemetry-ops-e2e/opentelemetry-operations-e2e-testing:0.5.10
- name: gcr.io/opentelemetry-ops-e2e/opentelemetry-operations-e2e-testing:$_TEST_SERVER_TAG
id: run-tests
dir: /
env:
Expand All @@ -37,5 +37,7 @@ steps:
- --network=cloudbuild

logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
substitutions:
_TEST_SERVER_TAG: 0.5.11
images:
- gcr.io/$PROJECT_ID/opentelemetry-operations-python-e2e-test-server:$SHORT_SHA
4 changes: 2 additions & 2 deletions e2e-test-server/e2e_test_server/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

INSTRUMENTING_MODULE_NAME = "opentelemetry-ops-e2e-test-server"
SCENARIO = "scenario"
STATUS = "status"
TEST_ID = "test-id"
STATUS_CODE = "status_code"
TEST_ID = "test_id"
11 changes: 6 additions & 5 deletions e2e-test-server/e2e_test_server/scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from dataclasses import dataclass
from typing import Any, Iterator, Mapping

from google.rpc import code_pb2

from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
Expand All @@ -28,12 +30,11 @@

@dataclass
class Response:
# HTTP style status code, even for pubsub
status: int
status_code: code_pb2.Code


@contextlib.contextmanager
def _tracer_setup(carrier: Mapping[str, str]) -> Iterator[tuple[str, Tracer]]:
def _tracer_setup(carrier: Mapping[str, str]) -> Iterator[Tracer]:
"""\
Context manager with common setup for tracing endpoints
Expand All @@ -56,7 +57,7 @@ def _tracer_setup(carrier: Mapping[str, str]) -> Iterator[tuple[str, Tracer]]:


def health(test_id: str, headers: Mapping[str, str], body: bytes) -> Response:
return Response(status=200)
return Response(status_code=code_pb2.OK)


def basicTrace(
Expand All @@ -68,4 +69,4 @@ def basicTrace(
with tracer.start_span("basicTrace", attributes={TEST_ID: test_id}):
pass

return Response(status=200)
return Response(status_code=code_pb2.OK)
19 changes: 13 additions & 6 deletions e2e-test-server/e2e_test_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from google.cloud.pubsub_v1.subscriber.message import Message

from . import scenarios
from .constants import SCENARIO, STATUS, TEST_ID
from .constants import SCENARIO, STATUS_CODE, TEST_ID
from google.rpc import code_pb2

PROJECT_ID = os.environ["PROJECT_ID"]
REQUEST_SUBSCRIPTION_NAME = os.environ["REQUEST_SUBSCRIPTION_NAME"]
Expand All @@ -34,16 +35,19 @@ def pubsub_pull() -> None:

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
PROJECT_ID, REQUEST_SUBSCRIPTION_NAME,
PROJECT_ID,
REQUEST_SUBSCRIPTION_NAME,
)

def respond(test_id: str, res: scenarios.Response) -> None:
"""Respond to the test runner that we finished executing the scenario"""
data = bytes()
attributes = {TEST_ID: test_id, STATUS: str(res.status)}
attributes = {TEST_ID: test_id, STATUS_CODE: str(res.status_code)}
print(f"publishing {data=} and {attributes=}")
publisher.publish(
response_topic, bytes(), **attributes,
response_topic,
bytes(),
**attributes,
)

def pubsub_callback(message: Message) -> None:
Expand All @@ -58,7 +62,10 @@ def pubsub_callback(message: Message) -> None:
publisher.publish(
response_topic,
f'Expected attribute "{SCENARIO}" is missing',
**{TEST_ID: test_id, "status": 500},
**{
TEST_ID: test_id,
STATUS_CODE: str(code_pb2.INVALID_ARGUMENT),
},
)
scenario = message.attributes[SCENARIO]

Expand All @@ -68,7 +75,7 @@ def pubsub_callback(message: Message) -> None:
scenarioFunc = scenarios.basicTrace
else:
scenarioFunc = lambda *args, **kwargs: scenarios.Response(
status=404
status_code=str(code_pb2.UNIMPLEMENTED)
)

res = scenarioFunc(test_id, message.attributes, message.data)
Expand Down
1 change: 1 addition & 0 deletions e2e-test-server/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ opentelemetry-sdk
opentelemetry-api
Flask
google-cloud-pubsub
googleapis-common-protos

0 comments on commit 2ece059

Please sign in to comment.