代码之家  ›  专栏  ›  技术社区  ›  Schwern

我怎样才能告诉哨兵不要提醒某些例外情况?

  •  1
  • Schwern  · 技术社区  · 6 年前

    我有一个Rails5应用程序使用 raven-ruby 向哨兵发送异常,然后哨兵向我们的懒汉发送警报。

    Raven.configure do |config|
      config.dsn = ENV['SENTRY_DSN']
      config.environments = %w[ production development ]
      config.excluded_exceptions += []
      config.async = lambda { |event|
        SentryWorker.perform_async(event.to_hash)
      }
    end
    
    class SentryWorker < ApplicationWorker
      sidekiq_options queue: :default
    
      def perform(event)
        Raven.send_event(event)
      end
    end
    

    我已经准备好了 added the retry_count to the jobs . 如何防止Sentry发送重试计数异常<N到Slack,同时仍然提醒其他异常?不应发出警报的示例将具有如下额外上下文:

    sidekiq: {
      context: Job raised exception,
      job: {
        args: [{...}],
        class: SomeWorker,
        created_at: 1540590745.3296254,
        enqueued_at: 1540607026.4979043,
        error_class: HTTP::TimeoutError,
        error_message: Timed out after using the allocated 13 seconds,
        failed_at: 1540590758.4266324,
        jid: b4c7a68c45b7aebcf7c2f577,
        queue: default,
        retried_at: 1540600397.5804272,
        retry: True,
        retry_count: 2
      },
    }
    

    不把他们送到哨兵那里和把他们送到哨兵那里却不被提醒有什么利弊?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Markus Unterwaditzer    6 年前

    class SentryWorker < ApplicationWorker
      sidekiq_options queue: :default
    
      def perform(event)
        retry_count = event.dig(:extra, :sidekiq, :job, retry_count)
        if retry_count.nil? || retry_count > N
          Raven.send_event(event)
        end
      end
    end
    

    另一个想法是根据是否重试设置不同的指纹。这样地:

    class MyJobProcessor < Raven::Processor
      def process(data)
        retry_count = event.dig(:extra, :sidekiq, :job, retry_count)
        if (retry_count || 0) < N
          data["fingerprint"] = ["will-retry-again", "{{default}}"]
        end
      end
    end
    

    看到了吗 https://docs.sentry.io/learn/rollups/?platform=javascript#custom-grouping

    我没有测试这个,但是这个应该把你的问题分成两部分,这取决于sidekiq是否会重试它们。然后可以忽略一个组,但仍然可以在需要数据时查看它。

        2
  •  8
  •   Katherine    4 年前

    摘要

    一个对我很有效的方法是配置 Sentry's should_capture 在Sidekiq的旁边 sidekiq_retries_exhausted 在异常上具有自定义属性。

    细节

    可以向异常添加自定义属性。您可以在任何错误类上使用 attr_accessor :

    class SomeError
      attr_accessor :ignore
    
      alias ignore? ignore
    end
    

    第1b条。挽救错误,设置自定义属性,&重新升起

    def perform
      # do something
    rescue SomeError => e
      e.ignore = true
      raise e
    end
    
    1. 你应该捕获吗

    允许您在异常满足定义的条件时捕获它们。将异常传递给它,您可以在其上访问自定义属性。

    config.should_capture { |e| !e.ignore? }

    1. 重试结束时翻转自定义属性

    根据使用的Sidekiq版本的不同,有两种方法可以定义作业终止时希望发生的行为。如果您想在全球范围内申请;有了sidekiqv5.1+,就可以使用 death handler . 如果要应用于特定的辅助进程或其版本低于v5.1,可以使用 sidekiq\u重试\u用尽

    sidekiq_retries_exhausted { |_job, ex| ex.ignore = false }

        3
  •  0
  •   theterminalguy    4 年前

    如果您试图忽略属于某个类的异常,一种更简洁的方法是将它们添加到配置文件中

    config.excluded_exceptions += ['ActionController::RoutingError', 'ActiveRecord::RecordNotFound']
    

    在上面的例子中,Rails用来生成404响应的异常将被抑制。

    See the docs for more configuration options

    推荐文章