logtail-ruby 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a1b5ff5c6415b72a76574ad17cdf273b69a2ee163f3751c982b933f92518379
4
- data.tar.gz: ddcddf4086118f54a0528ed76b6c550f30e9f0c39be44b016116a736bcf1b104
3
+ metadata.gz: 47c1ca6bdaee5eaf634fb6a058366c393f74bc75c295d96cdcca3f1f36f3f7fb
4
+ data.tar.gz: 2e7ac265ac46b0da133afd23a368fbeb0e8097aa23400c074627490251953065
5
5
  SHA512:
6
- metadata.gz: d64a8e17cb3fc10dbb19d4582979383aa3efd478e7f0284e84e82f1e4ea732c228c2f1d0954ae16d4d6ef44f17dcf18a351f2e91fa5eab5611b4f7b3ff2c768c
7
- data.tar.gz: bdce70cae9d0b3aa09128a63ab252e74d5c0bda77eb044a9fd5c5989ce5a52659ff8ab869aaf8438e5371ad6fb0a626b85748febb50a85126da5bc0f2bc1ed5b
6
+ metadata.gz: 79b04cbef17d3dee4fb6ce5a4c7dc5514f63aa511ab12298ebd9ec421c1d4c8cbc1a9bd9f8bad8685bc0442ae36b739aea0f78d76fad43a3257e6d68ec3bc369
7
+ data.tar.gz: 948a849a7714c8aa8ca24315121ac40a3a94ca7acd0f5a9d78afc53a25dde2ecbaa09141085690d32de147794c035d3ac0eab73b6fdbd2ec4dbcc35369ac33ee
data/CHANGELOG.md CHANGED
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ## [1.0.0] - 2021-02-11
10
+ ## [0.1.0] - 2021-02-11
11
11
 
12
12
  - The first version of the client.
13
+
14
+ ## [0.1.2] - 2021-04-22
15
+
16
+ - Fixed string encoding.
data/Gemfile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- # Specify your gem's dependencies in logtail-ruby.gemspec
5
+ # Specify your gem's dependencies in logtail.gemspec
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ def puts_with_level(message, level = :info)
14
14
  end
15
15
  end
16
16
 
17
- task :test_the_pipes, [:api_key] do |t, args|
17
+ task :test_the_pipes, [:source_token] do |t, args|
18
18
  support_email = "support@logtail.com"
19
19
  # Do not modify below this line. It's important to keep the `Logtail::Logger`
20
20
  # because it provides an API for logging structured data and capturing context.
@@ -26,7 +26,7 @@ task :test_the_pipes, [:api_key] do |t, args|
26
26
 
27
27
  current_context = Logtail::CurrentContext.instance.snapshot
28
28
  entry = Logtail::LogEntry.new(:info, Time.now, nil, "Testing the pipes (click the inspect icon to view more details)", current_context, nil)
29
- http_device = Logtail::LogDevices::HTTP.new(args.api_key, flush_continuously: false)
29
+ http_device = Logtail::LogDevices::HTTP.new(args.source_token, flush_continuously: false)
30
30
  response = http_device.deliver_one(entry)
31
31
  if response.is_a?(Exception)
32
32
  message = <<~HEREDOC
@@ -17,7 +17,7 @@ module Logtail
17
17
  #
18
18
  # See {#initialize} for options and more details.
19
19
  class HTTP
20
- LOGTAIL_STAGING_HOST = "in-staging.logtail.com".freeze
20
+ LOGTAIL_STAGING_HOST = "in.logtail.dev".freeze
21
21
  LOGTAIL_PRODUCTION_HOST = "in.logtail.com".freeze
22
22
  LOGTAIL_HOST = ENV['LOGTAIL_STAGING'] ? LOGTAIL_STAGING_HOST : LOGTAIL_PRODUCTION_HOST
23
23
  LOGTAIL_PORT = 443
@@ -36,7 +36,7 @@ module Logtail
36
36
  # you can drop the log messages instead by passing a {DroppingSizedQueue} via the
37
37
  # `:request_queue` option.
38
38
  #
39
- # @param api_key [String] The API key provided to you after you add your application to
39
+ # @param source_token [String] The API key provided to you after you add your application to
40
40
  # [Logtail](https://logtail.com).
