<commit_before># frozen_string_literal: true

module GraphQL
  module Tracing
    class PrometheusTracing < PlatformTracing
      class GraphQLCollector < ::PrometheusExporter::Server::TypeCollector
        def initialize
          @graphql_gauge = PrometheusExporter::Metric::Summary.new(
            'graphql_duration_seconds',
            'Time spent in GraphQL operations, in seconds'
          )
        end

        def type
          'graphql'
        end

        def collect(object)
          labels = { key: object['key'], platform_key: object['platform_key'] }
          @graphql_gauge.observe object['duration'], labels
        end

        def metrics
          [@graphql_gauge]
        end
      end
    end
  end
end
<commit_msg>GraphQLCollector support for prometheus' custom_labels<commit_after># frozen_string_literal: true

module GraphQL
  module Tracing
    class PrometheusTracing < PlatformTracing
      class GraphQLCollector < ::PrometheusExporter::Server::TypeCollector
        def initialize
          @graphql_gauge = PrometheusExporter::Metric::Summary.new(
            'graphql_duration_seconds',
            'Time spent in GraphQL operations, in seconds'
          )
        end

        def type
          'graphql'
        end

        def collect(object)
          default_labels = { key: object['key'], platform_key: object['platform_key'] }
          custom = object['custom_labels']
          labels = custom.nil? ? default_labels : default_labels.merge(custom)

          @graphql_gauge.observe object['duration'], labels
        end

        def metrics
          [@graphql_gauge]
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.07590361445783132, 'fraction_numerical': 0.0, 'mean_word_length': 2.282608695652174, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5525054', 'n_tokens_mistral': 457, 'n_tokens_neox': 434, 'n_words': 108}