|
| 1 | +# Copyright 2022 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 | +"""Add diagnostic instrumentation source information to logs""" |
| 16 | +from google.cloud.logging_v2.entries import StructEntry |
| 17 | +from google.cloud.logging_v2 import __version__ |
| 18 | + |
| 19 | +_DIAGNOSTIC_INFO_KEY = "logging.googleapis.com/diagnostic" |
| 20 | +_INSTRUMENTATION_SOURCE_KEY = "instrumentation_source" |
| 21 | +_PYTHON_LIBRARY_NAME = "python" |
| 22 | + |
| 23 | +_LIBRARY_VERSION = __version__ |
| 24 | + |
| 25 | +_MAX_NAME_LENGTH = 14 |
| 26 | +_MAX_VERSION_LENGTH = 14 |
| 27 | +_MAX_INSTRUMENTATION_ENTRIES = 3 |
| 28 | + |
| 29 | + |
| 30 | +def _add_instrumentation(entries, **kw): |
| 31 | + """Add instrumentation information to a list of entries |
| 32 | +
|
| 33 | + A new diagnostic entry is prepended to the list of |
| 34 | + entries. |
| 35 | +
|
| 36 | + Args: |
| 37 | + entries (Sequence[Mapping[str, ...]]): sequence of mappings representing |
| 38 | + the log entry resources to log. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + Sequence[Mapping[str, ...]]: entries with instrumentation info added to |
| 42 | + the beginning of list. |
| 43 | + """ |
| 44 | + |
| 45 | + diagnostic_entry = _create_diagnostic_entry(**kw) |
| 46 | + entries.insert(0, diagnostic_entry.to_api_repr()) |
| 47 | + return entries |
| 48 | + |
| 49 | + |
| 50 | +def _create_diagnostic_entry(name=_PYTHON_LIBRARY_NAME, version=_LIBRARY_VERSION, **kw): |
| 51 | + """Create a diagnostic log entry describing this library |
| 52 | +
|
| 53 | + The diagnostic log consists of a list of library name and version objects |
| 54 | + that have handled a given log entry. If this library is the originator |
| 55 | + of the log entry, it will look like: |
| 56 | + {logging.googleapis.com/diagnostic: {instrumentation_source: [{name: "python", version: "3.0.0"}]}} |
| 57 | +
|
| 58 | + Args: |
| 59 | + name(str): The name of this library (e.g. 'python') |
| 60 | + version(str) The version of this library (e.g. '3.0.0') |
| 61 | +
|
| 62 | + Returns: |
| 63 | + google.cloud.logging_v2.LogEntry: Log entry with library information |
| 64 | + """ |
| 65 | + payload = { |
| 66 | + _DIAGNOSTIC_INFO_KEY: { |
| 67 | + _INSTRUMENTATION_SOURCE_KEY: [_get_instrumentation_source(name, version)] |
| 68 | + } |
| 69 | + } |
| 70 | + kw["severity"] = "INFO" |
| 71 | + entry = StructEntry(payload=payload, **kw) |
| 72 | + return entry |
| 73 | + |
| 74 | + |
| 75 | +def _get_instrumentation_source(name=_PYTHON_LIBRARY_NAME, version=_LIBRARY_VERSION): |
| 76 | + """Gets a JSON representation of the instrumentation_source |
| 77 | +
|
| 78 | + Args: |
| 79 | + name(str): The name of this library (e.g. 'python') |
| 80 | + version(str) The version of this library (e.g. '3.0.0') |
| 81 | + Returns: |
| 82 | + obj: JSON object with library information |
| 83 | + """ |
| 84 | + source = {"name": name, "version": version} |
| 85 | + # truncate strings to no more than _MAX_NAME_LENGTH characters |
| 86 | + for key, val in source.items(): |
| 87 | + source[key] = ( |
| 88 | + val if len(val) <= _MAX_NAME_LENGTH else f"{val[:_MAX_NAME_LENGTH]}*" |
| 89 | + ) |
| 90 | + return source |
0 commit comments