41
41
  # @param [Hash] options the options to create a HTTP log device with.
42
42
  # @option attributes [Symbol] :batch_size (1000) Determines the maximum of log lines in
@@ -64,13 +64,13 @@ module Logtail
64
64
  # The default is set via {LOGTAIL_HOST}.
65
65
  #
66
66
  # @example Basic usage
67
- # Logtail::Logger.new(Logtail::LogDevices::HTTP.new("my_logtail_api_key"))
67
+ # Logtail::Logger.new(Logtail::LogDevices::HTTP.new("my_logtail_source_token"))
68
68
  #
69
69
  # @example Apply back pressure instead of dropping messages
70
- # http_log_device = Logtail::LogDevices::HTTP.new("my_logtail_api_key", request_queue: SizedQueue.new(25))
70
+ # http_log_device = Logtail::LogDevices::HTTP.new("my_logtail_source_token", request_queue: SizedQueue.new(25))
71
71
  # Logtail::Logger.new(http_log_device)
72
- def initialize(api_key, options = {})
73
- @api_key = api_key || raise(ArgumentError.new("The api_key parameter cannot be blank"))
72
+ def initialize(source_token, options = {})
73
+ @source_token = source_token || raise(ArgumentError.new("The source_token parameter cannot be blank"))
74
74
  @logtail_host = options[:logtail_host] || ENV['LOGTAIL_HOST'] || LOGTAIL_HOST
75
75
  @logtail_port = options[:logtail_port] || ENV['LOGTAIL_PORT'] || LOGTAIL_PORT
76
76
  @logtail_scheme = options[:logtail_scheme] || ENV['LOGTAIL_SCHEME'] || LOGTAIL_SCHEME
@@ -168,7 +168,7 @@ MESSAGE
168
168
  end
169
169
 
170
170
  raise <<-MESSAGE
171
-
171
+
172
172
  Log delivery failed! No request was made.
173
173
 
174
174
  You can enable internal debug logging with the following:
@@ -201,10 +201,20 @@ MESSAGE
201
201
  req['Authorization'] = authorization_payload
202
202
  req['Content-Type'] = CONTENT_TYPE
203
203
  req['User-Agent'] = USER_AGENT
204
- req.body = msgs.to_msgpack
204
+ req.body = msgs.map { |msg| force_utf8_encoding(msg.to_hash) }.to_msgpack
205
205
  req
206
206
  end
207
207
 
208
+ def force_utf8_encoding(data)
209
+ if data.respond_to?(:force_encoding)
210
+ data.dup.force_encoding('UTF-8')
211
+ elsif data.respond_to?(:transform_values)
212
+ data.transform_values { |val| force_utf8_encoding(val) }
213
+ else
214
+ data
215
+ end
216
+ end
217
+
208
218
  # Flushes the message buffer asynchronously. The reason we provide this
209
219
  # method is because the message buffer limit is constricted by the
210
220
  # Logtail API. The application limit is multiples of the buffer limit,
@@ -361,7 +371,7 @@ MESSAGE
361
371
 
362
372
  # Builds the `Authorization` header value for HTTP delivery to the Logtail API.
363
373
  def authorization_payload
364
- "Bearer #{@api_key}"
374
+ "Bearer #{@source_token}"
365
375
  end
366
376
  end
367
377
  end
@@ -1,5 +1,6 @@
1
1
  require "socket"
2
2
  require "time"
3
+ require "pathname"
3
4
 
4
5
  require "logtail/contexts"
5
6
  require "logtail/events"
@@ -11,6 +12,7 @@ module Logtail
11
12
  BINARY_LIMIT_THRESHOLD = 1_000.freeze
12
13
  DT_PRECISION = 6.freeze
13
14
  MESSAGE_MAX_BYTES = 8192.freeze
15
+ LOGTAIL_GEM_REGEX = /\/logtail(?:-ruby|-rails|-rack)?(?:-\d+(?:\.\d+)*)?\/lib$/.freeze
14
16
 
15
17
  attr_reader :context_snapshot, :event, :level, :message, :progname, :tags, :time
16
18
 
@@ -45,7 +47,7 @@ module Logtail
45
47
  hash = {
46
48
  :level => level,
47
49
  :dt => formatted_dt,
48
- :message => message
50
+ :message => message,
49
51
  }
