Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Possible missing import statement in google/cloud/logging_v2/logger.py #895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
parthea opened this issue May 22, 2024 · 0 comments · Fixed by #896
Closed

Possible missing import statement in google/cloud/logging_v2/logger.py #895

parthea opened this issue May 22, 2024 · 0 comments · Fixed by #896
Assignees
Labels
api: logging Issues related to the googleapis/python-logging API.

Comments

@parthea
Copy link
Contributor

parthea commented May 22, 2024

See the code below where we have google.cloud.logging_v2._instrumentation_emitted. I believe there should be an import statement import google.cloud.logging_v2 but there does not appear to be.

if google.cloud.logging_v2._instrumentation_emitted is False:

"""Define API Loggers."""
import collections
import re
from google.cloud.logging_v2._helpers import _add_defaults_to_filter
from google.cloud.logging_v2.entries import LogEntry
from google.cloud.logging_v2.entries import ProtobufEntry
from google.cloud.logging_v2.entries import StructEntry
from google.cloud.logging_v2.entries import TextEntry
from google.cloud.logging_v2.resource import Resource
from google.cloud.logging_v2.handlers._monitored_resources import detect_resource
from google.cloud.logging_v2._instrumentation import _add_instrumentation
from google.api_core.exceptions import InvalidArgument
from google.rpc.error_details_pb2 import DebugInfo
import google.protobuf.message
_GLOBAL_RESOURCE = Resource(type="global", labels={})

See stack trace below

self = <google.cloud.logging.tests.unit.test_logger.TestLogger testMethod=test_first_log_emits_instrumentation>

    def test_first_log_emits_instrumentation(self):
        import google.cloud.logging_v2
        from google.cloud.logging_v2._instrumentation import _create_diagnostic_entry
        from google.cloud.logging_v2.handlers._monitored_resources import (
            detect_resource,
        )
    
        google.cloud.logging_v2._instrumentation_emitted = False
        DEFAULT_LABELS = {"foo": "spam"}
        resource = detect_resource(self.PROJECT)
        instrumentation_entry = _create_diagnostic_entry(
            resource=resource,
            labels=DEFAULT_LABELS,
        ).to_api_repr()
        instrumentation_entry["logName"] = "projects/%s/logs/%s" % (
            self.PROJECT,
            self.LOGGER_NAME,
        )
        ENTRIES = [
            instrumentation_entry,
            {
                "logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME),
                "resource": resource._to_dict(),
                "labels": DEFAULT_LABELS,
            },
        ]
        client = _Client(self.PROJECT)
        api = client.logging_api = _DummyLoggingAPI()
        logger = self._make_one(self.LOGGER_NAME, client=client, labels=DEFAULT_LABELS)
>       logger.log_empty()

tests/unit/test_logger.py:1074: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../logging_v2/logger.py:183: in log_empty
    self._do_log(client, LogEntry, **kw)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <google.cloud.logging_v2.logger.Logger object at 0x129abdccd510>
client = <google.cloud.logging.tests.unit.test_logger._Client object at 0x129abb8f5350>
_entry_class = <class 'google.cloud.logging_v2.entries.LogEntry'>
payload = None
kw = {'labels': {'foo': 'spam'}, 'log_name': 'projects/test-project/logs/logger-name', 'resource': Resource(type='global', labels={'project_id': 'test-project'})}
severity = None
entry = LogEntry(log_name='projects/test-project/logs/logger-name', labels={'foo': 'spam'}, insert_id=None, severity=None, htt...ject'}), trace=None, span_id=None, trace_sampled=None, source_location=None, operation=None, logger=None, payload=None)
api_repr = {'labels': {'foo': 'spam'}, 'logName': 'projects/test-project/logs/logger-name', 'resource': {'labels': {'project_id': 'test-project'}, 'type': 'global'}}
entries = [{'labels': {'foo': 'spam'}, 'logName': 'projects/test-project/logs/logger-name', 'resource': {'labels': {'project_id': 'test-project'}, 'type': 'global'}}]

    def _do_log(self, client, _entry_class, payload=None, **kw):
        """Helper for :meth:`log_empty`, :meth:`log_text`, etc."""
        client = self._require_client(client)
    
        # Apply defaults
        kw["log_name"] = kw.pop("log_name", self.full_name)
        kw["labels"] = kw.pop("labels", self.labels)
        kw["resource"] = kw.pop("resource", self.default_resource)
    
        severity = kw.get("severity", None)
        if isinstance(severity, str):
            # convert severity to upper case, as expected by enum definition
            kw["severity"] = severity.upper()
    
        if isinstance(kw["resource"], collections.abc.Mapping):
            # if resource was passed as a dict, attempt to parse it into a
            # Resource object
            try:
                kw["resource"] = Resource(**kw["resource"])
            except TypeError as e:
                # dict couldn't be parsed as a Resource
                raise TypeError("invalid resource dict") from e
    
        if payload is not None:
            entry = _entry_class(payload=payload, **kw)
        else:
            entry = _entry_class(**kw)
    
        api_repr = entry.to_api_repr()
        entries = [api_repr]
>       if google.cloud.logging_v2._instrumentation_emitted is False:
E       NameError: name 'google' is not defined

logging_v2/logger.py:164: NameError
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/python-logging API. label May 22, 2024
@bhshkh bhshkh assigned gkevinzheng and unassigned bhshkh May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/python-logging API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants