google-cloud-logging 3.13.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google/cloud/logging/__init__.py +53 -0
- google/cloud/logging/gapic_version.py +16 -0
- google/cloud/logging/handlers/__init__.py +31 -0
- google/cloud/logging/handlers/middleware/__init__.py +17 -0
- google/cloud/logging/handlers/transports/__init__.py +28 -0
- google/cloud/logging/py.typed +2 -0
- google/cloud/logging_v2/__init__.py +61 -0
- google/cloud/logging_v2/_gapic.py +647 -0
- google/cloud/logging_v2/_helpers.py +152 -0
- google/cloud/logging_v2/_http.py +549 -0
- google/cloud/logging_v2/_instrumentation.py +92 -0
- google/cloud/logging_v2/client.py +429 -0
- google/cloud/logging_v2/entries.py +358 -0
- google/cloud/logging_v2/gapic_metadata.json +481 -0
- google/cloud/logging_v2/gapic_version.py +16 -0
- google/cloud/logging_v2/handlers/__init__.py +31 -0
- google/cloud/logging_v2/handlers/_helpers.py +283 -0
- google/cloud/logging_v2/handlers/_monitored_resources.py +303 -0
- google/cloud/logging_v2/handlers/app_engine.py +139 -0
- google/cloud/logging_v2/handlers/container_engine.py +62 -0
- google/cloud/logging_v2/handlers/handlers.py +340 -0
- google/cloud/logging_v2/handlers/middleware/__init__.py +17 -0
- google/cloud/logging_v2/handlers/middleware/request.py +52 -0
- google/cloud/logging_v2/handlers/structured_log.py +150 -0
- google/cloud/logging_v2/handlers/transports/__init__.py +29 -0
- google/cloud/logging_v2/handlers/transports/background_thread.py +343 -0
- google/cloud/logging_v2/handlers/transports/base.py +61 -0
- google/cloud/logging_v2/handlers/transports/sync.py +68 -0
- google/cloud/logging_v2/logger.py +541 -0
- google/cloud/logging_v2/metric.py +180 -0
- google/cloud/logging_v2/py.typed +2 -0
- google/cloud/logging_v2/resource.py +51 -0
- google/cloud/logging_v2/services/__init__.py +15 -0
- google/cloud/logging_v2/services/config_service_v2/__init__.py +22 -0
- google/cloud/logging_v2/services/config_service_v2/async_client.py +4232 -0
- google/cloud/logging_v2/services/config_service_v2/client.py +4686 -0
- google/cloud/logging_v2/services/config_service_v2/pagers.py +820 -0
- google/cloud/logging_v2/services/config_service_v2/transports/__init__.py +33 -0
- google/cloud/logging_v2/services/config_service_v2/transports/base.py +748 -0
- google/cloud/logging_v2/services/config_service_v2/transports/grpc.py +1345 -0
- google/cloud/logging_v2/services/config_service_v2/transports/grpc_asyncio.py +1660 -0
- google/cloud/logging_v2/services/logging_service_v2/__init__.py +22 -0
- google/cloud/logging_v2/services/logging_service_v2/async_client.py +1272 -0
- google/cloud/logging_v2/services/logging_service_v2/client.py +1686 -0
- google/cloud/logging_v2/services/logging_service_v2/pagers.py +516 -0
- google/cloud/logging_v2/services/logging_service_v2/transports/__init__.py +33 -0
- google/cloud/logging_v2/services/logging_service_v2/transports/base.py +361 -0
- google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py +566 -0
- google/cloud/logging_v2/services/logging_service_v2/transports/grpc_asyncio.py +703 -0
- google/cloud/logging_v2/services/metrics_service_v2/__init__.py +22 -0
- google/cloud/logging_v2/services/metrics_service_v2/async_client.py +1119 -0
- google/cloud/logging_v2/services/metrics_service_v2/client.py +1534 -0
- google/cloud/logging_v2/services/metrics_service_v2/pagers.py +196 -0
- google/cloud/logging_v2/services/metrics_service_v2/transports/__init__.py +33 -0
- google/cloud/logging_v2/services/metrics_service_v2/transports/base.py +320 -0
- google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py +520 -0
- google/cloud/logging_v2/services/metrics_service_v2/transports/grpc_asyncio.py +629 -0
- google/cloud/logging_v2/sink.py +233 -0
- google/cloud/logging_v2/types/__init__.py +178 -0
- google/cloud/logging_v2/types/log_entry.py +435 -0
- google/cloud/logging_v2/types/logging.py +602 -0
- google/cloud/logging_v2/types/logging_config.py +2418 -0
- google/cloud/logging_v2/types/logging_metrics.py +412 -0
- google_cloud_logging-3.13.0.dist-info/METADATA +143 -0
- google_cloud_logging-3.13.0.dist-info/RECORD +68 -0
- google_cloud_logging-3.13.0.dist-info/WHEEL +5 -0
- google_cloud_logging-3.13.0.dist-info/licenses/LICENSE +202 -0
- google_cloud_logging-3.13.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
# Copyright 2016 Google LLC All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Helper functions for logging handlers."""
|
|
16
|
+
|
|
17
|
+
import math
|
|
18
|
+
import json
|
|
19
|
+
import re
|
|
20
|
+
import warnings
|
|
21
|
+
|
|
22
|
+
try:
|
|
23
|
+
import flask
|
|
24
|
+
except ImportError: # pragma: NO COVER
|
|
25
|
+
flask = None
|
|
26
|
+
|
|
27
|
+
import opentelemetry.trace
|
|
28
|
+
|
|
29
|
+
from google.cloud.logging_v2.handlers.middleware.request import _get_django_request
|
|
30
|
+
|
|
31
|
+
_DJANGO_CONTENT_LENGTH = "CONTENT_LENGTH"
|
|
32
|
+
_DJANGO_XCLOUD_TRACE_HEADER = "HTTP_X_CLOUD_TRACE_CONTEXT"
|
|
33
|
+
_DJANGO_TRACEPARENT = "HTTP_TRACEPARENT"
|
|
34
|
+
_DJANGO_USERAGENT_HEADER = "HTTP_USER_AGENT"
|
|
35
|
+
_DJANGO_REMOTE_ADDR_HEADER = "REMOTE_ADDR"
|
|
36
|
+
_DJANGO_REFERER_HEADER = "HTTP_REFERER"
|
|
37
|
+
_FLASK_XCLOUD_TRACE_HEADER = "X_CLOUD_TRACE_CONTEXT"
|
|
38
|
+
_FLASK_TRACEPARENT = "TRACEPARENT"
|
|
39
|
+
_PROTOCOL_HEADER = "SERVER_PROTOCOL"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def format_stackdriver_json(record, message):
|
|
43
|
+
"""Helper to format a LogRecord in in Stackdriver fluentd format.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
str: JSON str to be written to the log file.
|
|
47
|
+
|
|
48
|
+
DEPRECATED: use StructuredLogHandler to write formatted logs to standard out instead.
|
|
49
|
+
"""
|
|
50
|
+
subsecond, second = math.modf(record.created)
|
|
51
|
+
|
|
52
|
+
payload = {
|
|
53
|
+
"message": message,
|
|
54
|
+
"timestamp": {"seconds": int(second), "nanos": int(subsecond * 1e9)},
|
|
55
|
+
"thread": record.thread,
|
|
56
|
+
"severity": record.levelname,
|
|
57
|
+
}
|
|
58
|
+
warnings.warn(
|
|
59
|
+
"format_stackdriver_json is deprecated. Use StructuredLogHandler instead.",
|
|
60
|
+
DeprecationWarning,
|
|
61
|
+
)
|
|
62
|
+
return json.dumps(payload, ensure_ascii=False)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def get_request_data_from_flask():
|
|
66
|
+
"""Get http_request and trace data from flask request headers.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
Tuple[Optional[dict], Optional[str], Optional[str], bool]:
|
|
70
|
+
Data related to the current http request, trace_id, span_id and trace_sampled
|
|
71
|
+
for the request. All fields will be None if a Flask request isn't found.
|
|
72
|
+
"""
|
|
73
|
+
if flask is None or not flask.request:
|
|
74
|
+
return None, None, None, False
|
|
75
|
+
|
|
76
|
+
# build http_request
|
|
77
|
+
http_request = {
|
|
78
|
+
"requestMethod": flask.request.method,
|
|
79
|
+
"requestUrl": flask.request.url,
|
|
80
|
+
"userAgent": flask.request.user_agent.string,
|
|
81
|
+
"protocol": flask.request.environ.get(_PROTOCOL_HEADER),
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
# find trace id and span id
|
|
85
|
+
# first check for w3c traceparent header
|
|
86
|
+
header = flask.request.headers.get(_FLASK_TRACEPARENT)
|
|
87
|
+
trace_id, span_id, trace_sampled = _parse_trace_parent(header)
|
|
88
|
+
if trace_id is None:
|
|
89
|
+
# traceparent not found. look for xcloud_trace_context header
|
|
90
|
+
header = flask.request.headers.get(_FLASK_XCLOUD_TRACE_HEADER)
|
|
91
|
+
trace_id, span_id, trace_sampled = _parse_xcloud_trace(header)
|
|
92
|
+
|
|
93
|
+
return http_request, trace_id, span_id, trace_sampled
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def get_request_data_from_django():
|
|
97
|
+
"""Get http_request and trace data from django request headers.
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Tuple[Optional[dict], Optional[str], Optional[str], bool]:
|
|
101
|
+
Data related to the current http request, trace_id, span_id, and trace_sampled
|
|
102
|
+
for the request. All fields will be None if a django request isn't found.
|
|
103
|
+
"""
|
|
104
|
+
request = _get_django_request()
|
|
105
|
+
|
|
106
|
+
if request is None:
|
|
107
|
+
return None, None, None, False
|
|
108
|
+
|
|
109
|
+
# Django can raise django.core.exceptions.DisallowedHost here for a
|
|
110
|
+
# malformed HTTP_HOST header. But we don't want to import Django modules.
|
|
111
|
+
try:
|
|
112
|
+
request_url = request.build_absolute_uri()
|
|
113
|
+
except Exception:
|
|
114
|
+
request_url = None
|
|
115
|
+
|
|
116
|
+
# build http_request
|
|
117
|
+
http_request = {
|
|
118
|
+
"requestMethod": request.method,
|
|
119
|
+
"requestUrl": request_url,
|
|
120
|
+
"userAgent": request.META.get(_DJANGO_USERAGENT_HEADER),
|
|
121
|
+
"protocol": request.META.get(_PROTOCOL_HEADER),
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# find trace id and span id
|
|
125
|
+
# first check for w3c traceparent header
|
|
126
|
+
header = request.META.get(_DJANGO_TRACEPARENT)
|
|
127
|
+
trace_id, span_id, trace_sampled = _parse_trace_parent(header)
|
|
128
|
+
if trace_id is None:
|
|
129
|
+
# traceparent not found. look for xcloud_trace_context header
|
|
130
|
+
header = request.META.get(_DJANGO_XCLOUD_TRACE_HEADER)
|
|
131
|
+
trace_id, span_id, trace_sampled = _parse_xcloud_trace(header)
|
|
132
|
+
|
|
133
|
+
return http_request, trace_id, span_id, trace_sampled
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _parse_trace_parent(header):
|
|
137
|
+
"""Given a w3 traceparent header, extract the trace and span ids.
|
|
138
|
+
For more information see https://www.w3.org/TR/trace-context/
|
|
139
|
+
|
|
140
|
+
Args:
|
|
141
|
+
header (str): the string extracted from the traceparent header
|
|
142
|
+
example: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
|
|
143
|
+
Returns:
|
|
144
|
+
Tuple[Optional[dict], Optional[str], bool]:
|
|
145
|
+
The trace_id, span_id and trace_sampled extracted from the header
|
|
146
|
+
Each field will be None if header can't be parsed in expected format.
|
|
147
|
+
"""
|
|
148
|
+
trace_id = span_id = None
|
|
149
|
+
trace_sampled = False
|
|
150
|
+
# see https://www.w3.org/TR/trace-context/ for W3C traceparent format
|
|
151
|
+
if header:
|
|
152
|
+
try:
|
|
153
|
+
VERSION_PART = r"(?!ff)[a-f\d]{2}"
|
|
154
|
+
TRACE_ID_PART = r"(?![0]{32})[a-f\d]{32}"
|
|
155
|
+
PARENT_ID_PART = r"(?![0]{16})[a-f\d]{16}"
|
|
156
|
+
FLAGS_PART = r"[a-f\d]{2}"
|
|
157
|
+
regex = f"^\\s?({VERSION_PART})-({TRACE_ID_PART})-({PARENT_ID_PART})-({FLAGS_PART})(-.*)?\\s?$"
|
|
158
|
+
match = re.match(regex, header)
|
|
159
|
+
trace_id = match.group(2)
|
|
160
|
+
span_id = match.group(3)
|
|
161
|
+
# trace-flag component is an 8-bit bit field. Read as an int
|
|
162
|
+
int_flag = int(match.group(4), 16)
|
|
163
|
+
# trace sampled is set if the right-most bit in flag component is set
|
|
164
|
+
trace_sampled = bool(int_flag & 1)
|
|
165
|
+
except (IndexError, AttributeError):
|
|
166
|
+
# could not parse header as expected. Return None
|
|
167
|
+
pass
|
|
168
|
+
return trace_id, span_id, trace_sampled
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _parse_xcloud_trace(header):
|
|
172
|
+
"""Given an X_CLOUD_TRACE header, extract the trace and span ids.
|
|
173
|
+
|
|
174
|
+
Args:
|
|
175
|
+
header (str): the string extracted from the X_CLOUD_TRACE header
|
|
176
|
+
Returns:
|
|
177
|
+
Tuple[Optional[str], Optional[str], bool]:
|
|
178
|
+
The trace_id, span_id and trace_sampled extracted from the header
|
|
179
|
+
Each field will be None if not found.
|
|
180
|
+
"""
|
|
181
|
+
trace_id = span_id = None
|
|
182
|
+
trace_sampled = False
|
|
183
|
+
|
|
184
|
+
# As per the format described at https://cloud.google.com/trace/docs/trace-context#legacy-http-header
|
|
185
|
+
# "X-Cloud-Trace-Context: TRACE_ID[/SPAN_ID][;o=OPTIONS]"
|
|
186
|
+
# for example:
|
|
187
|
+
# "X-Cloud-Trace-Context: 105445aa7843bc8bf206b12000100000/1;o=1"
|
|
188
|
+
#
|
|
189
|
+
# We expect:
|
|
190
|
+
# * trace_id (optional, 128-bit hex string): "105445aa7843bc8bf206b12000100000"
|
|
191
|
+
# * span_id (optional, 16-bit hex string): "0000000000000001" (needs to be converted into 16 bit hex string)
|
|
192
|
+
# * trace_sampled (optional, bool): true
|
|
193
|
+
if header:
|
|
194
|
+
try:
|
|
195
|
+
regex = r"([\w-]+)?(\/?([\w-]+))?(;?o=(\d))?"
|
|
196
|
+
match = re.match(regex, header)
|
|
197
|
+
trace_id = match.group(1)
|
|
198
|
+
span_id = match.group(3)
|
|
199
|
+
trace_sampled = match.group(5) == "1"
|
|
200
|
+
|
|
201
|
+
# Convert the span ID to 16-bit hexadecimal instead of decimal
|
|
202
|
+
try:
|
|
203
|
+
span_id_int = int(span_id)
|
|
204
|
+
if span_id_int > 0 and span_id_int < 2**64:
|
|
205
|
+
span_id = f"{span_id_int:016x}"
|
|
206
|
+
else:
|
|
207
|
+
span_id = None
|
|
208
|
+
except (ValueError, TypeError):
|
|
209
|
+
span_id = None
|
|
210
|
+
|
|
211
|
+
except IndexError:
|
|
212
|
+
pass
|
|
213
|
+
return trace_id, span_id, trace_sampled
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _retrieve_current_open_telemetry_span():
|
|
217
|
+
"""Helper to retrieve trace, span ID, and trace sampled information from the current
|
|
218
|
+
OpenTelemetry span.
|
|
219
|
+
|
|
220
|
+
Returns:
|
|
221
|
+
Tuple[Optional[str], Optional[str], bool]:
|
|
222
|
+
Data related to the current trace_id, span_id, and trace_sampled for the
|
|
223
|
+
current OpenTelemetry span. If a span is not found, return None/False for all
|
|
224
|
+
fields.
|
|
225
|
+
"""
|
|
226
|
+
span = opentelemetry.trace.get_current_span()
|
|
227
|
+
if span != opentelemetry.trace.span.INVALID_SPAN:
|
|
228
|
+
context = span.get_span_context()
|
|
229
|
+
trace_id = opentelemetry.trace.format_trace_id(context.trace_id)
|
|
230
|
+
span_id = opentelemetry.trace.format_span_id(context.span_id)
|
|
231
|
+
trace_sampled = context.trace_flags.sampled
|
|
232
|
+
|
|
233
|
+
return trace_id, span_id, trace_sampled
|
|
234
|
+
|
|
235
|
+
return None, None, False
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def get_request_data():
|
|
239
|
+
"""Helper to get http_request and trace data from supported web
|
|
240
|
+
frameworks (currently supported: Flask and Django), as well as OpenTelemetry. Attempts
|
|
241
|
+
to retrieve trace/spanID from OpenTelemetry first, before going to Traceparent then XCTC.
|
|
242
|
+
HTTP request data is taken from a supporting web framework (currently Flask or Django).
|
|
243
|
+
Because HTTP request data is decoupled from OpenTelemetry, it is possible to get as a
|
|
244
|
+
return value the HTTP request from the web framework of choice, and trace/span data from
|
|
245
|
+
OpenTelemetry, even if trace data is present in the HTTP request headers.
|
|
246
|
+
|
|
247
|
+
Returns:
|
|
248
|
+
Tuple[Optional[dict], Optional[str], Optional[str], bool]:
|
|
249
|
+
Data related to the current http request, trace_id, span_id, and trace_sampled
|
|
250
|
+
for the request. All fields will be None if a http request isn't found.
|
|
251
|
+
"""
|
|
252
|
+
|
|
253
|
+
(
|
|
254
|
+
otel_trace_id,
|
|
255
|
+
otel_span_id,
|
|
256
|
+
otel_trace_sampled,
|
|
257
|
+
) = _retrieve_current_open_telemetry_span()
|
|
258
|
+
|
|
259
|
+
# Get HTTP request data
|
|
260
|
+
checkers = (
|
|
261
|
+
get_request_data_from_django,
|
|
262
|
+
get_request_data_from_flask,
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
http_request, http_trace_id, http_span_id, http_trace_sampled = (
|
|
266
|
+
None,
|
|
267
|
+
None,
|
|
268
|
+
None,
|
|
269
|
+
False,
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
for checker in checkers:
|
|
273
|
+
http_request, http_trace_id, http_span_id, http_trace_sampled = checker()
|
|
274
|
+
if http_request is None:
|
|
275
|
+
http_trace_id, http_span_id, http_trace_sampled = None, None, False
|
|
276
|
+
else:
|
|
277
|
+
break
|
|
278
|
+
|
|
279
|
+
# otel_trace_id existing means the other return values are non-null
|
|
280
|
+
if otel_trace_id:
|
|
281
|
+
return http_request, otel_trace_id, otel_span_id, otel_trace_sampled
|
|
282
|
+
else:
|
|
283
|
+
return http_request, http_trace_id, http_span_id, http_trace_sampled
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# Copyright 2021 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import functools
|
|
16
|
+
import logging
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
from google.cloud.logging_v2.resource import Resource
|
|
20
|
+
from google.cloud.logging_v2._helpers import retrieve_metadata_server
|
|
21
|
+
|
|
22
|
+
_GAE_SERVICE_ENV = "GAE_SERVICE"
|
|
23
|
+
_GAE_VERSION_ENV = "GAE_VERSION"
|
|
24
|
+
_GAE_INSTANCE_ENV = "GAE_INSTANCE"
|
|
25
|
+
_GAE_ENV_VARS = [_GAE_SERVICE_ENV, _GAE_VERSION_ENV, _GAE_INSTANCE_ENV]
|
|
26
|
+
"""Environment variables set in App Engine environment."""
|
|
27
|
+
|
|
28
|
+
_CLOUD_RUN_SERVICE_ID = "K_SERVICE"
|
|
29
|
+
_CLOUD_RUN_REVISION_ID = "K_REVISION"
|
|
30
|
+
_CLOUD_RUN_CONFIGURATION_ID = "K_CONFIGURATION"
|
|
31
|
+
_CLOUD_RUN_SERVICE_ENV_VARS = [
|
|
32
|
+
_CLOUD_RUN_SERVICE_ID,
|
|
33
|
+
_CLOUD_RUN_REVISION_ID,
|
|
34
|
+
_CLOUD_RUN_CONFIGURATION_ID,
|
|
35
|
+
]
|
|
36
|
+
_CLOUD_RUN_JOB_ID = "CLOUD_RUN_JOB"
|
|
37
|
+
_CLOUD_RUN_EXECUTION_ID = "CLOUD_RUN_EXECUTION"
|
|
38
|
+
_CLOUD_RUN_TASK_INDEX = "CLOUD_RUN_TASK_INDEX"
|
|
39
|
+
_CLOUD_RUN_TASK_ATTEMPT = "CLOUD_RUN_TASK_ATTEMPT"
|
|
40
|
+
_CLOUD_RUN_JOB_ENV_VARS = [
|
|
41
|
+
_CLOUD_RUN_JOB_ID,
|
|
42
|
+
_CLOUD_RUN_EXECUTION_ID,
|
|
43
|
+
_CLOUD_RUN_TASK_INDEX,
|
|
44
|
+
_CLOUD_RUN_TASK_ATTEMPT,
|
|
45
|
+
]
|
|
46
|
+
"""Environment variables set in Cloud Run environment."""
|
|
47
|
+
|
|
48
|
+
_FUNCTION_TARGET = "FUNCTION_TARGET"
|
|
49
|
+
_FUNCTION_SIGNATURE = "FUNCTION_SIGNATURE_TYPE"
|
|
50
|
+
_FUNCTION_NAME = "FUNCTION_NAME"
|
|
51
|
+
_FUNCTION_REGION = "FUNCTION_REGION"
|
|
52
|
+
_FUNCTION_ENTRY = "ENTRY_POINT"
|
|
53
|
+
_FUNCTION_ENV_VARS = [_FUNCTION_TARGET, _FUNCTION_SIGNATURE, _CLOUD_RUN_SERVICE_ID]
|
|
54
|
+
_LEGACY_FUNCTION_ENV_VARS = [_FUNCTION_NAME, _FUNCTION_REGION, _FUNCTION_ENTRY]
|
|
55
|
+
"""Environment variables set in Cloud Functions environments."""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
_REGION_ID = "instance/region"
|
|
59
|
+
_ZONE_ID = "instance/zone"
|
|
60
|
+
_GCE_INSTANCE_ID = "instance/id"
|
|
61
|
+
"""Attribute in metadata server for compute region and instance."""
|
|
62
|
+
|
|
63
|
+
_GKE_CLUSTER_NAME = "instance/attributes/cluster-name"
|
|
64
|
+
"""Attribute in metadata server when in GKE environment."""
|
|
65
|
+
|
|
66
|
+
_GKE_CLUSTER_LOCATION = "instance/attributes/cluster-location"
|
|
67
|
+
"""Attribute in metadata server when in GKE environment."""
|
|
68
|
+
|
|
69
|
+
_PROJECT_NAME = "project/project-id"
|
|
70
|
+
"""Attribute in metadata server when in GKE environment."""
|
|
71
|
+
|
|
72
|
+
_GAE_RESOURCE_TYPE = "gae_app"
|
|
73
|
+
"""Resource type for App Engine environment."""
|
|
74
|
+
|
|
75
|
+
_CLOUD_RUN_JOB_RESOURCE_TYPE = "cloud_run_job"
|
|
76
|
+
"""Resource type for Cloud Run Jobs."""
|
|
77
|
+
|
|
78
|
+
_GAE_TRACE_ID_LABEL = "appengine.googleapis.com/trace_id"
|
|
79
|
+
"""Extra trace label to be added on App Engine environments"""
|
|
80
|
+
|
|
81
|
+
_CLOUD_RUN_JOBS_EXECUTION_NAME_LABEL = "run.googleapis.com/execution_name"
|
|
82
|
+
_CLOUD_RUN_JOBS_TASK_INDEX_LABEL = "run.googleapis.com/task_index"
|
|
83
|
+
_CLOUD_RUN_JOBS_TASK_ATTEMPT_LABEL = "run.googleapis.com/task_attempt"
|
|
84
|
+
"""Extra labels for Cloud Run environments to be recognized by Cloud Run Jobs web UI."""
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _create_functions_resource():
|
|
88
|
+
"""Create a standardized Cloud Functions resource.
|
|
89
|
+
Returns:
|
|
90
|
+
google.cloud.logging.Resource
|
|
91
|
+
"""
|
|
92
|
+
project = retrieve_metadata_server(_PROJECT_NAME)
|
|
93
|
+
region = retrieve_metadata_server(_REGION_ID)
|
|
94
|
+
if _FUNCTION_NAME in os.environ:
|
|
95
|
+
function_name = os.environ.get(_FUNCTION_NAME)
|
|
96
|
+
elif _CLOUD_RUN_SERVICE_ID in os.environ:
|
|
97
|
+
function_name = os.environ.get(_CLOUD_RUN_SERVICE_ID)
|
|
98
|
+
else:
|
|
99
|
+
function_name = ""
|
|
100
|
+
resource = Resource(
|
|
101
|
+
type="cloud_function",
|
|
102
|
+
labels={
|
|
103
|
+
"project_id": project if project else "",
|
|
104
|
+
"function_name": function_name if function_name else "",
|
|
105
|
+
"region": region.split("/")[-1] if region else "",
|
|
106
|
+
},
|
|
107
|
+
)
|
|
108
|
+
return resource
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _create_kubernetes_resource():
|
|
112
|
+
"""Create a standardized Kubernetes resource.
|
|
113
|
+
Returns:
|
|
114
|
+
google.cloud.logging.Resource
|
|
115
|
+
"""
|
|
116
|
+
location = retrieve_metadata_server(_GKE_CLUSTER_LOCATION)
|
|
117
|
+
cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME)
|
|
118
|
+
project = retrieve_metadata_server(_PROJECT_NAME)
|
|
119
|
+
|
|
120
|
+
resource = Resource(
|
|
121
|
+
type="k8s_container",
|
|
122
|
+
labels={
|
|
123
|
+
"project_id": project if project else "",
|
|
124
|
+
"location": location if location else "",
|
|
125
|
+
"cluster_name": cluster_name if cluster_name else "",
|
|
126
|
+
},
|
|
127
|
+
)
|
|
128
|
+
return resource
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _create_compute_resource():
|
|
132
|
+
"""Create a standardized Compute Engine resource.
|
|
133
|
+
Returns:
|
|
134
|
+
google.cloud.logging.Resource
|
|
135
|
+
"""
|
|
136
|
+
instance = retrieve_metadata_server(_GCE_INSTANCE_ID)
|
|
137
|
+
zone = retrieve_metadata_server(_ZONE_ID)
|
|
138
|
+
project = retrieve_metadata_server(_PROJECT_NAME)
|
|
139
|
+
resource = Resource(
|
|
140
|
+
type="gce_instance",
|
|
141
|
+
labels={
|
|
142
|
+
"project_id": project if project else "",
|
|
143
|
+
"instance_id": instance if instance else "",
|
|
144
|
+
"zone": zone if zone else "",
|
|
145
|
+
},
|
|
146
|
+
)
|
|
147
|
+
return resource
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _create_cloud_run_service_resource():
|
|
151
|
+
"""Create a standardized Cloud Run service resource.
|
|
152
|
+
Returns:
|
|
153
|
+
google.cloud.logging.Resource
|
|
154
|
+
"""
|
|
155
|
+
region = retrieve_metadata_server(_REGION_ID)
|
|
156
|
+
project = retrieve_metadata_server(_PROJECT_NAME)
|
|
157
|
+
resource = Resource(
|
|
158
|
+
type="cloud_run_revision",
|
|
159
|
+
labels={
|
|
160
|
+
"project_id": project if project else "",
|
|
161
|
+
"service_name": os.environ.get(_CLOUD_RUN_SERVICE_ID, ""),
|
|
162
|
+
"revision_name": os.environ.get(_CLOUD_RUN_REVISION_ID, ""),
|
|
163
|
+
"location": region.split("/")[-1] if region else "",
|
|
164
|
+
"configuration_name": os.environ.get(_CLOUD_RUN_CONFIGURATION_ID, ""),
|
|
165
|
+
},
|
|
166
|
+
)
|
|
167
|
+
return resource
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _create_cloud_run_job_resource():
|
|
171
|
+
"""Create a standardized Cloud Run job resource.
|
|
172
|
+
Returns:
|
|
173
|
+
google.cloud.logging.Resource
|
|
174
|
+
"""
|
|
175
|
+
region = retrieve_metadata_server(_REGION_ID)
|
|
176
|
+
project = retrieve_metadata_server(_PROJECT_NAME)
|
|
177
|
+
resource = Resource(
|
|
178
|
+
type=_CLOUD_RUN_JOB_RESOURCE_TYPE,
|
|
179
|
+
labels={
|
|
180
|
+
"project_id": project if project else "",
|
|
181
|
+
"job_name": os.environ.get(_CLOUD_RUN_JOB_ID, ""),
|
|
182
|
+
"location": region.split("/")[-1] if region else "",
|
|
183
|
+
},
|
|
184
|
+
)
|
|
185
|
+
return resource
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _create_app_engine_resource():
|
|
189
|
+
"""Create a standardized App Engine resource.
|
|
190
|
+
Returns:
|
|
191
|
+
google.cloud.logging.Resource
|
|
192
|
+
"""
|
|
193
|
+
zone = retrieve_metadata_server(_ZONE_ID)
|
|
194
|
+
project = retrieve_metadata_server(_PROJECT_NAME)
|
|
195
|
+
resource = Resource(
|
|
196
|
+
type=_GAE_RESOURCE_TYPE,
|
|
197
|
+
labels={
|
|
198
|
+
"project_id": project if project else "",
|
|
199
|
+
"module_id": os.environ.get(_GAE_SERVICE_ENV, ""),
|
|
200
|
+
"version_id": os.environ.get(_GAE_VERSION_ENV, ""),
|
|
201
|
+
"zone": zone if zone else "",
|
|
202
|
+
},
|
|
203
|
+
)
|
|
204
|
+
return resource
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _create_global_resource(project):
|
|
208
|
+
"""Create a global resource.
|
|
209
|
+
Args:
|
|
210
|
+
project (str): The project ID to pass on to the resource
|
|
211
|
+
Returns:
|
|
212
|
+
google.cloud.logging.Resource
|
|
213
|
+
"""
|
|
214
|
+
return Resource(type="global", labels={"project_id": project if project else ""})
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def detect_resource(project=""):
|
|
218
|
+
"""Return the default monitored resource based on the local environment.
|
|
219
|
+
If GCP resource not found, defaults to `global`.
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
project (str): The project ID to pass on to the resource (if needed)
|
|
223
|
+
Returns:
|
|
224
|
+
google.cloud.logging.Resource: The default resource based on the environment
|
|
225
|
+
"""
|
|
226
|
+
gke_cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME)
|
|
227
|
+
gce_instance_name = retrieve_metadata_server(_GCE_INSTANCE_ID)
|
|
228
|
+
|
|
229
|
+
if all([env in os.environ for env in _GAE_ENV_VARS]):
|
|
230
|
+
# App Engine Flex or Standard
|
|
231
|
+
return _create_app_engine_resource()
|
|
232
|
+
elif gke_cluster_name is not None:
|
|
233
|
+
# Kubernetes Engine
|
|
234
|
+
return _create_kubernetes_resource()
|
|
235
|
+
elif all([env in os.environ for env in _LEGACY_FUNCTION_ENV_VARS]) or all(
|
|
236
|
+
[env in os.environ for env in _FUNCTION_ENV_VARS]
|
|
237
|
+
):
|
|
238
|
+
# Cloud Functions
|
|
239
|
+
return _create_functions_resource()
|
|
240
|
+
elif all([env in os.environ for env in _CLOUD_RUN_SERVICE_ENV_VARS]):
|
|
241
|
+
# Cloud Run
|
|
242
|
+
return _create_cloud_run_service_resource()
|
|
243
|
+
elif all([env in os.environ for env in _CLOUD_RUN_JOB_ENV_VARS]):
|
|
244
|
+
# Cloud Run
|
|
245
|
+
return _create_cloud_run_job_resource()
|
|
246
|
+
elif gce_instance_name is not None:
|
|
247
|
+
# Compute Engine
|
|
248
|
+
return _create_compute_resource()
|
|
249
|
+
else:
|
|
250
|
+
# use generic global resource
|
|
251
|
+
return _create_global_resource(project)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
@functools.lru_cache(maxsize=None)
|
|
255
|
+
def _get_environmental_labels(resource_type):
|
|
256
|
+
"""Builds a dictionary of labels to be inserted into a LogRecord of the given resource type.
|
|
257
|
+
This function should only build a dict of items that are consistent across multiple logging statements
|
|
258
|
+
of the same resource type, such as environment variables. Th
|
|
259
|
+
|
|
260
|
+
Returns:
|
|
261
|
+
dict:
|
|
262
|
+
A dict representation of labels and the values of those labels
|
|
263
|
+
"""
|
|
264
|
+
labels = {}
|
|
265
|
+
environ_vars = {
|
|
266
|
+
_CLOUD_RUN_JOB_RESOURCE_TYPE: {
|
|
267
|
+
_CLOUD_RUN_JOBS_EXECUTION_NAME_LABEL: _CLOUD_RUN_EXECUTION_ID,
|
|
268
|
+
_CLOUD_RUN_JOBS_TASK_INDEX_LABEL: _CLOUD_RUN_TASK_INDEX,
|
|
269
|
+
_CLOUD_RUN_JOBS_TASK_ATTEMPT_LABEL: _CLOUD_RUN_TASK_ATTEMPT,
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if resource_type in environ_vars:
|
|
274
|
+
for key, env_var in environ_vars[resource_type].items():
|
|
275
|
+
val = os.environ.get(env_var, "")
|
|
276
|
+
if val:
|
|
277
|
+
labels[key] = val
|
|
278
|
+
|
|
279
|
+
return labels
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def add_resource_labels(resource: Resource, record: logging.LogRecord):
|
|
283
|
+
"""Returns additional labels to be appended on to a LogRecord object based on the
|
|
284
|
+
local environment. Defaults to an empty dictionary if none apply. This is only to be
|
|
285
|
+
used for CloudLoggingHandler, as the structured logging daemon already does this.
|
|
286
|
+
|
|
287
|
+
Args:
|
|
288
|
+
resource (google.cloud.logging.Resource): Resource based on the environment
|
|
289
|
+
record (logging.LogRecord): A LogRecord object representing a log record
|
|
290
|
+
Returns:
|
|
291
|
+
Dict[str, str]: New labels to append to the labels of the LogRecord
|
|
292
|
+
"""
|
|
293
|
+
if not resource:
|
|
294
|
+
return None
|
|
295
|
+
|
|
296
|
+
# Get environmental labels from the resource type
|
|
297
|
+
labels = _get_environmental_labels(resource.type)
|
|
298
|
+
|
|
299
|
+
# Add labels from log record
|
|
300
|
+
if resource.type == _GAE_RESOURCE_TYPE and record._trace is not None:
|
|
301
|
+
labels[_GAE_TRACE_ID_LABEL] = record._trace
|
|
302
|
+
|
|
303
|
+
return labels
|