google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

pubsub Message class does not set timestamp

Open kir-titievsky opened this issue 10 years ago • 6 comments

pubsub.Message timestamps are not set. There are two object properties, neither of them is set. What's even more confusing is that attempting to access an attribute of an instance throws an error.

To reproduce:

from gcloud import pubsub
t = pubsub.Client(project='...').topic('${TOPIC}')
# MAY NEED t.create()
s = t.subscription('${SUBSCRIPTION}')
# MAY NEED s.create()
t.publish('test data')  # RETURNS A MSG ID
ack_id, m = s.pull()  # WAS t.pull()

kir-titievsky avatar Sep 02 '16 22:09 kir-titievsky

Trying to repro right now.

dhermes avatar Sep 02 '16 22:09 dhermes

@kir-google-cloud-pubsub I can't reproduce. ack_id, m = s.pull() is essentially invalid, but maybe you meant to say

pairs = s.pull()
ack_id, m = pairs[0]

When I run the slightly modified code, I get

>>> pub_id = t.publish('test data')
>>> pub_id
u'44126525330808'
>>> msgs = s.pull()
>>> assert len(msgs) == 1
>>> ack_id, m = msgs[0]
>>> m.message_id
u'44126525330808'
>>> m.message_id == pub_id
True
>>> m.data
'test data'

Any more details you can provide?

dhermes avatar Sep 02 '16 22:09 dhermes

You are right -- about the mistake I meant. In your code, try m.timestamp -- it should be set, but is not.

kir-titievsky avatar Sep 07 '16 20:09 kir-titievsky

also, m.service_timestamp

kir-titievsky avatar Sep 07 '16 20:09 kir-titievsky

There are two timestamps possible on a message:

  • message.timestamp is in the attributes of the message, and must therefore be set by the topic: setting those is controlled by the topic.timestamp_messages attribute, which defaults to False.
  • message.service_timestamp is set by the back-end, and parsed from the publishTime field of the pulled message resource.

To activate the first kind:

from gcloud.pubsub.client import Client
client = Client(project='...')
topic = client.topic('${TOPIC}', timestamp_messages=True)
t.create()
s = t.subscription('${SUBSCRIPTION}')
s.create()
t.publish('test data')
ack_ids = []
for ack_id, m in s.pull():
    ack_ids.append(ack_id)
    print(m.data, m.timestamp, m.service_timetamp)
sub.acknowledge(ack_ids)

On my box, the previous example yields:

(datetime.datetime(2016, 9, 7, 20, 42, 18, 810563, tzinfo=<UTC>), 'test_data', None)

That tells me that for some reason the back-end is not setting the publishTime field in the message resource it is returning, but that the timestamp attribute set by the publishing application is being set.

tseaver avatar Sep 07 '16 20:09 tseaver

It appears that the GAX/gRPC adapter for pubsub is not mediating between the publish_time field in the PublishedMessage proto and the expected publishTime field (as returned from the JSON-over-HTTP API).

tseaver avatar Sep 07 '16 21:09 tseaver