---
title: Incorrect counts with Unicorn
source: https://docs.newrelic.com/docs/apm/agents/ruby-agent/troubleshooting/incorrect-data-unicorn
---

## Problem

You're using New Relic Ruby APM and Unicorn together, but the counts for metrics and custom events seem too small.

## Solution

Have Unicorn manually call `NewRelic::Agent.shutdown` to run the agent's exit handlers. Some of Unicorn's exit methods prevent the agent from shutting down as expected. The change below runs New Relic's shutdown method before killing the Unicorn process.

This fix relies on [@expectedbehavior](https://github.com/expectedbehavior)'s fork of [the unicorn-worker-killer gem](https://github.com/expectedbehavior/unicorn-worker-killer).

1.  Add this to your Gemfile using:
    ```ruby
    gem 'unicorn-worker-killer', git: 'https://github.com/expectedbehavior/unicorn-worker-killer'
    ```
    If you're already using `unicorn-worker-killer` in your Gemfile, update the reference to use this fork.

2.  Bundle:
    ```sh
    bundle install
    ```

3.  Update your unicorn configuration to include:

    ```ruby
    require "unicorn/worker_killer"

    ::Unicorn::WorkerKiller.configure do |config|
      config.before_kill do |signal|
        ::NewRelic::Agent.increment_metric("Custom/UnicornWorkerBeforeKillSignal/#{signal}")
        ::NewRelic::Agent.shutdown
      end
    end
    ```

4.  Restart your server
