ruby_for_grafana_loki 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/lib/ruby_for_grafana_loki/client.rb +19 -15
- data/lib/ruby_for_grafana_loki/connection.rb +22 -22
- data/lib/ruby_for_grafana_loki/request.rb +24 -25
- data/lib/ruby_for_grafana_loki/version.rb +1 -1
- data/lib/ruby_for_grafana_loki.rb +2 -2
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50f0f3f252b03e126ca79c758b0ec428942695733470126f9d70a1f4d9d817b1
|
4
|
+
data.tar.gz: 4bf5183f13b98110611e0767d2cb196c70ac2bb9a92f7711b36e90df71519087
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d5fccfd9c6a16fdfa7c710bcb3bd1a04c81343e6bcbccc4346530c45c62d269e50e26ab55ec126fce053d5dcfadad86fe11673c86f753593bdd84a5fce435a66
|
7
|
+
data.tar.gz: 5400a75de89bdbc5df1daf9078079130a3c89c4d41ba6c93a7c23e059b949e09af7413331b45f697be566f9645c929b2e94d399ef58b8681cdb68a20aa1bf81a
|
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
|
2
|
+
# Usage in your gem or application
|
3
|
+
<pre>
|
2
4
|
|
3
|
-
# Example with Rails logger
|
4
|
-
rails_logger = Logger.new(STDOUT)
|
5
|
-
client = RubyForGrafanaLoki.client(rails_logger)
|
6
5
|
|
7
|
-
#
|
8
|
-
|
6
|
+
logs_type = %w(ERROR WARN) # Use custom logs type: ERROR, WARN, FATAL, INFO, DEBUG
|
7
|
+
log_file_path = "log/#{Rails.env}.log"
|
8
|
+
client = RubyForGrafanaLoki.client(log_file_path, logs_type)
|
9
|
+
client.send_all_logs
|
10
|
+
client.send_log("This is a log message.") # not required
|
11
|
+
</pre>
|
@@ -1,11 +1,20 @@
|
|
1
1
|
module RubyForGrafanaLoki
|
2
|
+
LOGS_TYPE = %w(ERROR WARN FATAL INFO DEBUG).freeze
|
2
3
|
class Client
|
3
4
|
include RubyForGrafanaLoki::Request
|
4
5
|
|
5
|
-
def initialize(
|
6
|
-
@
|
6
|
+
def initialize(log_file_path, allowed_logs_type = LOGS_TYPE)
|
7
|
+
@log_file_path = log_file_path
|
8
|
+
@allowed_logs_type = allowed_logs_type
|
7
9
|
end
|
8
10
|
|
11
|
+
def send_all_logs
|
12
|
+
File.open(@log_file_path, 'r') do |file|
|
13
|
+
file.each_line do |line|
|
14
|
+
send_log(line) if match_logs_type?(line)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
9
18
|
def send_log(log_message)
|
10
19
|
curr_datetime = Time.now.to_i * 1_000_000_000
|
11
20
|
|
@@ -34,21 +43,16 @@ module RubyForGrafanaLoki
|
|
34
43
|
json_payload = JSON.generate(payload)
|
35
44
|
uri = '/loki/api/v1/push'
|
36
45
|
|
37
|
-
# Use logger for sending logs to both default logger and Loki
|
38
|
-
@logger.info "Sending log to Loki: #{json_payload}"
|
39
|
-
|
40
|
-
# Send logs to Loki using the post method
|
41
46
|
post(uri, json_payload)
|
42
47
|
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def match_logs_type?(log_line)
|
52
|
+
type = log_line.match(/(ERROR|WARN|FATAL|INFO|DEBUG)/)&.to_s
|
53
|
+
|
54
|
+
@allowed_logs_type.include?(type)
|
55
|
+
end
|
43
56
|
end
|
44
57
|
end
|
45
58
|
|
46
|
-
|
47
|
-
# # In your application code
|
48
|
-
#
|
49
|
-
# # Example with Rails logger
|
50
|
-
# rails_logger = Logger.new(STDOUT)
|
51
|
-
# client = RubyForGrafanaLoki.client(rails_logger)
|
52
|
-
#
|
53
|
-
# # Send a log entry to Loki
|
54
|
-
# client.send_log("This is a log message.")
|
@@ -4,36 +4,36 @@ module RubyForGrafanaLoki
|
|
4
4
|
BASE_URL = 'http://localhost:3100/'
|
5
5
|
|
6
6
|
def connection
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
Faraday.new(options) do |faraday|
|
8
|
+
faraday.adapter Faraday.default_adapter
|
9
|
+
faraday.request :url_encoded
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
def post(path, body)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
response = connection.post(path) do |req|
|
15
|
+
req.headers['Content-Type'] = 'application/json'
|
16
|
+
req.body = JSON.generate(body)
|
17
|
+
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
19
|
+
# Check if the request was successful
|
20
|
+
if response.success?
|
21
|
+
return JSON.parse(response.body)
|
22
|
+
else
|
23
|
+
raise "Failed to make POST request. Response code: #{response.status}, Response body: #{response.body}"
|
25
24
|
end
|
25
|
+
end
|
26
26
|
|
27
27
|
private
|
28
28
|
def options
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
headers = {
|
30
|
+
accept: 'application/json',
|
31
|
+
'Content-Type' => 'application/json'
|
32
|
+
}
|
33
|
+
return {
|
34
|
+
url: BASE_URL,
|
35
|
+
headers: headers
|
32
36
|
}
|
33
|
-
|
34
|
-
|
35
|
-
headers: headers
|
36
|
-
}
|
37
|
-
end
|
38
|
-
end
|
37
|
+
end
|
38
|
+
end
|
39
39
|
end
|
@@ -1,29 +1,28 @@
|
|
1
1
|
module RubyForGrafanaLoki
|
2
|
-
|
3
|
-
|
2
|
+
module Request
|
3
|
+
include RubyForGrafanaLoki:: Connection
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
def post(url, payload)
|
6
|
+
respond_with(
|
7
|
+
connection.post url, payload
|
8
|
+
)
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
11
|
+
private
|
12
|
+
def respond_with(response)
|
13
|
+
if response.success?
|
14
|
+
puts 'Log sent successfully to Loki.'
|
15
|
+
puts "Response code: #{response.status}, Response body: #{response.body}"
|
16
|
+
body = response.body.empty? ? response.body : JSON.parse(response.body)
|
17
|
+
puts body
|
18
|
+
else
|
19
|
+
puts "Failed to send log to Loki. Response code: #{response.status}, Response body: #{response.body}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
def get(path)
|
23
|
+
respond_with(
|
24
|
+
connection.get(path)
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
29
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_for_grafana_loki
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Ten
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
description: Attempt to make gem
|
42
56
|
email:
|
43
57
|
- tennet0505@gmail.com
|