vigilant-ruby 0.0.5 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0280598c4039160580005f39571754a95907101b616c6d3c8354da14eefb5de
4
- data.tar.gz: 9ddebfc7149b3950006c8b45c0471955b00af3f99b2b88ffbf8754ed20d2187f
3
+ metadata.gz: d1922622d6ddc6f7e1630e48672cd6d78c8d988436319af621e8c9a6e688dbd6
4
+ data.tar.gz: b73e50dca1371a51a5e7f83d1016e0b16b1c1d9ff43043bc280a418edd3ff3f9
5
5
  SHA512:
6
- metadata.gz: 7d97948a8d9aef93514e6caa314caa4a502a721d21bc55dbe6fabcc1a98845718b6285da5ecf967a5e4554bb8157dc6d999613399e730f96103a4a254ae336a6
7
- data.tar.gz: c47a61a034ee4271c7de661a765ca50fddf632c56b62a29bd8225127230fe04b4c3a5f788be0b1fd13d248e405f57e16e272f0705d66ae0161d6eeeb6ddbd80b
6
+ metadata.gz: 84dc689092a381d52aa4740a894d42fe95dc6e744c739b44daf92777cefece4711260c0f05f5e05f15964cdfcf03783d0b881fd06eda13a8a0fa98b627941967
7
+ data.tar.gz: b60229476423fffa95cd0697de5b3e8e7151eba286905ed0a08b8419781aa3ae0b4254ade52c909cba2539f3f62c0ad425cecc639ba2f16c1925c0c8b79da13f
data/README.md CHANGED
@@ -5,7 +5,21 @@ This is the Ruby SDK for Vigilant (https://vigilant.run).
5
5
  ## Installation
6
6
 
7
7
  ```bash
8
- gem install vigilant-ruby
8
+ bundle add vigilant-ruby
9
+ ```
10
+
11
+ ## Logging Usage (Rails)
12
+
13
+ ```ruby
14
+ # In config/application.rb
15
+ require 'vigilant-ruby'
16
+
17
+ module YourApp
18
+ class Application < Rails::Application
19
+ config.logger = Vigilant::Rails::Logger.new(token: 'tk_1234567890', name: 'test-app')
20
+ config.log_level = :debug
21
+ end
22
+ end
9
23
  ```
10
24
 
11
25
  ## Logging Usage (Standard)
@@ -15,8 +29,8 @@ require 'vigilant-ruby'
15
29
 
16
30
  # Initialize the logger
17
31
  logger = Vigilant::Logger.new(
18
- endpoint: "ingress.vigilant.run",
19
- token: "tk_0000000000000000",
32
+ name: 'test-app',
33
+ token: 'tk_1234567890',
20
34
  )
21
35
 
22
36
  # Basic logging
@@ -39,18 +53,18 @@ require 'vigilant-ruby'
39
53
 
40
54
  # Initialize the logger
41
55
  logger = Vigilant::Logger.new(
42
- endpoint: "ingress.vigilant.run",
43
- token: "tk_0000000000000000",
56
+ name: 'test-app',
57
+ token: 'tk_1234567890',
44
58
  )
45
59
 
46
60
  # Enable autocapture
47
61
  logger.autocapture_enable
48
62
 
49
63
  # Log with autocapture
50
- puts "A print statement"
64
+ puts 'A print statement'
51
65
 
52
66
  # Log without autocapture
53
- logger.info("A regular log")
67
+ logger.info('A regular log')
54
68
 
55
69
  # Shutdown the logger
56
70
  logger.shutdown
@@ -18,11 +18,13 @@ module Vigilant
18
18
  class Logger
19
19
  # Initialize a Vigilant::Logger instance.
20
20
  #
21
+ # @param name [String] The name of the application.
21
22
  # @param endpoint [String] The base endpoint for the Vigilant API (e.g. "ingress.vigilant.run").
22
23
  # @param token [String] The authentication token for the Vigilant API.
23
24
  # @param insecure [Boolean] Whether to use HTTP instead of HTTPS (optional, defaults to false).
24
- # @param passthrough [Boolean] Whether to also print logs to stdout/stderr (optional, defaults to true).
25
- def initialize(endpoint:, token:, insecure: false, passthrough: true)
25
+ # @param passthrough [Boolean] Whether to also print logs to stdout/stderr (optional, defaults to false).
26
+ def initialize(name:, token:, endpoint:, insecure: false, passthrough: false)
27
+ @name = name
26
28
  @token = token
27
29
 
28
30
  protocol = insecure ? 'http://' : 'https://'
@@ -47,22 +49,18 @@ module Vigilant
47
49
  start_dispatcher
48
50
  end
49
51
 
50
- # Logs a debug message.
51
52
  def debug(body, attributes = {})
52
53
  enqueue_log(DEBUG, body, attributes)
53
54
  end
54
55
 
55
- # Logs an info message.
56
56
  def info(body, attributes = {})
57
57
  enqueue_log(INFO, body, attributes)
58
58
  end
59
59
 
60
- # Logs a warning message.
61
60
  def warn(body, attributes = {})
62
61
  enqueue_log(WARNING, body, attributes)
63
62
  end
64
63
 
65
- # Logs an error message.
66
64
  def error(body, error = nil, attributes = {})
67
65
  if error.nil?
68
66
  enqueue_log(ERROR, body, attributes)
@@ -72,7 +70,6 @@ module Vigilant
72
70
  end
73
71
  end
74
72
 
75
- # Enables stdout/stderr autocapture.
76
73
  def autocapture_enable
77
74
  return if @autocapture_enabled
78
75
 
@@ -81,7 +78,6 @@ module Vigilant
81
78
  $stderr = StderrInterceptor.new(self, @original_stderr)
82
79
  end
83
80
 
84
- # Disables stdout/stderr autocapture.
85
81
  def autocapture_disable
86
82
  return unless @autocapture_enabled
87
83
 
@@ -90,7 +86,6 @@ module Vigilant
90
86
  $stderr = @original_stderr
91
87
  end
92
88
 
93
- # Shuts down the logger, flushing any pending logs.
94
89
  def shutdown
95
90
  flush_if_needed(force: true)
96
91
  @mutex.synchronize { @shutdown = true }
@@ -106,7 +101,7 @@ module Vigilant
106
101
  timestamp: Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%9NZ'),
107
102
  body: body.to_s,
108
103
  level: level.to_s,
109
- attributes: attributes.transform_values(&:to_s)
104
+ attributes: internal_attributes.merge(attributes.transform_values(&:to_s))
110
105
  }