50
52
 
51
53
  if !tags.nil? && tags.length > 0
@@ -60,6 +62,10 @@ module Logtail
60
62
  hash[:context] = context_snapshot
61
63
  end
62
64
 
65
+ hash[:context] ||= {}
66
+ hash[:context][:runtime] ||= {}
67
+ hash[:context][:runtime].merge!(current_runtime_context || {})
68
+
63
69
  if options[:only]
64
70
  hash.select do |key, _value|
65
71
  options[:only].include?(key)
@@ -106,5 +112,34 @@ module Logtail
106
112
  rescue Exception
107
113
  nil
108
114
  end
115
+
116
+ def current_runtime_context
117
+ index = caller_locations.rindex { |x| logtail_frame?(x) }
118
+ frame = caller_locations[index + 1] unless index.nil?
119
+ return convert_to_runtime_context(frame) unless frame.nil?
120
+ end
121
+
122
+ def convert_to_runtime_context(frame)
123
+ {
124
+ file: relative_to_main_module(frame.absolute_path),
125
+ line: frame.lineno,
126
+ frame_label: frame.label,
127
+ }
128
+ end
129
+
130
+ def logtail_frame?(frame)
131
+ return false if frame.absolute_path.nil? || logtail_gem_paths.empty?
132
+ logtail_gem_paths.any? { |path| frame.absolute_path.start_with?(path) }
133
+ end
134
+
135
+ def logtail_gem_paths
136
+ @logtail_gem_paths ||= $LOAD_PATH.select { |path| path.match(LOGTAIL_GEM_REGEX) }
137
+ end
138
+
139
+ def relative_to_main_module(path)
140
+ base_file = caller_locations.last.absolute_path
141
+ base_path = Pathname.new(File.dirname(base_file || '/'))
142
+ Pathname.new(path).relative_path_from(base_path).to_s
143
+ end
109
144
  end
110
145
  end
@@ -135,7 +135,7 @@ module Logtail
135
135
  # logger = Logtail::Logger.new(STDOUT)
136
136
  #
137
137
  # @example Logging to the Logtail HTTP device
138
- # http_device = Logtail::LogDevices::HTTP.new("my-logtail-api-key")
138
+ # http_device = Logtail::LogDevices::HTTP.new("my-logtail-source-token")
139
139
  # logger = Logtail::Logger.new(http_device)
140
140
  #
141
141
  # @example Logging to a file (with rotation)
@@ -143,7 +143,7 @@ module Logtail
143
143
  # logger = Logtail::Logger.new(file_device)
144
144
  #
145
145
  # @example Logging to a file and the Logtail HTTP device (multiple log devices)
146
- # http_device = Logtail::LogDevices::HTTP.new("my-logtail-api-key")
146
+ # http_device = Logtail::LogDevices::HTTP.new("my-logtail-source-token")
147
147
  # file_logger = ::Logger.new("path/to/file.log")
148
148
  # logger = Logtail::Logger.new(http_device, file_logger)
149
149
  def initialize(*io_devices_and_loggers)
@@ -186,6 +186,8 @@ module Logtail
186
186
  Logtail::Config.instance.debug { "Logtail::Logger instantiated, level: #{level}, formatter: #{formatter.class}" }
187
187
 
188
188
  @initialized = true
189
+
190
+ self.warn("The 'logtail-ruby' package has been deprecated. Please, switch to 'logtail' https://rubygems.org/gems/logtail")
189
191
  end
190
192
 
191
193
  # Sets a new formatted on the logger.
@@ -1,3 +1,3 @@
1
1
  module Logtail
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.homepage = "https://github.com/logtail/logtail-ruby"
12
12
  spec.license = "ISC"
13
13
 
14
- spec.summary = "Query logs like you query your database. https://logtail.com"
14
+ spec.summary = "This package has been deprecated. Please, switch to 'logtail' https://rubygems.org/gems/logtail"
15
15
 
16
16
  spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
17
17
  spec.metadata["changelog_uri"] = "#{spec.homepage}/tree/master/CHANGELOG.md"
