How to send custom metrics from an c++ application

Hi,

we are trying to send some custom data from our c++ application to the fluent-bit api (following the instructions here )the following way:

  auto sensor1Value = sensor1->GetValue();
  auto sensor2Value = sensor2->GetValue();
  auto sensor3Value = sensor3->GetValue();
  auto sensor4Value = sensor4->GetValue();

  std::stringstream jsonstream;
  jsonstream << "{\"sensor1\":" << sensor1Value << ",\"sensor2\":" << sensor2Value << ",\"sensor3\":" << sensor3Value << ",\"sensor4\":" << sensor4Value << "}";
  std::string jsonString = jsonstream.str();

  //WHY ??? If this is called from the console, it works, but not from the application
  std::string command = "curl -X POST http://localhost:9999/custom -H 'Content-Type: application/json' -d '"+jsonString+"'";

  FILE *file_ptr = ::popen(command.c_str(),"r"); //or system(command.c_str(),)
  if(file_ptr)
  {
    std::cout << "popen succeeded!" << command.c_str() <<std::endl;
  }
 
  if (pclose(file_ptr) != 0)
  {
    std::cout << "pclose failed!" << std::endl; 
  }

So the problem is, if the “command” string is run via the console on the board (via the VSCode extension->Open Terminal), the new custom metrics sensor1, sensor2, sensor3, … are shown under https://app.torizon.io/ “device metrics”.
But if we just run the C++ application they are not shown!

Could this be a Docker issue, we run our C++ app via the VSCode extension provided Dockerfiles.

We use the following Linux:
Linux verdin-imx8mp-07174514 5.15.129-6.4.0+git.67c3153d20ff #1-TorizonCore SMP PREEMPT Wed Sep 27 12:30:36 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux

Greetings @dan,

You’re running your application in a docker container is the key difference here. When you run the command on the command-line then it’s fine since the network in this context is what you would expect. However, a docker container has it’s own network stack by default, this means localhost in the container is not the same as localhost outside of the container.

More info on how networking with docker containers work can be found here: Networking overview | Docker Docs

In short, you need configure your container such that it can access http://localhost:9999/custom outside of the container on the host networking stack. Probably the most straightforward way to do this would be to configure your container to use the host networking stack instead of it’s own bridge network. That way the network your container uses is pretty much the same as what is outside of the container.

You could also probably do this by just exposing select port(s) from your container to the host network, instead of just exposing the entire host network. But, this largely depends on your use-case and requirements.

Best Regards,
Jeremias