111
106
 
112
107
  @queue << log_msg
@@ -165,6 +160,10 @@ module Vigilant
165
160
 
166
161
  http.request(request)
167
162
  end
163
+
164
+ def internal_attributes
165
+ { 'service.name' => @name }
166
+ end
168
167
  end
169
168
 
170
169
  # Interceptor for capturing stdout
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/logger'
4
+ require 'json'
5
+
6
+ module Vigilant
7
+ module Rails
8
+ # A wrapper that delegates Rails-style logging calls to Vigilant::Logger
9
+ class Logger < ::ActiveSupport::Logger
10
+ include ::ActiveSupport::LoggerThreadSafeLevel if defined?(::ActiveSupport::LoggerThreadSafeLevel)
11
+
12
+ if defined?(::ActiveSupport::LoggerSilence)
13
+ include ::ActiveSupport::LoggerSilence
14
+ elsif defined?(::LoggerSilence)
15
+ include ::LoggerSilence
16
+ end
17
+
18
+ SEVERITY_MAP = {
19
+ ::Logger::DEBUG => Vigilant::DEBUG,
20
+ ::Logger::INFO => Vigilant::INFO,
21
+ ::Logger::WARN => Vigilant::WARNING,
22
+ ::Logger::ERROR => Vigilant::ERROR,
23
+ ::Logger::FATAL => Vigilant::ERROR,
24
+ ::Logger::UNKNOWN => Vigilant::ERROR
25
+ }.freeze
26
+
27
+ def initialize(name: Vigilant.configuration.name,
28
+ endpoint: Vigilant.configuration.endpoint,
29
+ token: Vigilant.configuration.token,
30
+ insecure: Vigilant.configuration.insecure,
31
+ passthrough: Vigilant.configuration.passthrough)
32
+ super(nil)
33
+
34
+ @vigilant_logger = Vigilant::Logger.new(
35
+ endpoint: endpoint,
36
+ token: token,
37
+ name: name,
38
+ insecure: insecure,
39
+ passthrough: passthrough
40
+ )
41
+
42
+ at_exit { close }
43
+
44
+ self.level = ::Logger::DEBUG
45
+ @tags = []
46
+ @extra_loggers = []
47
+ end
48
+
49
+ def kind_of?(klass)
50
+ return true if defined?(::ActiveSupport::BroadcastLogger) && klass == ::ActiveSupport::BroadcastLogger
51
+
52
+ super(klass)
53
+ end
54
+ alias is_a? kind_of?
55
+
56
+ def broadcasts
57
+ [self] + @extra_loggers
58
+ end
59
+
60
+ def broadcast_to(*io_devices_and_loggers)
61
+ io_devices_and_loggers.each do |io_device_or_logger|
62
+ extra_logger =
63
+ if io_device_or_logger.is_a?(::Logger)
64
+ io_device_or_logger
65
+ else
66
+ ::ActiveSupport::Logger.new(io_device_or_logger)
67
+ end
68
+ @extra_loggers << extra_logger
69
+ end
70
+ end
71
+
72
+ def stop_broadcasting_to(io_device_or_logger)
73
+ if io_device_or_logger.is_a?(::Logger)
74
+ @extra_loggers.delete(io_device_or_logger)
75
+ else
76
+ @extra_loggers.reject! do |logger|
77
+ defined?(::ActiveSupport::Logger) &&
78
+ ::ActiveSupport::Logger.logger_outputs_to?(logger, io_device_or_logger)
79
+ end
80
+ end
81
+ end
82
+
83
+ def debug(progname = nil, &block)
84
+ add(::Logger::DEBUG, block, progname)
85
+ end
86
+
87
+ def info(progname = nil, &block)
88
+ add(::Logger::INFO, block, progname)
89
+ end
90
+
91
+ def warn(progname = nil, &block)
92
+ add(::Logger::WARN, block, progname)
93
+ end
94
+
95
+ def error(progname = nil, &block)
96
+ add(::Logger::ERROR, block, progname)
97
+ end
98
+
99
+ def fatal(progname = nil, &block)
100
+ add(::Logger::FATAL, block, progname)
101
+ end
102
+
103
+ def unknown(progname = nil, &block)
104
+ add(::Logger::UNKNOWN, block, progname)
105
+ end
106
+
107
+ def add(severity, message_or_block = nil, progname = nil)
108
+ return true if severity < level
109
+
110
+ msg =
111
+ if message_or_block.respond_to?(:call)
112
+ message_or_block.call.to_s
113
+ else
114
+ (message_or_block || progname).to_s
115
+ end.strip
116
+
117
+ vigilant_severity = SEVERITY_MAP.fetch(severity, Vigilant::ERROR)
118
+ log_to_vigilant(vigilant_severity, msg)
119
+
120
+ @extra_loggers.each { |logger| logger.add(severity, msg, progname) }
121
+ true
122
+ end
123
+
124
+ def tagged(*tags)
125
+ push_tags(*tags)
126
+ yield self
127
+ ensure
128
+ pop_tags(tags.size)
129
+ end
130
+
131
+ def push_tags(*tags)
132
+ @tags.concat(tags)
133
+ end
134
+
135
+ def pop_tags(amount = 1)
136
+ @tags.pop(amount)
137
+ end
138
+
139
+ def current_tags
140
+ @tags
141
+ end
142
+
143
+ def silence(temporary_level = ::Logger::ERROR)
144
+ old_level = level
145
+ self.level = temporary_level
146
+ yield self
147
+ ensure
148
+ self.level = old_level
149
+ end
150
+
151
+ def flush
152
+ @vigilant_logger.flush if @vigilant_logger.respond_to?(:flush)
153
+ rescue StandardError
154
+ nil
155
+ end
156
+
157
+ def reopen(_device = nil)
158
+ nil
159
+ end
160
+
161
+ def close
162
+ @vigilant_logger.shutdown if @vigilant_logger.respond_to?(:shutdown)
163
+ end
164
+
165
+ def datetime_format=(_format)
166
+ nil
167
+ end
168
+
169
+ def datetime_format
170
+ nil
171
+ end
172
+
173
+ attr_accessor :formatter
174
+
175
+ private
176
+
177
+ def log_to_vigilant(severity, message)
178
+ formatted_message, attributes = format_message(message)
179
+ case severity
180
+ when Vigilant::DEBUG
181
+ @vigilant_logger.debug(formatted_message, attributes)
182
+ when Vigilant::INFO
183
+ @vigilant_logger.info(formatted_message, attributes)
184
+ when Vigilant::WARNING
185
+ @vigilant_logger.warn(formatted_message, attributes)
186
+ when Vigilant::ERROR
187
+ @vigilant_logger.error(formatted_message, nil, attributes)
188
+ end
189
+ end
190
+
191
+ def format_message(message)
192
+ formatted_message = format_tags(message)
193
+ attributes = {}
194
+
195
+ begin
196
+ attributes = format_json_attributes(message) if message.start_with?('{') && message.end_with?('}')
197
+ rescue JSON::ParserError
198
+ nil
199
+ end
200
+
201
+ begin
202
+ attributes = format_key_value_attributes(message) if message.match?(/^[\w\-.]+=/)
203
+ rescue StandardError
204
+ nil
205
+ end
206
+
207
+ [formatted_message, attributes]
208
+ end
209
+
210
+ def format_json_attributes(message)
211
+ json_data = JSON.parse(message)
212
+ attributes = json_data['attributes'] ||= {}
213
+
214
+ json_data.each do |key, value|
215
+ attributes[key] = value
216
+ json_data.delete(key)
217
+ end
218
+
219
+ attributes['tags'] = @tags
220
+ attributes
221
+ end
222
+
223
+ def format_key_value_attributes(message)
224
+ attributes = {}
225
+ message.split(' ').each do |pair|
226
+ key, value = pair.split('=', 2)
227
+ value = case value
228
+ when /^\d+$/
229
+ value.to_i
230
+ when /^\d*\.\d+$/
231
+ value.to_f
232
+ else
233
+ value
234
+ end
235
+ attributes[key] = value
236
+ end
237
+
238
+ attributes['tags'] = @tags
239
+ attributes
240
+ end
241
+
242
+ def format_tags(message)
243
+ tag_prefix = @tags.map { |t| "[#{t}] " }.join
244
+ "#{tag_prefix}#{message}"
245
+ end
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+ require 'vigilant-ruby/logger'
5
+ require 'vigilant-ruby/rails/logger'
6
+
7
+ module Vigilant
8
+ module Rails
9
+ # Railtie for integrating Vigilant with Rails.
10
+ class Railtie < ::Rails::Railtie
11
+ initializer 'vigilant.rails_integration' do
12
+ nil
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vigilant
4
- VERSION = '0.0.5'
4
+ VERSION = '0.0.7'
5
5
  end
