0

I am writing code to remove metrics from a remote server and output them to localhost:9091. My problem is that I can't understand why the metrics are not displayed on localhost:9091 despite the fact that there is a connection to the server and the removed metrics are selected in the console.

I guess the problem is related to this part of the code:


    //creating the prometheus registry



    auto registry = std::make_shared<prometheus::Registry>();


    auto& ram_usage_gauge = prometheus::BuildGauge()
                               .Name("node_memory_usage_bytes")
                               .Help("RAM usage")
                               .Register(*registry);


    prometheus::Exposer exposer{"localhost:9091"};
    // collecting indicators every 1 seconds
    while (true) {
    try {
        // getting data about RAM on the server
        std::string output = execute_ssh_command(session, "free | grep Mem | awk '{print $3}'");
        int node_memory_usage_bytes = std::stoi(output);
        std::cout<< node_memory_usage_bytes<< std::endl;
        ram_usage_gauge.Add({{"name","id"}})
            .Set(node_memory_usage_bytes);
    } catch (std::exception& e) {
        std::cerr << "Error getting RAM usage: " << e.what() << std::endl;
    }
    // transferring metrics to port 9091

    

    exposer.RegisterCollectable(registry);
    
    std::this_thread::sleep_for(std::chrono::seconds(1));
}


in the prometheus-cpp documentation (https://jupp0r.github.io/prometheus-cpp /) nothing worthwhile has been written.

paffosx
  • 86
  • 4
  • Break the problem into two pieces. Are you retrieving the metrics properly? Are you outputting the values correctly? – Joseph Larson Mar 15 '23 at 21:02
  • @JosephLarson The connection to the server is successful and I get metrics. As to whether I am trying to upload them correctly to localhost:9091, I am not sure because there is not enough information in the documentation for this library, which is why I decided to ask the community. – paffosx Mar 15 '23 at 21:11
  • 1
    You're not going to like this. I was using that library, and I got frustrated with it and rolled my own. I'm sorry, I don't remember why I gave up on it. However, the location of exposer.RegistreCollectable() seems odd. Should this go above the loop? What happens if you curl "http://localhost:9191/metrics" while your program is running? – Joseph Larson Mar 15 '23 at 21:17
  • @JosephLarson I am very grateful to you for helping me. I didn't know I had to go to this address http://localhost:9091/metrics . However, I expected to see a graph. Thank you again. – paffosx Mar 15 '23 at 21:25
  • Prometheus works on the concept of pull technology. It expects to scrape your site. So that's what the prometheus library sets up for you -- it becomes a REST server. – Joseph Larson Mar 15 '23 at 21:28
  • @JosephLarson that is, if I want to see a graph, then I need to connect Grafana or something similar? – paffosx Mar 15 '23 at 21:30
  • Your program exposes metrics. Normally that means you would set up a Prometheus server to scrape them, and Grafana to display the data captured by Prometheus. I suppose there are ways you would point Grafana directly to your program, but I've never done anything like that. – Joseph Larson Mar 20 '23 at 21:32

0 Answers0