morganstanley / modern-cpp-kafka

A C++ API for Kafka clients (i.e. KafkaProducer, KafkaConsumer, AdminClient)
Apache License 2.0
331 stars 86 forks source link

Kafka Headers getting clobbered for some reason #237

Open samkhopkar opened 1 week ago

samkhopkar commented 1 week ago

I am setting the Kafka headers as follows in the publisher record

   ```
     string app_name = AppInfo::app_info().app_name();
        string host_name = IPAddress::our_host_name();
    record->headers() = {{
        kafka::Header{kafka::Header::Key{"Source"},  kafka::Header::Value{app_name.c_str(), app_name.size()}},
        kafka::Header{kafka::Header::Key{"Host"}, kafka::Header::Value{host_name.c_str(), host_name.size()}}
    }};

When I try to print them in the consumer as follows as string they seem be clobbered
   const string Headers() const 
    { 
        std::ostringstream outs;
        KAFKA_API::Headers headers = _consumer_record.headers();
        std::for_each(headers.cbegin(), headers.cend(),
              [&outs](const auto& header) 
              {
                  outs << ((outs.str().length() == 0) ? "" : ",") << header.key << "=" << std::string((const char *)header.value.data(),header.value.size());
              });
        return outs.str(); 
    }


Output of Headers

` Headers [Source=^@^@^@^@^@^@^@^@Ð^H^@ð^F^?^@^@her,Host=^@^@^@^@^@^@^@^@Ð^H^@ð^F^?^@^@group.com]`

Any ideas what I am doing wrong ?