Skip to content

Commit

Permalink
Port the existing functionality from hats to this gem
Browse files Browse the repository at this point in the history
  • Loading branch information
sulami committed Dec 15, 2023
0 parents commit 48210c9
Show file tree
Hide file tree
Showing 20 changed files with 751 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.0.5'

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
AllCops:
TargetRubyVersion: 2.6

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in degica_datadog.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "rubocop", "~> 1.21"
78 changes: 78 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
PATH
remote: .
specs:
degica_datadog (0.1.0)
ddtrace (~> 1.0)
dogstatsd-ruby (~> 5)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
datadog-ci (0.5.1)
msgpack
ddtrace (1.18.0)
datadog-ci (~> 0.5.0)
debase-ruby_core_source (= 3.2.3)
libdatadog (~> 5.0.0.1.0)
libddwaf (~> 1.14.0.0.0)
msgpack
debase-ruby_core_source (3.2.3)
diff-lcs (1.5.0)
dogstatsd-ruby (5.6.1)
ffi (1.16.3)
json (2.7.1)
language_server-protocol (3.17.0.3)
libdatadog (5.0.0.1.0)
libddwaf (1.14.0.0.0-arm64-darwin)
ffi (~> 1.0)
msgpack (1.7.2)
parallel (1.23.0)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
racc (1.7.3)
rainbow (3.1.1)
rake (13.1.0)
regexp_parser (2.8.3)
rexml (3.2.6)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (1.59.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.4)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-22

DEPENDENCIES
degica_datadog!
rake (~> 13.0)
rspec (~> 3.0)
rubocop (~> 1.21)

BUNDLED WITH
2.2.33
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# DegicaDatadog

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/degica_datadog`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'degica_datadog'
```

And then execute:

$ bundle install

Or install it yourself as:

$ gem install degica_datadog

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sulami/degica_datadog.
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "rubocop/rake_task"

RuboCop::RakeTask.new

task default: %i[spec rubocop]
15 changes: 15 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "degica_datadog"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
29 changes: 29 additions & 0 deletions degica_datadog.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative "lib/degica_datadog/version"

Gem::Specification.new do |spec|
spec.name = "degica_datadog"
spec.version = DegicaDatadog::VERSION
spec.authors = ["Robin Schroer"]
spec.email = ["[email protected]"]

spec.summary = "Datadog statsd & tracing utitlities for Degica services"
spec.homepage = "https://github.com/degica/degica_datadog"
spec.required_ruby_version = ">= 2.6.0"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = spec.homepage

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject do |f|
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
end
end
spec.require_paths = ["lib"]

spec.add_dependency "ddtrace", "~> 1.0"
spec.add_dependency "dogstatsd-ruby", "~> 5"
end
10 changes: 10 additions & 0 deletions lib/degica_datadog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require_relative "degica_datadog/config"
require_relative "degica_datadog/statsd"
require_relative "degica_datadog/tracing"
require_relative "degica_datadog/version"

# Standardised interactions with Datadog.
module DegicaDatadog
end
58 changes: 58 additions & 0 deletions lib/degica_datadog/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require "datadog/statsd"
require "json"
require "uri"

module DegicaDatadog
# Configuration for the Datadog agent.
module Config
class << self
def enabled?
%w[production staging].include?(environment) || ENV.fetch("DD_AGENT_URI", nil)
end

def statsd_client
@statsd_client ||= Datadog::Statsd.new(datadog_agent_host, statsd_port)
end

def service
ENV.fetch("SERVICE_NAME", nil)
end

def version
ENV.fetch("_GIT_REVISION", nil)
end

def environment
ENV.fetch("RAILS_ENV", nil)
end

# URI including http:// prefix & port for the tracing endpoint, or nil.
def datadog_agent_uri
return unless enabled?

ecs_meta_file = ENV.fetch("ECS_CONTAINER_METADATA_FILE", nil)
if ecs_meta_file
host_ip = JSON.parse(File.read(ecs_meta_file))&.dig("HostPrivateIPv4Address")
return URI.parse(format("http://%s:9126", host_ip)) if host_ip
end

env_uri = ENV.fetch("DD_AGENT_URI", nil)
URI.parse(env_uri) unless env_uri.nil?
end

def datadog_agent_host
datadog_agent_uri&.host || "localhost"
end

def statsd_port
tracing_port - 1
end

def tracing_port
datadog_agent_uri&.port || 8126
end
end
end
end
Loading

0 comments on commit 48210c9

Please sign in to comment.