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

Pub/Sub MergeFrom error publish message #5903

Closed
paulorcf opened this issue Sep 7, 2018 · 12 comments
Closed

Pub/Sub MergeFrom error publish message #5903

paulorcf opened this issue Sep 7, 2018 · 12 comments
Assignees
Labels
api: clouddebugger Issues related to the Cloud Debugger API. api: pubsub Issues related to the Pub/Sub API. priority: p2 Moderately-important priority. Fix may not be included in next release.

Comments

@paulorcf
Copy link

paulorcf commented Sep 7, 2018

🏷 pub/sub
SO: Linux/Ubuntu 16.04
api_core_version = 1.3.0
client_library_version = None
gapic_version = 0.37.2
grpc_version = 1.14.2
python_version = 3.6.4

client = pubsub.PublisherClient.from_service_account_json('account.json')
topic_path = client.topic_path('project_name', 'topic_name')
data = b'The rain in Wales falls mainly on the snails.'
response = client.publish(topic, data)

Version: google-cloud-pubsub==0.37.2

Error:

response = client.publish(topic_path, data) File "venvpy/lib/python3.6/site-packages/google/cloud/pubsub_v1/gapic/publisher_client.py", line 393, in publish messages=messages, TypeError: Parameter to MergeFrom() must be instance of same class: expected google.pubsub.v1.PubsubMessage got int.

@paulorcf
Copy link
Author

paulorcf commented Sep 7, 2018

Seems to be a problem on the pubsub.PublisherClient.from_service_account_json.
I try a different approach using the credentials and works fine!

from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file('account.json')
client = pubsub.PublisherClient(credentials=credentials)

Sorry, but now I don't have a lot of time to debug the "why".

@tseaver tseaver added type: question Request for information or clarification. Not an issue. api: pubsub Issues related to the Pub/Sub API. labels Sep 8, 2018
@tseaver
Copy link
Contributor

tseaver commented Sep 10, 2018

I don't think there can be any connection between the Client.from_service_account_json factory and the traceback you are reporting. What version of google-cloud-pubsub do you have installed? E.g.:

$ pip show google-cloud-pubsub

@paulorcf
Copy link
Author

Version: google-cloud-pubsub==0.37.2

@ericgj
Copy link

ericgj commented Sep 15, 2018

I just got the same error, google-cloud-pubsub==0.38.0. And the same workaround worked for me too (thanks Paulo). Possibly related StackOverflow question ?

@tseaver
Copy link
Contributor

tseaver commented Sep 17, 2018

@paulorcf It looks as though you are bypassing the "main" API for publishing messages (pubsub_v1.PublisherClient.publish), and instead calling the lower-level, GAPIC-generated helper (pubsub_v1.gapic.publisher_client.PublisherClient.publish]), which is definitely not recommended.

The error message you report indicates that one of the items you are passing in data is an int, rather than an instance of google.pubsub.v1.PubsubMessage. Can you show the code in your application which triggers the error?

The recommended path would be:

from google.cloud import pubsub_v1

client = pubsub_v1.PublisherClient()
topic_path = client.topic_path('my-project-123'. 'my-topic-name')
data = b'BYTES OF MESSAGE DATA'

client.publish(topic_path, data, attr='ATTR VALUE')

Please feel free to reopen / follow up if you can show the reproduction case where your app passes values using this pattern.

@tseaver tseaver closed this as completed Sep 17, 2018
@ericgj
Copy link

ericgj commented Sep 17, 2018

I believe they showed their code in the original post, re-pasting it here, it's the same I used. It's the from_service_account_json constructor.

client = pubsub.PublisherClient.from_service_account_json('account.json')
topic_path = client.topic_path('project_name', 'topic_name')
data = b'The rain in Wales falls mainly on the snails.'
response = client.publish(topic, data)

@tseaver
Copy link
Contributor

tseaver commented Sep 18, 2018

@ericgj we have passing system test for PublisherClient.publish.

Can you provide the complete traceback, along with the value you are passing to publish?

@paxsonsa
Copy link

paxsonsa commented Nov 8, 2018

Working:

from google.cloud import pubsub_v1
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file('auth.json')
client = pubsub_v1.PublisherClient(credentials=credentials)
topic = client.topic_path('project, 'topic')
client.publish(topic, b'hello')

Not Working:

from google.cloud import pubsub_v1

client = pubsub_v1.PublisherClient.from_service_account_json('auth.json')
topic = client.topic_path('project, 'topic')
client.publish(topic, b'hello')

TRACEBACK:

...
  File ".../python3.6/site-packages/google/cloud/pubsub_v1/gapic/publisher_client.py", line 395, in publish
    messages=messages,
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.pubsub.v1.PubsubMessage got int.

@tseaver
Copy link
Contributor

tseaver commented Nov 8, 2018

OK, I can reproduce here. The issue is that pubsub_v1.PublisherClient.from_service_account is returning an instance of the low-level pubsub_v1.gapic.publisher_client.PublisherClient: that class has a different signature for publish.

@tseaver tseaver reopened this Nov 8, 2018
@tseaver
Copy link
Contributor

tseaver commented Nov 8, 2018

@theacodes I believe this is the issue you tried to fix in #5826. Can you shed any light here?

@tseaver tseaver added priority: p2 Moderately-important priority. Fix may not be included in next release. api: clouddebugger Issues related to the Cloud Debugger API. and removed status: awaiting information type: question Request for information or clarification. Not an issue. labels Nov 8, 2018
@tseaver
Copy link
Contributor

tseaver commented Nov 8, 2018

Looks like #5826 returns a staticmethod, rather than a classmethod, for things that started life as classmethods. That would explain seeing the wrong cls.

tseaver added a commit that referenced this issue Nov 8, 2018
The '_gapic.add_methods' decorator doesn't quite get them right, so
blacklist them from it and create them locally.

Closes #5903.
@paxsonsa
Copy link

paxsonsa commented Nov 8, 2018

Thanks for looking into that and getting that fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: clouddebugger Issues related to the Cloud Debugger API. api: pubsub Issues related to the Pub/Sub API. priority: p2 Moderately-important priority. Fix may not be included in next release.
Projects
None yet
Development

No branches or pull requests

4 participants