-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscenarios.py
More file actions
164 lines (131 loc) · 5.35 KB
/
scenarios.py
File metadata and controls
164 lines (131 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
import dataclasses
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 import GoogleCloudResourceDetector
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.sdk.trace.sampling import ALWAYS_ON
from opentelemetry.trace import SpanKind, Tracer, format_trace_id
from opentelemetry.sdk.resources import get_aggregated_resources
from pydantic import BaseModel
from .constants import INSTRUMENTING_MODULE_NAME, PROJECT_ID, TEST_ID, TRACE_ID
class Request(BaseModel):
test_id: str
headers: Mapping[str, str]
data: bytes
@dataclasses.dataclass
class Response:
status_code: code_pb2.Code
headers: dict[str, str] = dataclasses.field(default_factory=dict)
data: bytes = bytes()
@contextlib.contextmanager
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_config)
tracer_provider.add_span_processor(
BatchSpanProcessor(
CloudTraceSpanExporter(project_id=PROJECT_ID, **exporter_config)
)
)
tracer = tracer_provider.get_tracer(INSTRUMENTING_MODULE_NAME)
try:
yield tracer
finally:
tracer_provider.shutdown()
def health(request: Request) -> Response:
return Response(status_code=code_pb2.OK)
def basic_trace(request: Request) -> Response:
"""Create a basic trace"""
with _tracer_setup() as tracer:
with tracer.start_span(
"basicTrace", 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 complex_trace(request: Request) -> Response:
"""Create a complex trace"""
with _tracer_setup() as tracer:
with tracer.start_as_current_span(
"complexTrace/root", attributes={TEST_ID: request.test_id}
) as root_span:
trace_id = format_trace_id(root_span.get_span_context().trace_id)
with tracer.start_as_current_span(
"complexTrace/child1",
attributes={TEST_ID: request.test_id},
kind=SpanKind.SERVER,
):
with tracer.start_as_current_span(
"complexTrace/child2",
attributes={TEST_ID: request.test_id},
kind=SpanKind.CLIENT,
):
pass
with tracer.start_as_current_span(
"complexTrace/child3",
attributes={TEST_ID: request.test_id},
):
pass
return Response(status_code=code_pb2.OK, headers={TRACE_ID: trace_id})
def basic_propagator(request: Request) -> Response:
"""Create a trace, using the Cloud Trace format propagator"""
with _tracer_setup() as tracer:
propagator = CloudTraceFormatPropagator()
context = propagator.extract(request.headers)
with tracer.start_span(
"basicPropagator",
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 detect_resource(request: Request) -> Response:
"""Create a trace with GCP resource detector"""
with _tracer_setup(
tracer_provider_config={
"resource": get_aggregated_resources(
[GoogleCloudResourceDetector(raise_on_error=True)]
)
},
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))
SCENARIO_TO_HANDLER: dict[str, Callable[[Request], Response]] = {
"/health": health,
"/basicTrace": basic_trace,
"/complexTrace": complex_trace,
"/basicPropagator": basic_propagator,
"/detectResource": detect_resource,
}