Sending metadata in gRPC message

6,096 views
Skip to first unread message

Martin Grześlowski

unread,
Jun 3, 2015, 5:30:17 AM6/3/15
to grp...@googlegroups.com
What's the easiest way to send some metadata (timestamp, server version, etc.)  that can’t be included into proto file. I found that there is Metadata class which can be helpful, but I have no idea how can I inject my own metadata. 
 
Thanks in andvance!

Michael Larson

unread,
Jun 3, 2015, 11:36:42 AM6/3/15
to grp...@googlegroups.com
you get to set client metadata (sent first thing when a client connects), server initial metadata (same), and server trailing metadata (sent at the end)

see the short description here: https://github.com/grpc/grpc

the metadata is a key (string) followed by a value (string).  In c++ this is a multimap because it does allow repeated keys.  I forget where I found the documentation but I think there are some disallowed characters.  Hasn't mattered for me yet so I didn't dig further.

in c++, this looks like:

client sending metadata:

before calling the stub:
context.AddMetadata("foo","hi");


server parsing metadata:

inside the stub:

//simplified search: finds only the first "foo" key (in my applications I don't repeat anything)
        std::multimap<grpc::string, grpc::string>::const_iterator foo_iterator = context->client_metadata().find("foo");

        if(foo_iterator != context->client_metadata().end())
        {
                std::cout << "client metadata for foo: " << foo_iterator->second << std::endl;
        }


other metadata types are similar, look in the context headers for function names:

Michael

Craig Tiller

unread,
Jun 3, 2015, 11:40:21 AM6/3/15
to Michael Larson, grp...@googlegroups.com
Regarding disallowed characters:

Normal metadata fields are allowed ASCII codepoints 32 through 126 inclusive (so, control characters and DEL are disallowed).
Metadata fields whose key is suffixed with '-bin' are allowed any bytes at all.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/64b9b910-6f59-419e-8951-c516b7670fac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martin Grześlowski

unread,
Jun 5, 2015, 3:38:41 AM6/5/15
to grp...@googlegroups.com
Thanks! 

jsmr...@gmail.com

unread,
Sep 28, 2015, 8:37:54 PM9/28/15
to grpc.io
Hi I am trying the same on my server but i am stuck with bunch of errors like this. 

/usr/include/c++/4.8/bits/stl_tree.h:316:5: note:   template argument deduction/substitution failed:
greeter_server.cc:57:52: note:   ‘std::multimap<std::basic_string<char>, std::basic_string<char> >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >}’ is not derived from ‘const std::_Rb_tree_iterator<_Tp>’
   if(foo_iterator!= context->client_metadata().end())
                                                    ^
In file included from /usr/include/c++/4.8/memory:82:0,
                 from greeter_server.cc:35:
/usr/include/c++/4.8/bits/shared_ptr.h:354:5: note: template<class _Tp> bool std::operator!=(std::nullptr_t, const std::shared_ptr<_Tp1>&)
     operator!=(nullptr_t, const shared_ptr<_Tp>& __a) noexcept
     ^
/usr/include/c++/4.8/bits/shared_ptr.h:354:5: note:   template argument deduction/substitution failed:
greeter_server.cc:57:52: note:   cannot convert ‘foo_iterator’ (type ‘std::multimap<std::basic_string<char>, std::basic_string<char> >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >}’) to type ‘std::nullptr_t’
   if(foo_iterator!= context->client_metadata().end())


Could someone please guide?? 

Michael Larson

unread,
Sep 28, 2015, 10:42:33 PM9/28/15
to jsmr...@gmail.com, grpc.io
at some point the API changed to use grpc::string_ref which is designed like boost::string_ref, instead of grpc::string (like std::string)

change the iterator to:

std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator foo_iterator = client_metadata.find("foo");

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.

Smruthi Elapavuluri

unread,
Sep 29, 2015, 12:29:23 AM9/29/15
to Michael Larson, grpc.io
Micheal thanks,

But I want to typecast to normal string as I have a function defined that takes string as a parameter as opposed to grpc::str_ref

Michael Larson

unread,
Sep 29, 2015, 12:34:45 AM9/29/15
to grpc.io
use foo->second.data() to access the grpc::string which is std::string on most platforms (I think)