@@ -102,7 +102,7 @@ describe Logtail::LogDevices::HTTP do
102
102
  request_queue = http.instance_variable_get(:@request_queue)
103
103
  request_attempt = request_queue.deq
104
104
  expect(request_attempt.request).to be_kind_of(Net::HTTP::Post)
105
- expect(request_attempt.request.body).to start_with("\x92\x83\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT"))
105
+ expect(request_attempt.request.body).to start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT"))
106
106
 
107
107
  message_queue = http.instance_variable_get(:@msg_queue)
108
108
  expect(message_queue.size).to eq(0)
@@ -127,7 +127,7 @@ describe Logtail::LogDevices::HTTP do
127
127
  it "should deliver requests on an interval" do
128
128
  stub = stub_request(:post, "https://in.logtail.com/").
129
129
  with(
130
- :body => start_with("\x92\x83\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")),
130
+ :body => start_with("\x92\x84\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z\xA7message\xB2test log message 1".force_encoding("ASCII-8BIT")),
131
131
  :headers => {
132
132
  'Authorization' => 'Bearer MYKEY',
133
133
  'Content-Type' => 'application/msgpack',
@@ -19,4 +19,18 @@ describe Logtail::LogEntry do
19
19
  expect(msgpack).to start_with("\x85\xA5level\xA4INFO\xA2dt\xBB2016-09-01T12:00:00.000000Z".force_encoding("ASCII-8BIT"))
20
20
  end
21
21
  end
22
+
23
+ describe "#to_hash" do
24
+ it "should include runtime context information" do
25
+ $:.unshift(File.expand_path(__dir__ + '/../../lib'))
26
+
27
+ log_entry = described_class.new("INFO", time, nil, "log message", {}, {})
28
+ hash = log_entry.to_hash
29
+ expect(hash[:context]).to_not be_nil
30
+ expect(hash[:context][:runtime]).to_not be_nil
31
+ expect(hash[:context][:runtime][:file]).to_not be_nil
32
+ expect(hash[:context][:runtime][:line]).to_not be_nil
33
+ expect(hash[:context][:runtime][:frame_label]).to_not be_nil
34
+ end
35
+ end
22
36
  end
@@ -161,7 +161,7 @@ describe Logtail::Logger do
161
161
  end
162
162
 
163
163
  context "with the HTTP log device" do
164
- let(:io) { Logtail::LogDevices::HTTP.new("my_key") }
164
+ let(:io) { Logtail::LogDevices::HTTP.new("my_source_token") }
165
165
 
166
166
  it "should use the PassThroughFormatter" do
167
167
  expect(logger.formatter).to be_kind_of(Logtail::Logger::PassThroughFormatter)
@@ -189,7 +189,7 @@ describe Logtail::Logger do
189
189
 
190
190
  describe "#formatter=" do
191
191
  it "should not allow changing the formatter when the device is HTTP" do
192
- http_device = Logtail::LogDevices::HTTP.new("api_key")
192
+ http_device = Logtail::LogDevices::HTTP.new("source_token")
193
193
  logger = Logtail::Logger.new(http_device)
194
194
  expect { logger.formatter = ::Logger::Formatter.new }.to raise_error(ArgumentError)
195
195
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logtail-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Logtail
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-16 00:00:00.000000000 Z
11
+ date: 2023-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -183,7 +183,7 @@ files:
183
183
  - lib/logtail/util.rb
184
184
  - lib/logtail/util/non_nil_hash_builder.rb
185
185
  - lib/logtail/version.rb
186
- - logtail-ruby.gemspec
186
+ - logtail.gemspec
187
187
  - spec/README.md
188
188
  - spec/logtail/current_context_spec.rb
189
189
  - spec/logtail/events/controller_call_spec.rb
@@ -219,10 +219,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  - !ruby/object:Gem::Version
220
220
  version: '0'
221
221
  requirements: []
222
- rubygems_version: 3.2.3
222
+ rubygems_version: 3.3.7
223
223
  signing_key:
224
224
  specification_version: 4
225
- summary: Query logs like you query your database. https://logtail.com
225
+ summary: This package has been deprecated. Please, switch to 'logtail' https://rubygems.org/gems/logtail
226
226
  test_files:
227
227
  - spec/README.md
228
228
  - spec/logtail/current_context_spec.rb