<commit_before># frozen_string_literal: true

require 'influxdb'

module Server
  module Pipeline
    module Metrics
      class Influx
        def initialize(database, **kwargs)
          if database.respond_to?(:write_point)
            @client = database
          else
            @client = ::InfluxDB::Client.new(database.to_s, **kwargs)
          end
        end

        def name
          ::Server::Pipeline::Metrics::Influx
        end

        # rubocop:disable AbcSize
        # rubocop:disable MethodLength
        def call(payload)
          values = {
            total: ::Server::Clock
              .to_seconds(payload[:stop] - payload[:start]).to_f
          }

          tags = {
            platform: payload[:platform],
            hostname: payload[:hostname],
            application: payload[:application],
          }

          case payload[:name]
            when 'app.web.request.rack'
              type = 'web'

              tags[:action] = payload.dig(:meta, :action)
              tags[:format] = payload.dig(:meta, :format)
              tags[:method] = payload.dig(:meta, :method)
              tags[:status] = payload.dig(:meta, :status)
              tags[:controller] = payload.dig(:meta, :controller)

            when 'app.job.perform.sidekiq'
              type = 'background'

              tags[:type] = 'job'
              tags[:queue] = payload.dig(:meta, :queue)
              tags[:worker] = payload.dig(:meta, :worker)

            when 'app.messaging.receive.msgr'
              type = 'messaging'

              tags[:type] = 'message'
              tags[:consumer] = payload.dig(:meta, :route, :consumer)
              tags[:action] = payload.dig(:meta, :route, :action)
              tags[:route] = payload.dig(:meta, :delivery_info, :routing_key)
          end

          yield(payload)

          return unless type

          data = {
            tags: tags,
            values: values,
            timestamp: payload[:stop]
          }

          @client.write_point(type, data, 'ns')
        end
      end
    end
  end
end
<commit_msg>Add instance tag to metrics<commit_after># frozen_string_literal: true

require 'influxdb'

module Server
  module Pipeline
    module Metrics
      class Influx
        def initialize(database, **kwargs)
          if database.respond_to?(:write_point)
            @client = database
          else
            @client = ::InfluxDB::Client.new(database.to_s, **kwargs)
          end
        end

        def name
          ::Server::Pipeline::Metrics::Influx
        end

        # rubocop:disable AbcSize
        # rubocop:disable MethodLength
        def call(payload)
          values = {
            total: ::Server::Clock
              .to_seconds(payload[:stop] - payload[:start]).to_f
          }

          tags = {
            platform: payload[:platform],
            hostname: payload[:hostname],
            application: payload[:application]
          }

          case payload[:name]
            when 'app.web.request.rack'
              type = 'web'

              tags[:format] = payload.dig(:meta, :format)
              tags[:method] = payload.dig(:meta, :method)
              tags[:status] = payload.dig(:meta, :status)

              action = payload.dig(:meta, :action)
              controller = payload.dig(:meta, :controller)

              if action && controller
                tags[:action] = action
                tags[:controller] = controller

                tags[:instance] = "#{controller}##{action}"
              end

            when 'app.job.perform.sidekiq'
              type = 'background'

              tags[:type] = 'job'
              tags[:queue] = payload.dig(:meta, :queue)
              tags[:worker] = payload.dig(:meta, :worker)

            when 'app.messaging.receive.msgr'
              type = 'messaging'

              tags[:type] = 'message'
              tags[:consumer] = payload.dig(:meta, :route, :consumer)
              tags[:action] = payload.dig(:meta, :route, :action)
              tags[:route] = payload.dig(:meta, :delivery_info, :routing_key)

              tags[:instance] = "#{tags[:consumer]}##{tags[:action]}"
          end

          yield(payload)

          return unless type

          data = {
            tags: tags,
            values: values,
            timestamp: payload[:stop]
          }

          @client.write_point(type, data, 'ns')
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.119533527696793, 'fraction_numerical': 0.0, 'mean_word_length': 1.6143024618991795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2122205', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1172, 'n_words': 254}