forked from googleapis/python-logging
/
_helpers.py
152 lines (113 loc) · 4.61 KB
/
_helpers.py
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
# Copyright 2016 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
#
# http://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.
"""Common logging helpers."""
import logging
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import requests
from google.cloud.logging.entries import LogEntry
from google.cloud.logging.entries import ProtobufEntry
from google.cloud.logging.entries import StructEntry
from google.cloud.logging.entries import TextEntry
try:
from google.cloud.logging_v2.gapic.enums import LogSeverity
except ImportError: # pragma: NO COVER
class LogSeverity(object):
"""Map severities for non-GAPIC usage."""
DEFAULT = 0
DEBUG = 100
INFO = 200
NOTICE = 300
WARNING = 400
ERROR = 500
CRITICAL = 600
ALERT = 700
EMERGENCY = 800
_NORMALIZED_SEVERITIES = {
logging.CRITICAL: LogSeverity.CRITICAL,
logging.ERROR: LogSeverity.ERROR,
logging.WARNING: LogSeverity.WARNING,
logging.INFO: LogSeverity.INFO,
logging.DEBUG: LogSeverity.DEBUG,
logging.NOTSET: LogSeverity.DEFAULT,
}
_TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%f%z"
"""Time format for timestamps used in API"""
METADATA_URL = "http://metadata.google.internal./computeMetadata/v1/"
METADATA_HEADERS = {"Metadata-Flavor": "Google"}
def entry_from_resource(resource, client, loggers):
"""Detect correct entry type from resource and instantiate.
:type resource: dict
:param resource: One entry resource from API response.
:type client: :class:`~google.cloud.logging.client.Client`
:param client: Client that owns the log entry.
:type loggers: dict
:param loggers:
A mapping of logger fullnames -> loggers. If the logger
that owns the entry is not in ``loggers``, the entry
will have a newly-created logger.
:rtype: :class:`~google.cloud.logging.entries._BaseEntry`
:returns: The entry instance, constructed via the resource
"""
if "textPayload" in resource:
return TextEntry.from_api_repr(resource, client, loggers)
if "jsonPayload" in resource:
return StructEntry.from_api_repr(resource, client, loggers)
if "protoPayload" in resource:
return ProtobufEntry.from_api_repr(resource, client, loggers)
return LogEntry.from_api_repr(resource, client, loggers)
def retrieve_metadata_server(metadata_key):
"""Retrieve the metadata key in the metadata server.
See: https://cloud.google.com/compute/docs/storing-retrieving-metadata
:type metadata_key: str
:param metadata_key: Key of the metadata which will form the url. You can
also supply query parameters after the metadata key.
e.g. "tags?alt=json"
:rtype: str
:returns: The value of the metadata key returned by the metadata server.
"""
url = METADATA_URL + metadata_key
try:
response = requests.get(url, headers=METADATA_HEADERS)
if response.status_code == requests.codes.ok:
return response.text
except requests.exceptions.RequestException:
# Ignore the exception, connection failed means the attribute does not
# exist in the metadata server.
pass
return None
def _normalize_severity(stdlib_level):
"""Normalize a Python stdlib severity to LogSeverity enum.
:type stdlib_level: int
:param stdlib_level: 'levelno' from a :class:`logging.LogRecord`
:rtype: int
:returns: Corresponding Stackdriver severity.
"""
return _NORMALIZED_SEVERITIES.get(stdlib_level, stdlib_level)
def _add_defaults_to_filter(filter_):
"""Modify the input filter expression to add sensible defaults.
:type filter_: str
:param filter_: The original filter expression
:rtype: str
:returns: sensible default filter string
"""
# By default, requests should only return logs in the last 24 hours
yesterday = datetime.now(timezone.utc) - timedelta(days=1)
time_filter = 'timestamp>="%s"' % yesterday.strftime(_TIME_FORMAT)
if filter_ is None:
filter_ = time_filter
elif "timestamp" not in filter_.lower():
filter_ = "%s AND %s" % (filter_, time_filter)
return filter_