ruby_for_grafana_loki 0.0.1 → 0.0.3
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 +4 -4
- data/README.md +5 -0
- data/lib/ruby_for_grafana_loki/client.rb +11 -4
- 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
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06ddcda970511a6f243e21f6d19eaa42bfabbc2f816cb7530617ef27d03b9715
|
4
|
+
data.tar.gz: 58ca70bcfa882b7266330a1ca941faa49e3f97aa05546cf7e7e5fd2a28bbb875
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be9241c7bab297a1488fc039857fbc76e6bac11a97e343115bc9bedc254ed7ad911d4687a97bf65b58c2e29e65185885d10940affff50b9b018b8d4d7d3796ab
|
7
|
+
data.tar.gz: a847f31bad0534f38356c532c54dd39eb8e02e21175e66bb4823c93e4a80e83e46de678ad288311423817cc95b15198bd4aee266d8e4cca0de58910c4ef369ca
|
data/README.md
CHANGED
@@ -2,10 +2,17 @@ module RubyForGrafanaLoki
|
|
2
2
|
class Client
|
3
3
|
include RubyForGrafanaLoki::Request
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
@
|
5
|
+
def initialize(log_file_path)
|
6
|
+
@log_file_path = log_file_path
|
7
7
|
end
|
8
8
|
|
9
|
+
def send_all_logs
|
10
|
+
File.open(@log_file_path, 'r') do |file|
|
11
|
+
file.each_line do |line|
|
12
|
+
send_log(line)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
9
16
|
def send_log(log_message)
|
10
17
|
curr_datetime = Time.now.to_i * 1_000_000_000
|
11
18
|
|
@@ -35,7 +42,7 @@ module RubyForGrafanaLoki
|
|
35
42
|
uri = '/loki/api/v1/push'
|
36
43
|
|
37
44
|
# Use logger for sending logs to both default logger and Loki
|
38
|
-
@logger.info "Sending log to Loki: #{json_payload}"
|
45
|
+
# @logger.info "Sending log to Loki: #{json_payload}"
|
39
46
|
|
40
47
|
# Send logs to Loki using the post method
|
41
48
|
post(uri, json_payload)
|
@@ -48,7 +55,7 @@ end
|
|
48
55
|
#
|
49
56
|
# # Example with Rails logger
|
50
57
|
# rails_logger = Logger.new(STDOUT)
|
51
|
-
# client =
|
58
|
+
# client = RubyForGrafanaLoki.client(rails_logger)
|
52
59
|
#
|
53
60
|
# # Send a log entry to Loki
|
54
61
|
# 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,7 +1,7 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Ten
|
@@ -54,7 +54,7 @@ files:
|
|
54
54
|
- lib/ruby_for_grafana_loki/query.rb
|
55
55
|
- lib/ruby_for_grafana_loki/request.rb
|
56
56
|
- lib/ruby_for_grafana_loki/version.rb
|
57
|
-
homepage: https://
|
57
|
+
homepage: https://github.com/tennet0505/31github/tree/main/ruby_for_grafana_loki
|
58
58
|
licenses:
|
59
59
|
- MIT
|
60
60
|
metadata: {}
|