ruby_for_grafana_loki 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5ec9e06323e8376d4ee340c324820349259f2d3b9590d66b494fa537520e6e15
4
+ data.tar.gz: 878b6ef35c8ae5635b96a8c525f36dbbcabb92cf8f5f7b38af660ccc3c71d304
5
+ SHA512:
6
+ metadata.gz: 311e1cabe3829e3f4b5a4a9f5fc3f3c6da4842eb4767531d7d76dbc658f862cd469b6ef5b4bee6a6defbcdca6e93af5ff5415e28f08390bfb637aab7e508f2cd
7
+ data.tar.gz: e00297c20a6d8e57fed8b65e6005d23dabf3fdf4f4695efa8d1b5c7ab08ee544fb8cd0820e5a6f5b2d75b7a1cd5508f13b0d018e8f3e9dd67aa5843085b3efb9
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org/'
2
+
3
+ gemspec
data/README.md ADDED
File without changes
@@ -0,0 +1,54 @@
1
+ module RubyForGrafanaLoki
2
+ class Client
3
+ include RubyForGrafanaLoki::Request
4
+
5
+ def initialize(logger)
6
+ @logger = logger
7
+ end
8
+
9
+ def send_log(log_message)
10
+ curr_datetime = Time.now.to_i * 1_000_000_000
11
+
12
+ host = "somehost"
13
+ msg = "On server #{host} detected error"
14
+
15
+ payload = {
16
+ "streams" => [
17
+ {
18
+ "stream" => {
19
+ "source" => "Name-of-your-source",
20
+ "job" => "name-of-your-job",
21
+ "host" => host
22
+ },
23
+ "values" => [[curr_datetime.to_s, log_message]],
24
+ "entries" => [
25
+ {
26
+ "ts" => curr_datetime,
27
+ "line" => "[WARN] " + msg
28
+ }
29
+ ]
30
+ }
31
+ ]
32
+ }
33
+
34
+ json_payload = JSON.generate(payload)
35
+ uri = '/loki/api/v1/push'
36
+
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
+ post(uri, json_payload)
42
+ end
43
+ end
44
+ end
45
+
46
+
47
+ # # In your application code
48
+ #
49
+ # # Example with Rails logger
50
+ # rails_logger = Logger.new(STDOUT)
51
+ # client = RailsLokiExporterDev.client(rails_logger)
52
+ #
53
+ # # Send a log entry to Loki
54
+ # client.send_log("This is a log message.")
@@ -0,0 +1,39 @@
1
+ module RubyForGrafanaLoki
2
+ module Connection
3
+
4
+ BASE_URL = 'http://localhost:3100/'
5
+
6
+ def connection
7
+ Faraday.new(options) do |faraday|
8
+ faraday.adapter Faraday.default_adapter
9
+ faraday.request :url_encoded
10
+ end
11
+ end
12
+
13
+ def post(path, body)
14
+ response = connection.post(path) do |req|
15
+ req.headers['Content-Type'] = 'application/json'
16
+ req.body = JSON.generate(body)
17
+ end
18
+
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}"
24
+ end
25
+ end
26
+
27
+ private
28
+ def options
29
+ headers = {
30
+ accept: 'application/json',
31
+ 'Content-Type' => 'application/json'
32
+ }
33
+ return {
34
+ url: BASE_URL,
35
+ headers: headers
36
+ }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,8 @@
1
+ module RubyForGrafanaLoki
2
+ class Query
3
+ attr_reader :streams_data
4
+ def initialize(response)
5
+ @streams_data = response['streams']
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+ module RubyForGrafanaLoki
2
+ module Request
3
+ include RubyForGrafanaLoki:: Connection
4
+
5
+ def post(url, payload)
6
+ respond_with(
7
+ connection.post url, payload
8
+ )
9
+ end
10
+
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
+
23
+ def get(path)
24
+ respond_with(
25
+ connection.get(path)
26
+ )
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module RubyForGrafanaLoki
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'faraday'
2
+ require 'zeitwerk'
3
+
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.setup
6
+
7
+ module RubyForGrafanaLoki
8
+ def self.client(logger)
9
+ RubyForGrafanaLoki::Client.new(logger)
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_for_grafana_loki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Ten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: zeitwerk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.4'
41
+ description: Attempt to make gem
42
+ email:
43
+ - tennet0505@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - README.md
48
+ files:
49
+ - Gemfile
50
+ - README.md
51
+ - lib/ruby_for_grafana_loki.rb
52
+ - lib/ruby_for_grafana_loki/client.rb
53
+ - lib/ruby_for_grafana_loki/connection.rb
54
+ - lib/ruby_for_grafana_loki/query.rb
55
+ - lib/ruby_for_grafana_loki/request.rb
56
+ - lib/ruby_for_grafana_loki/version.rb
57
+ homepage: https://rubygems.org/gems/ruby_for_grafana_loki
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 2.7.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.4.10
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Ruby for Grafana Loki
80
+ test_files: []