Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/logging_v2/_instrumentation.py: 52%
25 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:45 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:45 +0000
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.
15"""Add diagnostic instrumentation source information to logs"""
16from google.cloud.logging_v2.entries import StructEntry
17from google.cloud.logging_v2 import __version__
19_DIAGNOSTIC_INFO_KEY = "logging.googleapis.com/diagnostic"
20_INSTRUMENTATION_SOURCE_KEY = "instrumentation_source"
21_PYTHON_LIBRARY_NAME = "python"
23_LIBRARY_VERSION = __version__
25_MAX_NAME_LENGTH = 14
26_MAX_VERSION_LENGTH = 14
27_MAX_INSTRUMENTATION_ENTRIES = 3
30def _add_instrumentation(entries, **kw):
31 """Add instrumentation information to a list of entries
33 A new diagnostic entry is prepended to the list of
34 entries.
36 Args:
37 entries (Sequence[Mapping[str, ...]]): sequence of mappings representing
38 the log entry resources to log.
40 Returns:
41 Sequence[Mapping[str, ...]]: entries with instrumentation info added to
42 the beginning of list.
43 """
45 diagnostic_entry = _create_diagnostic_entry(**kw)
46 entries.insert(0, diagnostic_entry.to_api_repr())
47 return entries
50def _create_diagnostic_entry(name=_PYTHON_LIBRARY_NAME, version=_LIBRARY_VERSION, **kw):
51 """Create a diagnostic log entry describing this library
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"}]}}
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')
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 # only keep the log_name and resource from the parent log
71 allow_list = ("log_name", "resource")
72 active_kws = {k: v for k, v in kw.items() if k in allow_list}
73 entry = StructEntry(payload=payload, **active_kws)
74 return entry
77def _get_instrumentation_source(name=_PYTHON_LIBRARY_NAME, version=_LIBRARY_VERSION):
78 """Gets a JSON representation of the instrumentation_source
80 Args:
81 name(str): The name of this library (e.g. 'python')
82 version(str) The version of this library (e.g. '3.0.0')
83 Returns:
84 obj: JSON object with library information
85 """
86 source = {"name": name, "version": version}
87 # truncate strings to no more than _MAX_NAME_LENGTH characters
88 for key, val in source.items():
89 source[key] = (
90 val if len(val) <= _MAX_NAME_LENGTH else f"{val[:_MAX_NAME_LENGTH]}*"
91 )
92 return source