Sha256: cf04ca7e768b0f78097b51b00f054c788b8cffe9d24a221efaa1de806d2b6936

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module PrometheusExporter::Server
  class WebCollector < TypeCollector
    def initialize
      @metrics = {}
    end

    def type
      "web"
    end

    def collect(obj)
      ensure_metrics
      observe(obj)
    end

    def metrics
      @metrics.values
    end

    protected

    def ensure_metrics
      unless @http_requests
        @metrics["http_requests"] = @http_requests = PrometheusExporter::Metric::Counter.new(
          "http_requests",
          "Total HTTP requests from web app"
        )

        @metrics["http_duration_seconds"] = @http_duration_seconds = PrometheusExporter::Metric::Summary.new(
          "http_duration_seconds",
          "Time spent in HTTP reqs in seconds"
        )

        @metrics["http_redis_duration_seconds"] = @http_redis_duration_seconds = PrometheusExporter::Metric::Summary.new(
          "http_redis_duration_seconds",
          "Time spent in HTTP reqs in redis seconds"
        )

        @metrics["http_sql_duration_seconds"] = @http_sql_duration_seconds = PrometheusExporter::Metric::Summary.new(
          "http_sql_duration_seconds",
          "Time spent in HTTP reqs in SQL in seconds"
        )
      end
    end

    def observe(obj)

      labels = {
        controller: obj["controller"] || "other",
        action: obj["action"] || "other"
      }

      @http_requests.observe(1, labels.merge(status: obj["status"]))

      if timings = obj["timings"]
        @http_duration_seconds.observe(timings["total_duration"], labels)
        if redis = timings["redis"]
          @http_redis_duration_seconds.observe(redis["duration"], labels)
        end
        if sql = timings["sql"]
          @http_sql_duration_seconds.observe(sql["duration"], labels)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
prometheus_exporter-0.1.16 lib/prometheus_exporter/server/web_collector.rb
prometheus_exporter-0.1.15 lib/prometheus_exporter/server/web_collector.rb
prometheus_exporter-0.1.14 lib/prometheus_exporter/server/web_collector.rb
prometheus_exporter-0.1.13 lib/prometheus_exporter/server/web_collector.rb
prometheus_exporter-0.1.12 lib/prometheus_exporter/server/web_collector.rb
prometheus_exporter-0.1.11 lib/prometheus_exporter/server/web_collector.rb
prometheus_exporter-0.1.10 lib/prometheus_exporter/server/web_collector.rb