Skip to content
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

Bug, double conversion. #3129

Closed
sactre opened this issue Mar 10, 2017 · 5 comments · Fixed by #3144
Closed

Bug, double conversion. #3129

sactre opened this issue Mar 10, 2017 · 5 comments · Fixed by #3144
Assignees
Labels
api: logging Issues related to the Cloud Logging API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@sactre
Copy link

sactre commented Mar 10, 2017

in #2860 line: 163, file: logging/google/cloud/logging/logger.py

the function _make_entry_resource return a resource(dict), but de timestamp element has to be a datetime not a string. Because in the next step the timestamp has a conversion from datetime to string (file: _gax.py, function: _log_entry_mapping_to_pb).

@daspecster daspecster added the api: logging Issues related to the Cloud Logging API. label Mar 10, 2017
@Fkawala
Copy link

Fkawala commented Mar 13, 2017

@sactre that's wired ! from the source I get the same understanding than you, it should fail. However, I currently use the timestamp parameter in the log_struct function and it works like a charm.

Do you have a stack trace to investigate on ?

@plowman
Copy link

plowman commented Mar 14, 2017

@Fkawala I'm seeing the same thing that @sactre sees. Specifically, if I specify a timestamp, it fails.

Using a string timestamp

    from google.cloud import logging as google_logging
    one_week_ago = datetime.utcnow() - timedelta(days=7)
    one_week_ago = one_week_ago.strftime("%Y-%m-%dT%H:%M:%S.%fZ")

    logging_client = google_logging.Client(project="my-project")
    logger = logging_client.logger("mobile-logger")

    logger.log_text("My Logs Here", timestamp=one_week_ago)

String timestamp stack trace:

  File "/project/project3/project/servers/apis/mobile/api_helpers.py", line 261, in _inner_method
    return api_method(api_instance, request, *args, **kwargs)
  File "/project/project3/project/servers/apis/mobile/resources/logs.py", line 144, in write_bulk
    logger.log_text("My Logs Here", timestamp=one_week_ago)
  File "/home/vagrant/.virtualenvs/project3/local/lib/python2.7/site-packages/google/cloud/logging/logger.py", line 201, in log_text
    http_request=http_request, timestamp=timestamp)
  File "/home/vagrant/.virtualenvs/project3/local/lib/python2.7/site-packages/google/cloud/logging/logger.py", line 163, in _make_entry_resource
    resource['timestamp'] = _datetime_to_rfc3339(timestamp)
  File "/home/vagrant/.virtualenvs/project3/local/lib/python2.7/site-packages/google/cloud/_helpers.py", line 329, in _datetime_to_rfc3339
    return value.strftime(_RFC3339_MICROS)
AttributeError: 'str' object has no attribute 'strftime'

Using a datetime timestamp

    from google.cloud import logging as google_logging
    one_week_ago = datetime.utcnow() - timedelta(days=7)

    logging_client = google_logging.Client(project="my-project")
    logger = logging_client.logger("mobile-logger")

    logger.log_text("My Logs Here", timestamp=one_week_ago)

Datetime timestamp stack trace:

  File "/project/project3/project/servers/apis/mobile/api_helpers.py", line 261, in _inner_method
    return api_method(api_instance, request, *args, **kwargs)
  File "/project/project3/project/servers/apis/mobile/resources/logs.py", line 143, in write_bulk
    logger.log_text("My Logs Here", timestamp=one_week_ago)
  File "/home/vagrant/.virtualenvs/project3/local/lib/python2.7/site-packages/google/cloud/logging/logger.py", line 202, in log_text
    client.logging_api.write_entries([entry_resource])
  File "/home/vagrant/.virtualenvs/project3/local/lib/python2.7/site-packages/google/cloud/logging/_gax.py", line 128, in write_entries
    entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]
  File "/home/vagrant/.virtualenvs/project3/local/lib/python2.7/site-packages/google/cloud/logging/_gax.py", line 456, in _log_entry_mapping_to_pb
    mapping['timestamp'] = _datetime_to_rfc3339(mapping['timestamp'])
  File "/home/vagrant/.virtualenvs/project3/local/lib/python2.7/site-packages/google/cloud/_helpers.py", line 329, in _datetime_to_rfc3339
    return value.strftime(_RFC3339_MICROS)
AttributeError: 'str' object has no attribute 'strftime'

@Fkawala
Copy link

Fkawala commented Mar 15, 2017

@plowman Thanks for the MRE! We observe different behaviors because I use the Batch class instead of Logger class. A Batch instance is created by the batch method of the Logger class.

Methods log_text, log_structand log_proto from Batch simply stack entries for further use. These methods, contrary to their counterpart from Logger do not call to the _make_entry_resource method. The reason why my code works it that the commit method of Batch does not convert the datetime timestamp to string.

From the docs. I understand that is it correct to convert datetime into string in _make_entry_resource to convert the datetime into string. Therefore, I guess that the fix should be done on the "gax side". If we go this way we would have to fix the commit method from Batch. That being said I don't know what could break if we go this way.

Maybe a better way would be to refactor so that commit method from Batch shares code with the _make_entry_resource method.

@daspecster Could you flag this as bug ? Could you advise me about the right way to fix that ?

@daspecster daspecster added the type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. label Mar 15, 2017
@daspecster
Copy link
Contributor

@plowman, @Fkawala, @sactre this is great information.

@Fkawala, I believe the best place to fix is where @sactre mentioned. I'll take a stab at it right now, I think it should be pretty simple to fix.

@Fkawala
Copy link

Fkawala commented Mar 15, 2017

@daspecster great !

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 Cloud Logging API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants