Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kokuyouwind committed Oct 16, 2023
0 parents commit 478975b
Show file tree
Hide file tree
Showing 18 changed files with 336 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.1.0'

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
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
Gemfile.lock
Gemfile-*.lock
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
29 changes: 29 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require:
- rubocop-rake
- rubocop-rspec

AllCops:
TargetRubyVersion: 3.0
NewCops: enable
Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: single_quotes

Layout/LineLength:
Max: 120

Metrics/BlockLength:
Exclude:
- 'spec/**/*'
- 'apm_traceable.gemspec'

RSpec/MultipleExpectations:
Enabled: false
RSpec/ExampleLength:
Max: 20
RSpec/NamedSubject:
Enabled: false
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.2.2
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 apm_traceable-datadog.gemspec
gemspec

gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.21'
gem 'rubocop-rake', '~> 0.6.0'
gem 'rubocop-rspec', '~> 2.24.0'
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 kokuyouwind

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ApmTraceable::Datadog

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/apm_traceable/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 'apm_traceable-datadog'
```

And then execute:

$ bundle install

Or install it yourself as:

$ gem install apm_traceable-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/[USERNAME]/apm_traceable-datadog.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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]
36 changes: 36 additions & 0 deletions apm_traceable-datadog.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require_relative 'lib/apm_traceable/datadog/version'

Gem::Specification.new do |spec|
spec.name = 'apm_traceable-datadog'
spec.version = ApmTraceable::Datadog::VERSION
spec.authors = ['kokuyouwind']
spec.email = ['[email protected]']

spec.summary = 'APM Traceable gem Plugin for Datadog'
spec.description = 'APM Traceable gem Plugin for Datadog'
spec.homepage = 'https://github.com/kokuyouwind/apm_traceable-datadog'
spec.license = 'MIT'
spec.required_ruby_version = '>= 3.0.0'

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/kokuyouwind/apm_traceable-datadog'
spec.metadata['changelog_uri'] = 'https://github.com/kokuyouwind/apm_traceable-datadog'

# 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.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'activesupport', '>= 7.0.0'
spec.add_dependency 'apm_traceable', '>= 1.0.0'
spec.add_dependency 'ddtrace', '~> 1.14.0'
spec.metadata['rubygems_mfa_required'] = 'true'
end
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 'apm_traceable/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
34 changes: 34 additions & 0 deletions lib/apm_traceable/adapters/datadog_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require 'active_support/inflector'
require 'datadog/tracing'

module ApmTraceable
module Adapters
# Datadogへトレース結果を送るためのアダプター
class DatadogAdapter
def initialize(service_name:)
super()

@service_name = service_name
end

def trace(trace_name, context_class:, **options, &block)
::Datadog::Tracing.trace(
trace_name,
**options.merge(service: service_name, resource: resource_name(context_class)),
&block
)
end

private

attr_reader :service_name

def resource_name(context_class)
# include 先のクラス名を利用して `admin.users_controller` のような文字列を作る
context_class.name.underscore&.tr('/', '.')
end
end
end
end
12 changes: 12 additions & 0 deletions lib/apm_traceable/datadog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'apm_traceable'

require_relative 'adapters/datadog_adapter'
require_relative 'datadog/version'

module ApmTraceable
# ApmTraceable::Datadog 全体のモジュール
module Datadog
end
end
7 changes: 7 additions & 0 deletions lib/apm_traceable/datadog/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module ApmTraceable
module Datadog
VERSION = '0.1.0'
end
end
45 changes: 45 additions & 0 deletions spec/apm_traceable/adapters/datadog_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class TracerTestClass
include ApmTraceable::Tracer

trace_methods :test_trace_method
def test_trace_method
'test'
end
end

RSpec.describe ApmTraceable::Adapters::DatadogAdapter do
let(:test_object) { TracerTestClass.new }

before do
ApmTraceable.configure do |config|
config.adapter = 'datadog', { service_name: 'test_service' }
end
end

describe '#trace_methods' do
it '適切な引数で Datadog::Tracing.trace を呼び出す' do
allow(Datadog::Tracing).to receive(:trace) { |_, &block| block.call }
expect(test_object.test_trace_method).to eq 'test'
expect(Datadog::Tracing).to have_received(:trace).with(
'test_trace_method',
service: 'test_service',
resource: 'tracer_test_class'
)
end
end

describe '#trace_span' do
it '適切な引数で Datadog::Tracing.trace を呼び出す' do
allow(Datadog::Tracing).to receive(:trace) { |_, &block| block.call }
expect(test_object.trace_span('test_name', option1: :value1) { 'test' }).to eq 'test'
expect(Datadog::Tracing).to have_received(:trace).with(
'test_name',
service: 'test_service',
resource: 'tracer_test_class',
option1: :value1
)
end
end
end
7 changes: 7 additions & 0 deletions spec/apm_traceable/datadog_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

RSpec.describe ApmTraceable::Datadog do
it 'has a version number' do
expect(ApmTraceable::Datadog::VERSION).not_to be_nil
end
end
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'apm_traceable/datadog'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

config.expect_with :rspec do |c|
c.syntax = :expect
end
end

0 comments on commit 478975b

Please sign in to comment.