Michael Larson

unread,
Sep 29, 2015, 1:07:25 AM9/29/15
to Smruthi Elapavuluri, grpc.io
I'm copying the google group on this:

if I understand, you want to know how to approach using c++ grpc without much documentation?

I remember when this API change was made, I missed the exact commit and I didn't know what string_ref was, and it actually took me a couple hours to figure out from the error message what had happened.  I started by searching the github code and going into the headers (include/grpc++/ mainly).  You might also want to search in src/core/ or src/cpp/ to understand what is happening in the background.

Also, the vast majority of grpc's functionality is used in one or more tests.  Effectively, google programmers have already made examples for how to do individual bits.  For example:


this has a use of a metadata iterator that I think I copied.  You can quickly find these by searching for any use of the keyword you're interested in within the /test/cpp/ folder.

As time goes on there should be some larger example programs available as open source to review.  For now you have to put in some extra work because this is still new.

On Mon, Sep 28, 2015 at 9:46 PM, Smruthi Elapavuluri <jsmr...@gmail.com> wrote:
Micheal I more question I have, 

Is it mentioned somewhere how to use these APIs for ex getting metadata from the client at the server. (I couldnt get until u gave a solution and later for the string_ref to string)? I have been struggling to understand how to call these as such. 

Any suggestion on how to approach is greatly appreciated as it would help me a lot. I am currently building a project using gRPC in cPP. 

Thanks. 

On Mon, Sep 28, 2015 at 9:38 PM, Smruthi Elapavuluri <jsmr...@gmail.com> wrote:
Awesome Thank you.. 


You received this message because you are subscribed to a topic in the Google Groups "grpc.io" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/grpc-io/tNlNfYgWv7k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to grpc-io+u...@googlegroups.com.

To post to this group, send email to grp...@googlegroups.com.

Smruthi Elapavuluri

unread,
Sep 29, 2015, 1:14:30 AM9/29/15
to Michael Larson, grpc-io

Sure,
Thank you.. This helps..

Smruthi Elapavuluri

unread,
Sep 29, 2015, 7:25:46 PM9/29/15
to Michael Larson, grpc-io
Hi, 

Are there any system specific requirements to install the gRPC framework? Like the minimum gcc version needed, the system header files etc?? 

Any help is greatly appreciated. 

Thank you,
JS 

Michael Larson

unread,
Sep 29, 2015, 7:40:49 PM9/29/15
to Smruthi Elapavuluri, grpc-io
some of the c++11 things require newer compilers.  I know the cutoff for clang is somewhere between 3.0 (doesn't work) and 3.5 (works).  same deal for gcc, you need something newish, don't know the cutoff

building works for me on debian 8 / ubuntu 14.04 with the default tools available from apt-get, try that.  the release-0_11 branch is sorta stable now but head is sometimes broken, so use 0.11 to get it going

Smruthi Elapavuluri

unread,
Sep 29, 2015, 8:00:23 PM9/29/15
to Michael Larson, grpc-io

yea, I have the code building on ubuntu 14.04 but I am trying on a diff machine which has gcc 4.7 maximum version. But now the latest error is the system doesn't have few header files like epoll.h and its failing there. 

Smruthi Elapavuluri

unread,
Sep 30, 2015, 6:33:29 PM9/30/15
to Michael Larson, grpc-io
Is grpc a 64 bit version? 

The system on which I need to install gRPC has a separate toolchain and  I have updated the flags accordingly. I have installed the protobuf 3 version. 

While trying to do make on gRPC. This is failing on installation of protobuf from third party. The following are the errors noted: 


toolchain/x86_64-wrlinuxsdk-linux/usr/toolchain/bin/../lib/gcc/i686-wrs-linux-gnu/4.6.3/../../../../i686-wrs-linux-gnu/bin/ld: skipping incompatible /auto/andpkg/rep_cache//wr-x86-xeon/5.0-n9k/sysroot/intel_xeon_32_core-wrs-linux/usr/lib/libc.a when searching for -lc

cannot find crtn.o: No such file or directory

collect2: ld returned 1 exit status


Any help is greatly appreciated. 


Thanks, 
-JS


Reply all
Reply to author
Forward
0 new messages