index.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /**
  3. * dolphin. Collection of useful PHP skeletons.
  4. * Copyright (C) 2024 Johannes 'Banana' Keßler
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  8. *
  9. * You should have received a copy of the
  10. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  11. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  12. */
  13. /**
  14. * This is an exmaple to show how to send custom logs/metrics
  15. * to loki and use them in grafana
  16. */
  17. # basic loki endpoint information
  18. const LOKI_HOST = "localhost";
  19. const LOKI_PORT = 3100;
  20. const LOKI_PUSH_API = "/loki/api/v1/push";
  21. # loki does not have protection
  22. # https://grafana.com/docs/loki/latest/operations/authentication/
  23. # this example does use basic auth
  24. # if you do not use it, comment both constants
  25. const LOKI_USER = "name";
  26. const LOKI_USER_PW = "pass";
  27. # Loki
  28. require_once 'loki.php';
  29. # see https://grafana.com/docs/loki/latest/reference/loki-http-api/#ingest-logs for
  30. # data structure
  31. $Loki = new Loki(LOKI_HOST, LOKI_PORT, array("streamlabel1" => "value1", "streamlabel2" => "value2"));
  32. $Loki->log("log line text", array("structuredData" => "value"));
  33. $Loki->log("log line text", array("structuredData" => "value2"));
  34. # send the data to loki at the end to avoid and multiple calls and breaking
  35. # the process of the script.
  36. $Loki->send();