Sha256: c617b3e5e32625ff0b4f6bfd39ce2c48305ad8c0ff82abfbb877aaa3097ed666

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

require "rspec/datadog/version"
require 'rspec/core'
require 'rspec/core/formatters'
require 'rspec/core/formatters/json_formatter'
require 'active_support'

require 'dogapi'

class DatadogFormatter < RSpec::Core::Formatters::JsonFormatter
  BASE_TAGS = [] # Override this to add custom base tags - name of the system under test, for example

  KEYS_TO_EMIT = [:full_description, :status, :file_path, :line_number].freeze

  def initialize(output)
    super

    # Initialize Dogapi client
    @dog = Dogapi::Client.new(ENV['DATADOG_API_KEY'], ENV['DATADOG_APPLICATION_KEY'])
  end

  def close
    # Set now to a value shared by all events for this test run
    now = Time.now.to_i

    datadog_events = @output_hash[:examples].map do |example|
      tags = hash_to_tags(
        example.slice(*KEYS_TO_EMIT)
      )

      Dogapi::Event.new("#{example[:full_description]}: #{example[:status]}",
        :msg_title => "RSpec example",
        :tags => BASE_TAGS + tags,
        :date_happened => now
      )
    end

    datadog_events.each do |event|
      emit_event(event)
    end
  end

  private
  def hash_to_tags(example_hash)
    example_hash.map do |k,v|
      "#{k}:#{v}"
    end
  end

  def emit_event(event)
    if ENV['DEBUG']
      puts JSON.pretty_generate(event.to_hash)
    else
      @dog.emit_event(event)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rspec-datadog-0.2.0 lib/rspec/datadog.rb
rspec-datadog-0.1.2 lib/rspec/datadog.rb
rspec-datadog-0.1.1 lib/rspec/datadog.rb