Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider a PR for .current similar to Redis #25

Open
harlow opened this issue Dec 14, 2015 · 8 comments
Open

Consider a PR for .current similar to Redis #25

harlow opened this issue Dec 14, 2015 · 8 comments

Comments

@harlow
Copy link

harlow commented Dec 14, 2015

We have a number of tags that we instantiate Statsd with. With Redis.current we do something similar:

# config/initializers/redis.rb
config = {
  # ...
}
Redis.current = Redis.new(config)

https://github.com/redis/redis-rb/blob/b9ac235fa03de173d1d382878757850afae94bc3/lib/redis.rb#L19-L25

Would DataDog team consider a PR that does same for Statsd?

# config/initializers/redis.rb
tags: {
  # env, service name, revision, etc
}
Statsd.current = Statsd.new(url, port, tags)
@yannmh
Copy link
Member

yannmh commented Jan 8, 2016

Thank @harlow

Could you give us more details about the use cases please ? I am not sure to understand how it would help you.

-- Yann

@yannmh yannmh added this to the Triage milestone Jan 8, 2016
@harlow
Copy link
Author

harlow commented Jan 9, 2016

hi @yannmh we use Statsd throughout our codebase so it would be nice to be able to reference an initialized version of it.

# config/initializers/statsd.rb
tags: {
  # env, service name, revision, etc
}
Statsd.current = Statsd.new(url, port, tags)
# app/models/foo.rb
class Foo
  def bar
    if some_success_case?
      statsd.increment('xyz')
    end
  end

  private

  def statsd
    Statsd.current
  end
end

This was we have access to the configured Statsd.new that was initialized in config.

@yannmh
Copy link
Member

yannmh commented Jan 11, 2016

Seems like a nice feature, thanks for the further explanations @harlow !
We'll certainly consider it for an upcoming release.

@sj26
Copy link
Contributor

sj26 commented Dec 20, 2017

@harlow the easiest way we've found to do this is set a global variable for use across your application:

# config/initializers/statsd.rb
$statsd = Datadog::Statsd.new(...)
# app/models/foo.rb
class Foo
  def bar
    if some_success_case?
      $statsd.increment('xyz')
    end
  end
end

@masci masci removed this from the Next milestone Mar 21, 2019
@aharpervc
Copy link

Is using a global variable compatible with the v5 requirement of needing to call close? Or, put another way, how could you do the global variable approach in v5?

I'm trying to upgrade an app that used a similar pattern, but I want to use buffer_flush_interval and not send stats live for each invocation of increment (etc).

Thoughts?

@ivoanjo
Copy link
Member

ivoanjo commented Oct 3, 2022

You could perhaps use Kernel#at_exit to ask Ruby to call it automatically on shutdown, although there may be a better way depending on your application.

@aharpervc
Copy link

You could perhaps use Kernel#at_exit to ask Ruby to call it automatically on shutdown, although there may be a better way depending on your application.

Yep, that's more or less what I figured out over here (then ran into a different issue): #265 (comment)

@ivoanjo
Copy link
Member

ivoanjo commented Oct 3, 2022

Ah, I totally missed the other ticket 😰

pudiva pushed a commit to pudiva/dogstatsd-ruby that referenced this issue Jun 5, 2023
…-update

feat(epic.md): Soften people plan to convey section purpose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants