boringmetrics 0.1.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: 1fbcee196ad899188c5c5984ee65ef840750920322dc7d764c622ebd2312b44f
4
+ data.tar.gz: 30924063fb4692030c525a011d46a12710d2be07889ef3fc327ab6e614b74c78
5
+ SHA512:
6
+ metadata.gz: 4c041440af73eda67c140596f290668ea282eefd337a79ac0517c79c06e0a2589601d861fb709cef45d366faed9dfb1bd7d28ee483c7a07c0b3bc22057394c15
7
+ data.tar.gz: dcb9b561a76a52bc9371af098988438ba9a971ae60229598d96771d0d493e0f3a15a74ee57c3225b8fcb39f78d7bb0968223b34e3b9445e063e5b740df68e45d
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2025 Boring Metrics
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19
+ OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # Boring Metrics Ruby SDK
2
+
3
+ This is a Ruby SDK for the Boring Metrics API. It provides a simple and efficient way to interact with the API from your Ruby applications.
4
+
5
+ ## Supported Platforms
6
+
7
+ The SDK is available for the following platforms:
8
+
9
+ - [`boringmetrics`](https://github.com/boringmetrics/ruby-sdk/tree/main/packages/boringmetrics): SDK for Ruby
10
+ - [`boringmetrics-rails`](https://github.com/boringmetrics/ruby-sdk/tree/main/packages/boringmetrics-rails): SDK for Ruby on Rails
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ # For plain Ruby applications
18
+ gem 'boringmetrics'
19
+
20
+ # For Rails applications
21
+ gem 'boringmetrics-rails'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```bash
27
+ $ bundle install
28
+ ```
29
+
30
+ Or install it yourself as:
31
+
32
+ ```bash
33
+ $ gem install boringmetrics
34
+ $ gem install boringmetrics-rails
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ### Ruby
40
+
41
+ ```ruby
42
+ # Initialize the SDK
43
+ BoringMetrics.initialize("YOUR_API_TOKEN")
44
+
45
+ # Send a log
46
+ BoringMetrics.logs.send(
47
+ type: "log",
48
+ level: "info",
49
+ message: "User signed in",
50
+ data: { user_id: "123" },
51
+ )
52
+
53
+ # Send multiple logs
54
+ BoringMetrics.logs.send_batch([
55
+ { type: "log", level: "warn", message: "Something looks weird" },
56
+ { type: "log", level: "error", message: "Something broke!", data: { error: "Connection timeout" } }
57
+ ])
58
+
59
+ # Set a live metric value
60
+ BoringMetrics.lives.update(
61
+ live_id: "metric-123",
62
+ value: 42,
63
+ operation: "set",
64
+ )
65
+
66
+ # Increment a live metric value
67
+ BoringMetrics.lives.update(
68
+ live_id: "metric-123",
69
+ value: 5,
70
+ operation: "increment",
71
+ )
72
+ ```
73
+
74
+ ### Rails
75
+
76
+ In a Rails application, you can initialize the SDK in an initializer:
77
+
78
+ ```ruby
79
+ # config/initializers/boringmetrics.rb
80
+ BoringMetrics::Rails.initialize("YOUR_API_TOKEN", {
81
+ logs_max_batch_size: 50,
82
+ logs_send_interval: 10
83
+ })
84
+ ```
85
+
86
+ The Rails integration automatically captures exceptions and logs them to BoringMetrics.
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/boringmetrics/ruby-sdk.
91
+
92
+ ## Contributors
93
+
94
+ Thanks to everyone who contributed to the Boring Metrics Ruby SDK!
95
+
96
+ <a href="https://github.com/boringmetrics/ruby-sdk/graphs/contributors">
97
+ <img src="https://contributors-img.web.app/image?repo=boringmetrics/ruby-sdk" />
98
+ </a>
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent"
4
+ require "time"
5
+
6
+ module BoringMetrics
7
+ # Main client for the BoringMetrics SDK
8
+ class Client
9
+ attr_reader :logs, :lives
10
+
11
+ # Initialize a new client
12
+ #
13
+ # @param token [String] Your BoringMetrics API token
14
+ # @param config [Hash] Optional configuration options
15
+ def initialize(token, **config)
16
+ @config = Configuration.new(token, **config)
17
+ @transport = Transport.new(@config)
18
+
19
+ @logs_queue = Concurrent::Array.new
20
+ @logs_mutex = Mutex.new
21
+ @logs_timer = nil
22
+
23
+ @lives_queue = Concurrent::Array.new
24
+ @lives_mutex = Mutex.new
25
+ @lives_timer = nil
26
+
27
+ @logs = LogMethods.new(self)
28
+ @lives = LiveMethods.new(self)
29
+ end
30
+
31
+ # Add a log to the queue
32
+ #
33
+ # @param log [Hash] The log to add
34
+ # @return [void]
35
+ def add_log(log)
36
+ log_with_sent_at = log.dup
37
+ log_with_sent_at[:sent_at] ||= Time.now.iso8601
38
+
39
+ @logs_mutex.synchronize do
40
+ @logs_queue << log_with_sent_at
41
+
42
+ if @logs_queue.size >= @config.logs_max_batch_size
43
+ flush_logs
44
+ elsif @logs_timer.nil?
45
+ schedule_logs_flush
46
+ end
47
+ end
48
+ end
49
+
50
+ # Update a live metric
51
+ #
52
+ # @param update [Hash] The live update
53
+ # @return [void]
54
+ def update_live(update)
55
+ update_with_sent_at = update.dup
56
+ update_with_sent_at[:sent_at] ||= Time.now.iso8601
57
+
58
+ @lives_mutex.synchronize do
59
+ @lives_queue << update_with_sent_at
60
+
61
+ if @lives_queue.size >= @config.lives_max_batch_size
62
+ flush_lives
63
+ elsif @lives_timer.nil?
64
+ schedule_lives_flush
65
+ end
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def schedule_logs_flush
72
+ @logs_timer = Concurrent::ScheduledTask.execute(@config.logs_send_interval) do
73
+ flush_logs
74
+ end
75
+ end
76
+
77
+ def flush_logs
78
+ @logs_mutex.synchronize do
79
+ @logs_timer&.cancel
80
+ @logs_timer = nil
81
+
82
+ return if @logs_queue.empty?
83
+
84
+ logs_to_send = @logs_queue.dup
85
+ @logs_queue.clear
86
+
87
+ send_logs(logs_to_send)
88
+ end
89
+ end
90
+
91
+ def send_logs(logs)
92
+ Thread.new do
93
+ begin
94
+ @transport.send_logs(logs)
95
+ rescue StandardError => e
96
+ puts "[BoringMetrics] Error sending logs: #{e.message}"
97
+ end
98
+ end
99
+ end
100
+
101
+ def schedule_lives_flush
102
+ @lives_timer = Concurrent::ScheduledTask.execute(@config.lives_debounce_time) do
103
+ flush_lives
104
+ end
105
+ end
106
+
107
+ def flush_lives
108
+ @lives_mutex.synchronize do
109
+ @lives_timer&.cancel
110
+ @lives_timer = nil
111
+
112
+ return if @lives_queue.empty?
113
+
114
+ lives_to_send = @lives_queue.dup
115
+ @lives_queue.clear
116
+
117
+ send_lives(lives_to_send)
118
+ end
119
+ end
120
+
121
+ def send_lives(lives)
122
+ Thread.new do
123
+ begin
124
+ lives.each do |live|
125
+ @transport.update_live(live)
126
+ end
127
+ rescue StandardError => e
128
+ puts "[BoringMetrics] Error sending live updates: #{e.message}"
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoringMetrics
4
+ class Configuration
5
+ attr_accessor :api_url, :max_retry_attempts, :logs_max_batch_size, :logs_send_interval
6
+
7
+ def initialize
8
+ @api_url = "https://api.getboringmetrics.com"
9
+ @max_retry_attempts = 5
10
+ @logs_max_batch_size = 100
11
+ @logs_send_interval = 5 # seconds
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoringMetrics
4
+ # Methods for updating live metrics
5
+ class LiveMethods
6
+ # Initialize the lives API
7
+ #
8
+ # @param client [BoringMetrics::Client] The client instance
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ # Update a live metric value
14
+ #
15
+ # @param update [Hash] The live update to send
16
+ # @option update [String] :live_id The ID of the live metric
17
+ # @option update [Numeric] :value The value to set or increment
18
+ # @option update [String] :operation The operation to perform ("set" or "increment")
19
+ # @option update [String] :sent_at ISO8601 date - will be automatically set if not provided
20
+ # @return [void]
21
+ def update(update)
22
+ @client.update_live(update)
23
+ end
24
+
25
+ # Update multiple live metrics values in a batch
26
+ #
27
+ # @param updates [Array<Hash>] Array of live updates to send
28
+ # @return [void]
29
+ def update_batch(updates)
30
+ updates.each { |update| update(update) }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoringMetrics
4
+ # Methods for sending logs
5
+ class LogMethods
6
+ # Initialize the logs API
7
+ #
8
+ # @param client [BoringMetrics::Client] The client instance
9
+ def initialize(client)
10
+ @client = client
11
+ end
12
+
13
+ # Send a single log event
14
+ #
15
+ # @param log [Hash] The log event to send
16
+ # @option log [String] :type The type of log (default: "log")
17
+ # @option log [String] :level The log level (trace, debug, info, warn, error, fatal)
18
+ # @option log [String] :message The log message
19
+ # @option log [Hash] :data Additional structured data (optional)
20
+ # @option log [String] :session_id Session identifier for grouping related logs (optional)
21
+ # @option log [String] :sent_at ISO8601 date - will be automatically set if not provided
22
+ # @return [void]
23
+ def send(log)
24
+ @client.add_log(log)
25
+ end
26
+
27
+ # Send multiple log events in a batch
28
+ #
29
+ # @param logs [Array<Hash>] Array of log events to send
30
+ # @return [void]
31
+ def send_batch(logs)
32
+ logs.each { |log| send(log) }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+
6
+ module BoringMetrics
7
+ # Transport layer for the BoringMetrics SDK
8
+ class Transport
9
+ # Initialize a new transport
10
+ #
11
+ # @param config [BoringMetrics::Configuration] The SDK configuration
12
+ def initialize(config)
13
+ @config = config
14
+ end
15
+
16
+ # Send logs to the BoringMetrics API
17
+ #
18
+ # @param logs [Array<Hash>] The logs to send
19
+ # @return [void]
20
+ def send_logs(logs)
21
+ with_retry do
22
+ response = connection.post("/api/v1/logs") do |req|
23
+ req.headers["Content-Type"] = "application/json"
24
+ req.headers["Authorization"] = "Bearer #{@config.token}"
25
+ req.body = { logs: logs }.to_json
26
+ end
27
+
28
+ raise "Failed to send logs: #{response.status}" unless response.success?
29
+ end
30
+ end
31
+
32
+ # Update a live metric
33
+ #
34
+ # @param update [Hash] The live update to send
35
+ # @return [void]
36
+ def update_live(update)
37
+ with_retry do
38
+ response = connection.put("/api/v1/lives/#{update[:live_id]}") do |req|
39
+ req.headers["Content-Type"] = "application/json"
40
+ req.headers["Authorization"] = "Bearer #{@config.token}"
41
+ req.body = { live: update }.to_json
42
+ end
43
+
44
+ raise "Failed to send live update: #{response.status}" unless response.success?
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def connection
51
+ @connection ||= Faraday.new(url: Configuration::API_URL) do |faraday|
52
+ faraday.adapter Faraday.default_adapter
53
+ end
54
+ end
55
+
56
+ def with_retry
57
+ retries = 0
58
+ begin
59
+ yield
60
+ rescue StandardError => e
61
+ retries += 1
62
+ if retries <= @config.max_retry_attempts
63
+ sleep(2**retries * 0.1) # Exponential backoff
64
+ retry
65
+ else
66
+ raise e
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BoringMetrics
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "boringmetrics/version"
4
+ require_relative "boringmetrics/client"
5
+ require_relative "boringmetrics/errors"
6
+
7
+ # Main module for the Boring Metrics SDK
8
+ module BoringMetrics
9
+ class << self
10
+ # @return [BoringMetrics::Client] The configured client instance
11
+ attr_reader :instance
12
+
13
+ # Initialize the SDK with your API token
14
+ # @param token [String] Your Boring Metrics API token
15
+ # @return [BoringMetrics::Client] The configured client instance
16
+ def initialize(token)
17
+ @instance ||= Client.new(token)
18
+ end
19
+
20
+ # Access logs functionality
21
+ # @return [BoringMetrics::Client::LogMethods]
22
+ def logs
23
+ instance.logs
24
+ end
25
+
26
+ # Access lives functionality
27
+ # @return [BoringMetrics::Client::LiveMethods]
28
+ def lives
29
+ instance.lives
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boringmetrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aymeric Chauvin
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-05-15 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: concurrent-ruby
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.2'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.2'
26
+ - !ruby/object:Gem::Dependency
27
+ name: faraday
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.7'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.7'
40
+ - !ruby/object:Gem::Dependency
41
+ name: bundler
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rake
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '13.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '13.0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rubocop
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.21'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.21'
82
+ description: Ruby SDK for the Boring Metrics API. It provides a simple and efficient
83
+ way to interact with the API from your Ruby applications.
84
+ email:
85
+ - contact@halftheopposite.dev
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - lib/boringmetrics.rb
93
+ - lib/boringmetrics/client.rb
94
+ - lib/boringmetrics/configuration.rb
95
+ - lib/boringmetrics/live_methods.rb
96
+ - lib/boringmetrics/log_methods.rb
97
+ - lib/boringmetrics/transport.rb
98
+ - lib/boringmetrics/version.rb
99
+ homepage: https://github.com/boringmetrics/ruby-sdk
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ homepage_uri: https://github.com/boringmetrics/ruby-sdk
104
+ source_code_uri: https://github.com/boringmetrics/ruby-sdk
105
+ changelog_uri: https://github.com/boringmetrics/ruby-sdk/blob/main/CHANGELOG.md
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.6.0
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 3.6.2
121
+ specification_version: 4
122
+ summary: Ruby SDK for Boring Metrics
123
+ test_files: []