apm_traceable 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3723420fbeb821a59ef4032f6af31fb5f0ec49faa905c85c2705b37b4c8e15ba
4
+ data.tar.gz: 75e2e16bc448d23e1bfdeb98d52fe114d8027b767452d11b7a85179c3ff3a7cb
5
+ SHA512:
6
+ metadata.gz: 11da2030ca3ac8dbab7950d169762ef461dc0bde0a27d2892d1ef3836b6a7b82e3f90d559dcb8814ae02ae475cce7f7c2c49c5a285ecfc2c6021b6c7880cbd3c
7
+ data.tar.gz: 8202ec5887b20d64d67dbe4120fb4e1f6a3f539346adc9f04a080409e22ac8ea86e810c7a4ffc0cdbab8830dea3ab271310a7694ee690cb5d4a0b561a2955a83
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,29 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.0
7
+ NewCops: enable
8
+ Style/StringLiterals:
9
+ Enabled: true
10
+ EnforcedStyle: single_quotes
11
+
12
+ Style/StringLiteralsInInterpolation:
13
+ Enabled: true
14
+ EnforcedStyle: single_quotes
15
+
16
+ Layout/LineLength:
17
+ Max: 120
18
+
19
+ Metrics/BlockLength:
20
+ Exclude:
21
+ - 'spec/**/*'
22
+ - 'apm_traceable.gemspec'
23
+
24
+ RSpec/MultipleExpectations:
25
+ Enabled: false
26
+ RSpec/ExampleLength:
27
+ Max: 20
28
+ RSpec/NamedSubject:
29
+ Enabled: false
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.2.2
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in apm_traceable.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'rubocop', '~> 1.21'
11
+ gem 'rubocop-rake', '~> 0.6.0'
12
+ gem 'rubocop-rspec', '~> 2.24.0'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 kokuyouwind
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # ApmTraceable
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/apm_traceable.svg)](https://badge.fury.io/rb/apm_traceable)
4
+ [![Build Status](https://github.com/kokuyouwind/apm_traceable/actions/workflows/main.yml/badge.svg)](https://github.com/kokuyouwind/apm_traceable/actions/workflows/main.yml)
5
+
6
+ APM で任意区間のトレースを取得するためのライブラリです。
7
+
8
+ ## Installation
9
+
10
+ この Gem と、利用する APM に対応するアダプタの2つを Gemfile に記述します。
11
+
12
+ ```ruby
13
+ gem 'apm_traceable'
14
+ gem 'apm_traceable-datadog' # Datadogの場合
15
+ ```
16
+
17
+ その後、以下を実行してください。
18
+
19
+ $ bundle install
20
+
21
+ アダプタの設定方法は各 Gem を参照してください。
22
+ 組み込みの `ApmTraceable::Adapters::StdoutAdapter` であれば、以下のように設定します。
23
+
24
+ ```ruby
25
+ ApmTraceable.configure do |config|
26
+ config.adapter = 'stdout'
27
+ end
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ トレースを取得するクラスごとに `ApmTraceable::Tracer` を include します。
33
+
34
+ その後、トレースを取得したいメソッドを `trace_methods` に指定するか、 `trace_span` を利用してブロックを指定します。
35
+
36
+ ```ruby
37
+ class TracerTestClass
38
+ include ApmTraceable::Tracer
39
+
40
+ trace_methods :test_trace_method
41
+ def test_trace_method
42
+ 'test'
43
+ end
44
+
45
+ def test_trace_span
46
+ trace_span('test_name', option1: :value1) do
47
+ 'test'
48
+ end
49
+ end
50
+ end
51
+ ```
52
+
53
+ ## Development
54
+
55
+ 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.
56
+
57
+ 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).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kokuyouwind/apm_traceable.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/apm_traceable/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'apm_traceable'
7
+ spec.version = ApmTraceable::VERSION
8
+ spec.authors = ['kokuyouwind']
9
+ spec.email = ['kokuyouwind@gmail.com']
10
+
11
+ spec.summary = 'APM Traceable'
12
+ spec.description = 'Tracer to facilitate tracking of method calls, etc. in APM'
13
+ spec.homepage = 'https://github.com/kokuyouwind/apm_traceable'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 3.0.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/kokuyouwind/apm-traceable'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/kokuyouwind/apm-traceable'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.metadata['rubygems_mfa_required'] = 'true'
33
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'apm_traceable'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApmTraceable
4
+ module Adapters
5
+ # トレース結果を送信するためのアダプターのベースクラス
6
+ # @abstract
7
+ class BaseAdapter
8
+ def trace(trace_name, context_class:, **_options, &_block)
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'benchmark'
4
+
5
+ module ApmTraceable
6
+ module Adapters
7
+ # 標準出力へトレース結果を送るためのアダプター
8
+ class StdoutAdapter < BaseAdapter
9
+ def trace(trace_name, context_class:, **_options, &block)
10
+ result = nil
11
+ traced_sec = Benchmark.realtime do
12
+ result = block.call
13
+ end
14
+ puts "#{context_class.name}##{trace_name} #{format('%05.6f', traced_sec)}s"
15
+ result
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApmTraceable
4
+ # 設定値を保持するクラス
5
+ class Configuration
6
+ class InsufficientConfigurationError < StandardError; end
7
+
8
+ def adapter
9
+ @adapter || raise(InsufficientConfigurationError, 'adapter is not set')
10
+ end
11
+
12
+ def adapter=(args)
13
+ adapter_name, options = args
14
+ @adapter = ::ApmTraceable::Adapters.const_get("#{adapter_name.to_s.capitalize}Adapter").new(**(options || {}))
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApmTraceable
4
+ # トレース対象クラスにincludeして利用するTracerクラス
5
+ # トレースに利用する以下2メソッドが利用可能になる
6
+ # - trace_span: 指定したブロックをトレースする
7
+ # - trace_methods: 指定したメソッドの呼び出し全体をトレースする
8
+ module Tracer
9
+ def self.included(klass)
10
+ klass.extend(ClassMethods)
11
+ end
12
+
13
+ # include先クラスで利用可能にするクラスメソッド群のモジュール
14
+ module ClassMethods
15
+ # 指定したメソッド群をトレース対象にする.
16
+ # 引数を複数指定すると、すべてのメソッドがそれぞれトレース対象になる.
17
+ #
18
+ # fg.
19
+ # class Test
20
+ # include DatadogTraceable
21
+ # trace_methods :method_a, :method_b
22
+ # end
23
+ def trace_methods(*method_names)
24
+ # 計測対象をラップする必要があるため、計測対象メソッドと同名で計測用メソッドを定義したモジュールを生成する
25
+ wrapper = Module.new do
26
+ method_names.each do |method_name|
27
+ define_method method_name do |*args, **options|
28
+ trace_span(method_name.to_s) { super(*args, **options) }
29
+ end
30
+ end
31
+ end
32
+
33
+ # 計測対象メソッドより先に計測用メソッドが呼び出されないといけないため、
34
+ # prependして継承チェインの先頭側に追加する
35
+ prepend(wrapper)
36
+ end
37
+ end
38
+
39
+ # 指定したブロックをトレース対象にする. Datadog::Tracing#trace のラッパーメソッド.
40
+ # リソース名指定のみ必須で、それ以外に指定したオプションは Datadog::Tracing#trace にそのまま渡される.
41
+ #
42
+ # fg.
43
+ # class Test
44
+ # include DatadogTraceable
45
+ #
46
+ # def test_method
47
+ # trace_span('mySpan') { some_heavy_process }
48
+ # end
49
+ # end
50
+ def trace_span(trace_name, **options, &block)
51
+ ApmTraceable.configuration.adapter.trace(trace_name, context_class: context_class, **options, &block)
52
+ end
53
+
54
+ private
55
+
56
+ def context_class
57
+ # template から呼び出された場合、 self が ActionView::Base のオブジェクトとなるため controller から生成する
58
+ self.class || controller.class
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApmTraceable
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'apm_traceable/adapters/base_adapter'
4
+ require_relative 'apm_traceable/adapters/stdout_adapter'
5
+ require_relative 'apm_traceable/configuration'
6
+ require_relative 'apm_traceable/tracer'
7
+ require_relative 'apm_traceable/version'
8
+
9
+ # ApmTraceable 全体のモジュール
10
+ module ApmTraceable
11
+ class << self
12
+ def configure
13
+ yield(configuration)
14
+ end
15
+
16
+ def configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apm_traceable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - kokuyouwind
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tracer to facilitate tracking of method calls, etc. in APM
14
+ email:
15
+ - kokuyouwind@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - ".tool-versions"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - apm_traceable.gemspec
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/apm_traceable.rb
31
+ - lib/apm_traceable/adapters/base_adapter.rb
32
+ - lib/apm_traceable/adapters/stdout_adapter.rb
33
+ - lib/apm_traceable/configuration.rb
34
+ - lib/apm_traceable/tracer.rb
35
+ - lib/apm_traceable/version.rb
36
+ homepage: https://github.com/kokuyouwind/apm_traceable
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/kokuyouwind/apm_traceable
41
+ source_code_uri: https://github.com/kokuyouwind/apm-traceable
42
+ changelog_uri: https://github.com/kokuyouwind/apm-traceable
43
+ rubygems_mfa_required: 'true'
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.0.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.10
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: APM Traceable
63
+ test_files: []