Sha256: f85261a9189fa29eb95d9ec904cf034996b1118f765853933da8a3347862eb75

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

namespace :ci do
  namespace :prometheus do
    desc 'Set up Prometheus'
    task :setup do
      @metrics = []

      ENV['PROMETHEUS_PUSHGATEWAY'] ||= ask('Prometheus pushgateway (host:port): ')
      ENV['PROMETHEUS_PUSHGATEWAY'] = nil if ENV['PROMETHEUS_PUSHGATEWAY'] == ''

      ENV['PROMETHEUS_PROJECTNAME'] ||= ask('Prometheus project name: ')
      ENV['PROMETHEUS_PROJECTNAME'] = nil if ENV['PROMETHEUS_PROJECTNAME'] == ''
    end

    desc 'Push Prometheus stats'
    task publish: :setup do
      next if @metrics.empty? || ENV['PROMETHEUS_PUSHGATEWAY'].nil?

      require 'prometheus/client'
      require 'prometheus/client/push'

      # returns a default registry
      prometheus = Prometheus::Client.registry

      @metrics.each do |metric|
        name = "ci_#{metric[:name]}".to_sym
        # TODO: Add :docstring where required
        docstring = metric[:docstring] || 'TODO'
        label_set = metric[:label_set] || {}
        value = metric[:value]

        case metric[:type]
        when :gauge
          gauge =
            if prometheus.exist?(name)
              prometheus.get(name)
            else
              prometheus.gauge(name, docstring, project: ENV['PROMETHEUS_PROJECTNAME'])
            end
          gauge.set(label_set, value)
        else
          raise "Unknown metric type (#{metric.inspect})"
        end
      end

      Prometheus::Client::Push.new(
        'rake-ci-job', nil, ENV['PROMETHEUS_PUSHGATEWAY']
      ).add(prometheus)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ndr_dev_support-4.0.0 lib/tasks/ci/prometheus.rake
ndr_dev_support-3.1.3 lib/tasks/ci/prometheus.rake
ndr_dev_support-3.1.2 lib/tasks/ci/prometheus.rake
ndr_dev_support-3.1.1 lib/tasks/ci/prometheus.rake
ndr_dev_support-3.1.0 lib/tasks/ci/prometheus.rake
ndr_dev_support-3.0.0 lib/tasks/ci/prometheus.rake