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

How to get metrics with prometheus? #976

Closed
umlumpa opened this issue Oct 19, 2023 · 6 comments
Closed

How to get metrics with prometheus? #976

umlumpa opened this issue Oct 19, 2023 · 6 comments

Comments

@umlumpa
Copy link

umlumpa commented Oct 19, 2023

hello i can get metrics inside of container like this curl -X GET --unix-socket /var/run/control.unit.sock http://localhost/statusbut i cant get it outside of container please help i am using this in kubernetes! before i use https://github.com/hipages/php-fpm_exporter for metrics in php-fpm !

@lcrilly
Copy link
Contributor

lcrilly commented Oct 19, 2023

You can run a PHP application inside the Unit container to read the /status endpoint and expose the data in Prometheus format.

I hope this helps, please let me know
https://gist.github.com/lcrilly/ad516de8378dd8ae5303a942e67d55b5

@umlumpa
Copy link
Author

umlumpa commented Oct 20, 2023

Can you send me some example please? Not fully understand! i have like this conf for my php app!
how to add here?

{
  "listeners":{
    "*:80":{
      "pass":"routes"
    }
  },
  "routes":[
    {
      "match":{
        "uri":[
          "!/assets/*",
          "*.php",
          "*.php/*"
        ]
      },
      "action":{
        "pass":"applications/php/direct"
      }
    },
    {
      "action":{
        "share":"/var/www/html/src/frontend/web$uri",
        "fallback":{
          "pass":"applications/php/index"
        }
      }
    }
  ],
  "applications":{
    "php":{
      "type":"php",
      "limits": {
        "timeout": 60,
        "requests": 10000
      },
      "options": {
        "file": "/etc/php.ini",
        "admin": {
          "memory_limit": "1000M",
          "variables_order": "EGPCS"
        },
        "user": {
          "display_errors": "0"
        }
      },
      "processes":{
        "max":30,
        "spare":1,
        "idle_timeout":5
      },
      "targets":{
        "direct":{
          "root":"/var/www/html/src/frontend/web"
        },
        "index":{
          "root":"/var/www/html/src/frontend/web",
          "script":"index.php"
        }
      }
    }
  }
}

@lcrilly
Copy link
Contributor

lcrilly commented Oct 20, 2023

OK, here's a complete solution for you :)

  1. Copy this PHP application to /var/www/unit/prometheus.php
<?php
$sock = stream_socket_client("unix://".getenv("control_socket"), $errno, $errst);
fwrite($sock, "GET /status HTTP/1.0\r\n\r\n");
$resp = fread($sock, 4096);
fclose($sock);

list($headers, $body) = explode("\r\n\r\n", $resp, 2);
$json = json_decode($body);

$metrics = array();
array_push($metrics, "unit_connections_accepted_total ".$json->connections->accepted);
array_push($metrics, "unit_connections_active ".$json->connections->active);
array_push($metrics, "unit_connections_idle ".$json->connections->idle);
array_push($metrics, "unit_connections_closed_total ".$json->connections->closed);
array_push($metrics, "unit_requests_total ".$json->requests->total);

foreach($json->applications as $application => $data) {
    array_push($metrics, "unit_application_".$application."_processes_running ".$data->processes->running);
    array_push($metrics, "unit_application_".$application."_processes_starting ".$data->processes->starting);
    array_push($metrics, "unit_application_".$application."_processes_idle ".$data->processes->idle);
    array_push($metrics, "unit_application_".$application."_requests_active ".$data->requests->active);
}

header("Content-Type: text/plain");
echo join("\n", $metrics)."\n";
?>
  1. Apply this configuration
{
  "listeners": {
    "*:80": {
      "pass": "routes"
    }
  },
  "routes": [
    {
      "match": {
        "uri": "/metrics"
      },
      "action": {
        "pass": "applications/prom"
      }
    },
    {
      "match": {
        "uri": [
          "!/assets/*",
          "*.php",
          "*.php/*"
        ]
      },
      "action": {
        "pass": "applications/php/direct"
      }
    },
    {
      "action": {
        "share": "/var/www/html/src/frontend/web$uri",
        "fallback": {
          "pass": "applications/php/index"
        }
      }
    }
  ],
  "applications": {
    "prom": {
      "type": "php",
      "root": "/var/www/unit",
      "script": "prometheus.php",
      "user": "root",
      "environment": {
        "control_socket": "/var/run/control.unit.sock"
      }
    },
    "php": {
      "type": "php",
      "limits": {
        "timeout": 60,
        "requests": 10000
      },
      "options": {
        "file": "/etc/php.ini",
        "admin": {
          "memory_limit": "1000M",
          "variables_order": "EGPCS"
        },
        "user": {
          "display_errors": "0"
        }
      },
      "processes": {
        "max": 30,
        "spare": 1,
        "idle_timeout": 5
      },
      "targets": {
        "direct": {
          "root": "/var/www/html/src/frontend/web"
        },
        "index": {
          "root": "/var/www/html/src/frontend/web",
          "script": "index.php"
        }
      }
    }
  }
}
  1. Test the Prometheus exporter
curl http://localhost/metrics
  1. Tell me if it works :)

@umlumpa
Copy link
Author

umlumpa commented Oct 20, 2023

thnx a lot this is works! but problem is in permission of /var/run/control.unit.sock

@lcrilly
Copy link
Contributor

lcrilly commented Oct 20, 2023

Yes, you need to manage the ownership and permissions of the control socket yourself. The plan is to solve this issue with something like #840 but for now the simplest solution is to run the Prometheus exporter application as root.

@umlumpa
Copy link
Author

umlumpa commented Oct 20, 2023

thnx a lot! appreciate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants