Giter Club home page Giter Club logo

Comments (8)

knyar avatar knyar commented on August 30, 2024

This looks like https://github.com/knyar/nginx-lua-prometheus#caveats
How many metrics do you have? You can run curl -s localhost:9145/metrics | grep -c -v '^#' to check.

from nginx-lua-prometheus.

jils2013 avatar jils2013 commented on August 30, 2024

thanks for reply,more than 60,000 records,most time,two or three seconds can get all metrics. exceptional cases cost a lot; any advice ?

from nginx-lua-prometheus.

knyar avatar knyar commented on August 30, 2024

Yes, 60000 sounds like a lot. You'll need to reduce the number of metrics, or add support for external metric storage (like redis) instead of using the lua dictionary.

Keep in mind, that the metric dictionary is locked while metrics are exposed via /metrics, and it's likely that other HTTP requests for nginx are being blocked while that happens (i.e. every time you fetch metrics, other requests might experience higher latency).

from nginx-lua-prometheus.

jils2013 avatar jils2013 commented on August 30, 2024

https://groups.google.com/forum/#!topic/openresty/8MX33Dw-E38
hi,following this page,i add ngx.flush(true) to collect() long-time wait reduced greatly.
Unfortunately, this page is Chinese...

from nginx-lua-prometheus.

knyar avatar knyar commented on August 30, 2024

That's a useful data point, thanks. Could you please try applying the following patch (instead of adding ngx.flush) and check if it makes things faster for you?

diff --git a/prometheus.lua b/prometheus.lua
index 0a62166..e6e8194 100644
--- a/prometheus.lua
+++ b/prometheus.lua
@@ -504,25 +504,30 @@ function Prometheus:collect()
   table.sort(keys)
 
   local seen_metrics = {}
+  local output = {}
   for _, key in ipairs(keys) do
     local value, err = self.dict:get(key)
     if value then
       local short_name = short_metric_name(key)
       if not seen_metrics[short_name] then
         if self.help[short_name] then
-          ngx.say("# HELP " .. self.prefix .. short_name .. " " .. self.help[short_name])
+          table.insert(output, string.format("# HELP %s%s %s\n",
+            self.prefix, short_name, self.help[short_name]))
         end
         if self.type[short_name] then
-          ngx.say("# TYPE " .. self.prefix .. short_name .. " " .. self.type[short_name])
+          table.insert(output, string.format("# TYPE %s%s %s\n",
+            self.prefix, short_name, self.type[short_name]))
         end
         seen_metrics[short_name] = true
       end
       -- Replace "Inf" with "+Inf" in each metric's last bucket 'le' label.
-      ngx.say(self.prefix .. key:gsub('le="Inf"', 'le="+Inf"'), " ", value)
+      table.insert(output, string.format("%s%s %d\n",
+        self.prefix, key:gsub('le="Inf"', 'le="+Inf"'), value))
     else
       self:log_error("Error getting '", key, "': ", err)
     end
   end
+  ngx.print(output)
 end
 
 return Prometheus

from nginx-lua-prometheus.

jils2013 avatar jils2013 commented on August 30, 2024

Great,~10s(ngx.say+ngx.flush) to ~2s(table),Not seen long-time wait yet .
A little different: an type error for "value" here,so i use "%s%s %s\n",

table.insert(output, string.format("%s%s %d\n",self.prefix, key:gsub('le="Inf"', 'le="+Inf"'), value))

from nginx-lua-prometheus.

knyar avatar knyar commented on August 30, 2024

I am glad it helped. I will merge this into the master branch soon.

an type error for "value" here,so i use "%s%s %s\n",

Good catch, you probably have metric values which are floats rather than integers.

from nginx-lua-prometheus.

jils2013 avatar jils2013 commented on August 30, 2024

many thanks for your help👻

from nginx-lua-prometheus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.