data/lib/vigilant-ruby.rb CHANGED
@@ -2,21 +2,22 @@
2
2
 
3
3
  require 'vigilant-ruby/version'
4
4
  require 'vigilant-ruby/logger'
5
+ require 'vigilant-ruby/rails/railtie' if defined?(Rails)
5
6
 
6
- # Vigilant is a logging service that provides structured logging capabilities
7
- # with asynchronous batch processing and thread-safe operations.
7
+ # Vigilant is a logging library for the Vigilant platform.
8
8
  module Vigilant
9
9
  class Error < StandardError; end
10
10
 
11
11
  # Configuration for the Vigilant logging service.
12
12
  class Configuration
13
- attr_accessor :endpoint, :token, :insecure, :passthrough
13
+ attr_accessor :name, :token, :endpoint, :insecure, :passthrough
14
14
 
15
15
  def initialize
16
+ @name = 'test-app'
17
+ @token = 'tk_1234567890'
16
18
  @endpoint = 'ingress.vigilant.run'
17
19
  @insecure = false
18
- @token = 'tk_1234567890'
19
- @passthrough = true
20
+ @passthrough = false
20
21
  end
21
22
  end
22
23
 
@@ -31,9 +32,10 @@ module Vigilant
31
32
 
32
33
  def logger
33
34
  @logger ||= Vigilant::Logger.new(
35
+ name: configuration.name,
36
+ token: configuration.token,
34
37
  endpoint: configuration.endpoint,
35
38
  insecure: configuration.insecure,
36
- token: configuration.token,
37
39
  passthrough: configuration.passthrough
38
40
  )
39
41
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vigilant-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vigilant
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-27 00:00:00.000000000 Z
10
+ date: 2025-01-29 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: json
@@ -89,6 +89,8 @@ files:
89
89
  - README.md
90
90
  - lib/vigilant-ruby.rb
91
91
  - lib/vigilant-ruby/logger.rb
92
+ - lib/vigilant-ruby/rails/logger.rb
93
+ - lib/vigilant-ruby/rails/railtie.rb
92
94
  - lib/vigilant-ruby/version.rb
93
95
  homepage: https://vigilant.run
94
96
  licenses: