grafana_annotations 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.bumpversion.cfg +14 -0
- data/.github/workflows/ci.yml +25 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.rubocop.yml +19 -0
- data/.tool-versions +1 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +109 -0
- data/LICENSE +21 -0
- data/README.md +87 -0
- data/Rakefile +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/grafana_annotations.gemspec +40 -0
- data/lib/grafana_annotations/annotation.rb +12 -0
- data/lib/grafana_annotations/api_client.rb +86 -0
- data/lib/grafana_annotations/config/dsl.rb +22 -0
- data/lib/grafana_annotations/config.rb +12 -0
- data/lib/grafana_annotations/patch_annotation_request.rb +10 -0
- data/lib/grafana_annotations/rake.rb +24 -0
- data/lib/grafana_annotations/types.rb +9 -0
- data/lib/grafana_annotations/utils/time.rb +11 -0
- data/lib/grafana_annotations/version.rb +5 -0
- data/lib/grafana_annotations/wrap.rb +50 -0
- data/lib/grafana_annotations.rb +52 -0
- metadata +185 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6b8f824ed8c6d9b1a0c170564819962895aa7b1e28b205943f10c50747d60742
|
4
|
+
data.tar.gz: 3f8244937b2ee09e8f1d7eec67f553f51c0165257f68bcabe70d1c45783a859b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d4aa25b46e25cb0bb08d60459c3fbbdfaf11f8a19a2c5f64e3e0c039eeb7276d5907868748bfdc15504f1a8c723cd1c7862daf672a24436f166af63f516ce238
|
7
|
+
data.tar.gz: 20037e0fbd7e636c1b341991e270ef0c9808de853e9e79b13480a3b926aa2259a1e2895170d217d4be63f64e72e887796946dd1dac8d40e889af328c98e15473
|
data/.bumpversion.cfg
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
[bumpversion]
|
2
|
+
current_version = 0.1.0
|
3
|
+
commit = True
|
4
|
+
tag = True
|
5
|
+
tag_name = {new_version}
|
6
|
+
message = bump version {current_version} → {new_version}
|
7
|
+
|
8
|
+
[bumpversion:file:lib/grafana_annotations/version.rb]
|
9
|
+
|
10
|
+
[bumpversion:file:README.md]
|
11
|
+
|
12
|
+
[bumpversion:file:Gemfile.lock]
|
13
|
+
search = http_health_check ({current_version})
|
14
|
+
replace = http_health_check ({new_version})
|
@@ -0,0 +1,25 @@
|
|
1
|
+
name: ci
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [main]
|
6
|
+
pull_request:
|
7
|
+
branches: [main]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
ruby: ["2.5", "2.6", "2.7", "3.0"]
|
15
|
+
steps:
|
16
|
+
- uses: actions/checkout@v3
|
17
|
+
- name: Set up Ruby
|
18
|
+
uses: ruby/setup-ruby@v1
|
19
|
+
with:
|
20
|
+
bundler-cache: true
|
21
|
+
ruby-version: ${{ matrix.ruby }}
|
22
|
+
- name: Run all tests
|
23
|
+
run: bundle exec rspec
|
24
|
+
- name: Run rubocop
|
25
|
+
run: bundle exec rubocop
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
|
4
|
+
Style/Documentation:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Metrics/BlockLength:
|
8
|
+
Exclude:
|
9
|
+
- "spec/**/*.rb"
|
10
|
+
- "grafana_annotations.gemspec"
|
11
|
+
|
12
|
+
Style/CommentedKeyword:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Layout/ArgumentAlignment:
|
16
|
+
EnforcedStyle: with_fixed_indentation
|
17
|
+
|
18
|
+
Layout/ParameterAlignment:
|
19
|
+
EnforcedStyle: with_fixed_indentation
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 2.5.8
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
grafana_annotations (http_health_check (0.1.0))
|
5
|
+
dry-initializer
|
6
|
+
dry-monads
|
7
|
+
dry-struct
|
8
|
+
dry-types
|
9
|
+
faraday (~> 1.0)
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
addressable (2.7.0)
|
15
|
+
public_suffix (>= 2.0.2, < 5.0)
|
16
|
+
ast (2.4.2)
|
17
|
+
concurrent-ruby (1.1.10)
|
18
|
+
crack (0.4.3)
|
19
|
+
safe_yaml (~> 1.0.0)
|
20
|
+
diff-lcs (1.5.0)
|
21
|
+
dry-configurable (0.12.0)
|
22
|
+
concurrent-ruby (~> 1.0)
|
23
|
+
dry-core (~> 0.5, >= 0.5.0)
|
24
|
+
dry-container (0.7.2)
|
25
|
+
concurrent-ruby (~> 1.0)
|
26
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
27
|
+
dry-core (0.5.0)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
dry-equalizer (0.3.0)
|
30
|
+
dry-inflector (0.2.0)
|
31
|
+
dry-initializer (3.0.4)
|
32
|
+
dry-logic (1.1.0)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
dry-core (~> 0.5, >= 0.5)
|
35
|
+
dry-monads (1.3.5)
|
36
|
+
concurrent-ruby (~> 1.0)
|
37
|
+
dry-core (~> 0.4, >= 0.4.4)
|
38
|
+
dry-equalizer
|
39
|
+
dry-struct (1.4.0)
|
40
|
+
dry-core (~> 0.5, >= 0.5)
|
41
|
+
dry-types (~> 1.5)
|
42
|
+
ice_nine (~> 0.11)
|
43
|
+
dry-types (1.5.0)
|
44
|
+
concurrent-ruby (~> 1.0)
|
45
|
+
dry-container (~> 0.3)
|
46
|
+
dry-core (~> 0.5, >= 0.5)
|
47
|
+
dry-inflector (~> 0.1, >= 0.1.2)
|
48
|
+
dry-logic (~> 1.0, >= 1.0.2)
|
49
|
+
faraday (1.3.0)
|
50
|
+
faraday-net_http (~> 1.0)
|
51
|
+
multipart-post (>= 1.2, < 3)
|
52
|
+
ruby2_keywords
|
53
|
+
faraday-net_http (1.0.1)
|
54
|
+
hashdiff (0.4.0)
|
55
|
+
ice_nine (0.11.2)
|
56
|
+
multipart-post (2.1.1)
|
57
|
+
parallel (1.22.1)
|
58
|
+
parser (3.1.2.0)
|
59
|
+
ast (~> 2.4.1)
|
60
|
+
public_suffix (4.0.6)
|
61
|
+
rainbow (3.1.1)
|
62
|
+
rake (13.0.6)
|
63
|
+
regexp_parser (2.5.0)
|
64
|
+
rexml (3.2.5)
|
65
|
+
rspec (3.11.0)
|
66
|
+
rspec-core (~> 3.11.0)
|
67
|
+
rspec-expectations (~> 3.11.0)
|
68
|
+
rspec-mocks (~> 3.11.0)
|
69
|
+
rspec-core (3.11.0)
|
70
|
+
rspec-support (~> 3.11.0)
|
71
|
+
rspec-expectations (3.11.0)
|
72
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
73
|
+
rspec-support (~> 3.11.0)
|
74
|
+
rspec-mocks (3.11.1)
|
75
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
76
|
+
rspec-support (~> 3.11.0)
|
77
|
+
rspec-support (3.11.0)
|
78
|
+
rubocop (0.93.1)
|
79
|
+
parallel (~> 1.10)
|
80
|
+
parser (>= 2.7.1.5)
|
81
|
+
rainbow (>= 2.2.2, < 4.0)
|
82
|
+
regexp_parser (>= 1.8)
|
83
|
+
rexml
|
84
|
+
rubocop-ast (>= 0.6.0)
|
85
|
+
ruby-progressbar (~> 1.7)
|
86
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
87
|
+
rubocop-ast (1.17.0)
|
88
|
+
parser (>= 3.1.1.0)
|
89
|
+
ruby-progressbar (1.11.0)
|
90
|
+
ruby2_keywords (0.0.4)
|
91
|
+
safe_yaml (1.0.5)
|
92
|
+
unicode-display_width (1.8.0)
|
93
|
+
webmock (3.3.0)
|
94
|
+
addressable (>= 2.3.6)
|
95
|
+
crack (>= 0.3.2)
|
96
|
+
hashdiff
|
97
|
+
|
98
|
+
PLATFORMS
|
99
|
+
x86_64-darwin-21
|
100
|
+
|
101
|
+
DEPENDENCIES
|
102
|
+
grafana_annotations!
|
103
|
+
rake (~> 13.0)
|
104
|
+
rspec (~> 3.2)
|
105
|
+
rubocop (~> 0.81)
|
106
|
+
webmock
|
107
|
+
|
108
|
+
BUNDLED WITH
|
109
|
+
2.3.15
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 SberMarket Tech
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# GrafanaAnnotations
|
2
|
+
|
3
|
+
Add custom annotations to grafana from your ruby application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'grafana_annotations', '~> 0.1.0'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install grafana_annotations
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Configuration
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
GrafanaAnnotations.configure do |c|
|
27
|
+
# Logs requests and responses. Optional.
|
28
|
+
c.logger Rails.logger
|
29
|
+
|
30
|
+
# Tags for rake tasks instrumentation. Empty by default.
|
31
|
+
c.rake_tags [:my_app, :rake]
|
32
|
+
|
33
|
+
# Rake task annotation prefix
|
34
|
+
c.rake_text_prefix 'Rake task'
|
35
|
+
|
36
|
+
# URL to your grafana installation.
|
37
|
+
c.grafana_base_url ENV.fetch('GRAFANA_URL')
|
38
|
+
|
39
|
+
# Grafana authorization (i.e. `Bearer xxxxx`)
|
40
|
+
c.grafana_authorization ENV.fetch('GRAFANA_AUTHZ')
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
### Creating annotations
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
result = GrafanaAnnotations.default_api_client.create(
|
48
|
+
time: GrafanaAnnotations::Utils::Time.now_ms, # timestamp in milliseconds (integer)
|
49
|
+
tags: [:my_app, :my_event],
|
50
|
+
text: "Boom!"
|
51
|
+
)
|
52
|
+
```
|
53
|
+
|
54
|
+
### Creating annotation span
|
55
|
+
|
56
|
+
`wrap` utility function creates an annotation when block starts executing and updates it with end time after.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
GrafanaAnnotations.wrap(text: 'something is happening', tags: [:my_app, :something]) do
|
60
|
+
do_something()
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
### Rake tasks instrumentation
|
65
|
+
|
66
|
+
Just require `grafana_annotations/rake` within your Rakefile and annotation request will be sent for every rake task.
|
67
|
+
```ruby
|
68
|
+
# Rakefile
|
69
|
+
require 'grafana_annotations/rake'
|
70
|
+
```
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
75
|
+
|
76
|
+
## Deployment
|
77
|
+
|
78
|
+
1. Update changelog and git add it
|
79
|
+
2.
|
80
|
+
|
81
|
+
```sh
|
82
|
+
bump2version patch --allow-dirty
|
83
|
+
```
|
84
|
+
|
85
|
+
3. git push && git push --tags
|
86
|
+
4. gem build
|
87
|
+
5. gem push grafana_annotations-x.x.x.gem
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'grafana_annotations'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/grafana_annotations/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'grafana_annotations'
|
7
|
+
spec.version = GrafanaAnnotations::VERSION
|
8
|
+
spec.licenses = ['MIT']
|
9
|
+
spec.authors = ['SberMarket Team']
|
10
|
+
spec.email = ['nikita.babushkin@sbermarket.ru']
|
11
|
+
|
12
|
+
spec.summary = 'Utilities for creating grafana annotations from your ruby application.'
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = 'https://github.com/SberMarket-Tech/grafana_annotations'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
16
|
+
|
17
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
18
|
+
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/SberMarket-Tech/grafana_annotations'
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/SberMarket-Tech/grafana_annotations/blob/main/CHANGELOG.md'
|
22
|
+
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
# TODO: Get rid of dry.rb and faraday dependencies
|
31
|
+
spec.add_dependency 'dry-initializer'
|
32
|
+
spec.add_dependency 'dry-monads'
|
33
|
+
spec.add_dependency 'dry-struct'
|
34
|
+
spec.add_dependency 'dry-types'
|
35
|
+
spec.add_dependency 'faraday', '~> 1.0'
|
36
|
+
|
37
|
+
spec.add_development_dependency 'rspec', '~> 3.2'
|
38
|
+
spec.add_development_dependency 'rubocop', '~> 0.81'
|
39
|
+
spec.add_development_dependency 'webmock'
|
40
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaAnnotations
|
4
|
+
class Annotation < Dry::Struct
|
5
|
+
attribute? :dashboardUID, Types::Strict::String.optional
|
6
|
+
attribute? :panelId, Types::Strict::String.optional
|
7
|
+
attribute :time, Types::Strict::Integer
|
8
|
+
attribute? :timeEnd, Types::Strict::Integer
|
9
|
+
attribute :tags, Types::Strict::Array.of(Types::Coercible::String)
|
10
|
+
attribute :text, Types::Strict::String
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
require 'dry/monads/result'
|
5
|
+
require 'dry/initializer'
|
6
|
+
require 'faraday'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
module GrafanaAnnotations
|
10
|
+
#
|
11
|
+
# Implements Grafana annotations API client
|
12
|
+
# https://grafana.com/docs/grafana/latest/developers/http_api/annotations
|
13
|
+
#
|
14
|
+
class ApiClient
|
15
|
+
include Dry::Monads::Result::Mixin
|
16
|
+
extend Dry::Initializer
|
17
|
+
|
18
|
+
option :base_url
|
19
|
+
option :authorization, default: proc { nil }
|
20
|
+
option :logger, default: proc { nil }
|
21
|
+
|
22
|
+
# @param [Annotation,Hash] annotation
|
23
|
+
# @return [Success<Integer>, Failure<Symbol>]
|
24
|
+
def create(annotation)
|
25
|
+
annotation = GrafanaAnnotations::Annotation.new(annotation)
|
26
|
+
|
27
|
+
result = with_error_handler { connection.post('/api/annotations', annotation.to_json) }
|
28
|
+
result.bind do |resp|
|
29
|
+
parse_json_body(resp.body).bind do |resp_body|
|
30
|
+
annotation_id = resp_body['id']
|
31
|
+
annotation_id = nil if annotation_id == ''
|
32
|
+
|
33
|
+
annotation_id ? Success(annotation_id) : Failure(:invalid_response_format)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param [Integer] id
|
39
|
+
# @param [Hash] params
|
40
|
+
# @return [Success<Integer>, Failure<Symbol>]
|
41
|
+
def patch(id, params)
|
42
|
+
result = with_error_handler do
|
43
|
+
struct = PatchAnnotationRequest.new(params)
|
44
|
+
return Failure(:empty_changeset) if struct.to_h.empty?
|
45
|
+
|
46
|
+
connection.patch("/api/annotations/#{id}", struct.to_h.to_json)
|
47
|
+
end
|
48
|
+
|
49
|
+
result.bind { Success(id) }
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def parse_json_body(body)
|
55
|
+
Success(JSON.parse(body))
|
56
|
+
rescue JSON::ParseError
|
57
|
+
Failure(:invalid_response_format)
|
58
|
+
end
|
59
|
+
|
60
|
+
def with_error_handler
|
61
|
+
resp = yield
|
62
|
+
return Failure(:"http_error_#{resp.status}") unless resp.success?
|
63
|
+
|
64
|
+
Success(resp)
|
65
|
+
rescue Faraday::ParsingError => e
|
66
|
+
Failure(:"http_error_#{e.response&.status}")
|
67
|
+
rescue Faraday::TimeoutError
|
68
|
+
Failure(:timeout)
|
69
|
+
rescue Faraday::ConnectionFailed
|
70
|
+
Failure(:connection_failed)
|
71
|
+
end
|
72
|
+
|
73
|
+
def connection
|
74
|
+
@connection ||= Faraday.new(url: base_url, headers: headers) do |conn|
|
75
|
+
conn.response :logger, logger, headers: logger.debug?, bodies: logger.debug? if logger
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def headers
|
80
|
+
{
|
81
|
+
'Content-Type' => 'application/json',
|
82
|
+
'Authorization' => authorization
|
83
|
+
}.compact
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaAnnotations
|
4
|
+
class Config
|
5
|
+
class Dsl
|
6
|
+
def initialize
|
7
|
+
@config = {}
|
8
|
+
end
|
9
|
+
attr_reader :config
|
10
|
+
|
11
|
+
%i[logger rake_tags grafana_base_url grafana_authorization rake_text_prefix].each do |meth|
|
12
|
+
define_method(meth) do |value|
|
13
|
+
@config[meth] = value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def faraday_config(&block)
|
18
|
+
@config[:faraday_config] = block
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaAnnotations
|
4
|
+
class Config < Dry::Struct
|
5
|
+
attribute(:grafana_base_url, Types::Strict::String)
|
6
|
+
attribute?(:grafana_authorization, Types::Strict::String.optional)
|
7
|
+
attribute(:logger, Types::Nominal::Any.default { Logger.new(IO::NULL) })
|
8
|
+
attribute(:faraday_config, Types::Nominal::Any.default { ->(_c) {} })
|
9
|
+
attribute(:rake_tags, Types::Strict::Array.of(Types::Coercible::String).default { [] })
|
10
|
+
attribute(:rake_text_prefix, Types::Strict::String.default('Rake task'))
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaAnnotations
|
4
|
+
class PatchAnnotationRequest < Dry::Struct
|
5
|
+
attribute? :time, Types::Strict::Integer
|
6
|
+
attribute? :timeEnd, Types::Strict::Integer
|
7
|
+
attribute? :tags, Types::Strict::Array.of(Types::Coercible::String)
|
8
|
+
attribute? :text, Types::Strict::String
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaAnnotations
|
4
|
+
module Rake
|
5
|
+
module Task
|
6
|
+
def invoke(*args)
|
7
|
+
return super unless GrafanaAnnotations.config
|
8
|
+
|
9
|
+
text = [GrafanaAnnotations.config.rake_text_prefix, name].join(' ')
|
10
|
+
text += " #{args.inspect}" unless args.empty?
|
11
|
+
|
12
|
+
::GrafanaAnnotations.wrap(text: text, tags: GrafanaAnnotations.config.rake_tags) do
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Rake
|
21
|
+
class Task
|
22
|
+
prepend(::GrafanaAnnotations::Rake::Task)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GrafanaAnnotations
|
4
|
+
module Wrap
|
5
|
+
def wrap(api_client: nil, text:, tags:, ok_tag: :ok, error_tag: :error) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
6
|
+
return yield if GrafanaAnnotations.config.nil? && api_client.nil?
|
7
|
+
|
8
|
+
api_client ||= GrafanaAnnotations.default_api_client
|
9
|
+
res = api_client.create(time: Utils::Time.now_ms, tags: tags, text: text)
|
10
|
+
maybe_log_result(res, 'sending grafana annotation')
|
11
|
+
|
12
|
+
begin
|
13
|
+
block_result = yield
|
14
|
+
maybe_patch_annotation(api_client, res,
|
15
|
+
text: "#{text} ok",
|
16
|
+
tags: tags.push(ok_tag))
|
17
|
+
|
18
|
+
block_result
|
19
|
+
rescue StandardError => e
|
20
|
+
maybe_patch_annotation(api_client, res,
|
21
|
+
text: "#{text} #{e.class.name} #{e.message}",
|
22
|
+
tags: tags.push(error_tag))
|
23
|
+
|
24
|
+
raise
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def maybe_patch_annotation(api_client, create_res, opts)
|
31
|
+
return unless create_res.success?
|
32
|
+
|
33
|
+
api_client.patch(create_res.value!, opts.merge(timeEnd: Utils::Time.now_ms)).tap do |result|
|
34
|
+
maybe_log_result(
|
35
|
+
result,
|
36
|
+
"patching grafana annotation #{create_res.value!} with #{opts[:text]}"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def maybe_log_result(result, msg)
|
42
|
+
return unless GrafanaAnnotations.config&.logger
|
43
|
+
|
44
|
+
prefix = result.success? ? 'Ok' : 'Error'
|
45
|
+
GrafanaAnnotations.config.logger.debug(
|
46
|
+
[prefix, msg, result.failure].compact.join(' ')
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/struct'
|
4
|
+
|
5
|
+
require_relative 'grafana_annotations/version'
|
6
|
+
require_relative 'grafana_annotations/utils/time'
|
7
|
+
|
8
|
+
require_relative 'grafana_annotations/types'
|
9
|
+
require_relative 'grafana_annotations/config'
|
10
|
+
require_relative 'grafana_annotations/config/dsl'
|
11
|
+
require_relative 'grafana_annotations/annotation'
|
12
|
+
require_relative 'grafana_annotations/patch_annotation_request'
|
13
|
+
require_relative 'grafana_annotations/api_client'
|
14
|
+
require_relative 'grafana_annotations/wrap'
|
15
|
+
|
16
|
+
module GrafanaAnnotations
|
17
|
+
class Error < StandardError; end
|
18
|
+
class ConfigurationError < Error; end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
attr_reader :config
|
22
|
+
include GrafanaAnnotations::Wrap
|
23
|
+
|
24
|
+
def configure
|
25
|
+
dsl = GrafanaAnnotations::Config::Dsl.new
|
26
|
+
yield dsl
|
27
|
+
@config = GrafanaAnnotations::Config.new(dsl.config)
|
28
|
+
rescue StandardError => e
|
29
|
+
raise ConfigurationError, e.message
|
30
|
+
end
|
31
|
+
|
32
|
+
def default_api_client
|
33
|
+
@default_api_client ||= begin
|
34
|
+
raise ConfigurationError, 'todo' unless config
|
35
|
+
|
36
|
+
new_api_client({})
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def new_api_client(opts)
|
41
|
+
return ApiClient.new(opts) unless config
|
42
|
+
|
43
|
+
ApiClient.new(
|
44
|
+
opts.merge(
|
45
|
+
logger: config.logger,
|
46
|
+
base_url: config.grafana_base_url,
|
47
|
+
authorization: config.grafana_authorization
|
48
|
+
)
|
49
|
+
)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grafana_annotations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SberMarket Team
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-initializer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-monads
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-struct
|
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'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: dry-types
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faraday
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.2'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.81'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.81'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Utilities for creating grafana annotations from your ruby application.
|
126
|
+
email:
|
127
|
+
- nikita.babushkin@sbermarket.ru
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".bumpversion.cfg"
|
133
|
+
- ".github/workflows/ci.yml"
|
134
|
+
- ".gitignore"
|
135
|
+
- ".rspec"
|
136
|
+
- ".rubocop.yml"
|
137
|
+
- ".tool-versions"
|
138
|
+
- CHANGELOG.md
|
139
|
+
- Gemfile
|
140
|
+
- Gemfile.lock
|
141
|
+
- LICENSE
|
142
|
+
- README.md
|
143
|
+
- Rakefile
|
144
|
+
- bin/console
|
145
|
+
- bin/setup
|
146
|
+
- grafana_annotations.gemspec
|
147
|
+
- lib/grafana_annotations.rb
|
148
|
+
- lib/grafana_annotations/annotation.rb
|
149
|
+
- lib/grafana_annotations/api_client.rb
|
150
|
+
- lib/grafana_annotations/config.rb
|
151
|
+
- lib/grafana_annotations/config/dsl.rb
|
152
|
+
- lib/grafana_annotations/patch_annotation_request.rb
|
153
|
+
- lib/grafana_annotations/rake.rb
|
154
|
+
- lib/grafana_annotations/types.rb
|
155
|
+
- lib/grafana_annotations/utils/time.rb
|
156
|
+
- lib/grafana_annotations/version.rb
|
157
|
+
- lib/grafana_annotations/wrap.rb
|
158
|
+
homepage: https://github.com/SberMarket-Tech/grafana_annotations
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata:
|
162
|
+
allowed_push_host: https://rubygems.org
|
163
|
+
homepage_uri: https://github.com/SberMarket-Tech/grafana_annotations
|
164
|
+
source_code_uri: https://github.com/SberMarket-Tech/grafana_annotations
|
165
|
+
changelog_uri: https://github.com/SberMarket-Tech/grafana_annotations/blob/main/CHANGELOG.md
|
166
|
+
post_install_message:
|
167
|
+
rdoc_options: []
|
168
|
+
require_paths:
|
169
|
+
- lib
|
170
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: 2.5.0
|
175
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '0'
|
180
|
+
requirements: []
|
181
|
+
rubygems_version: 3.2.3
|
182
|
+
signing_key:
|
183
|
+
specification_version: 4
|
184
|
+
summary: Utilities for creating grafana annotations from your ruby application.
|
185
|
+
test_files: []
|