upfluence-utils 0.11.3 → 0.12.0
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/lib/upfluence/error_logger/null.rb +1 -1
- data/lib/upfluence/error_logger/sentry.rb +77 -27
- data/lib/upfluence/http/middleware/request_stapler.rb +16 -0
- data/lib/upfluence/http/server.rb +31 -12
- data/lib/upfluence/utils/version.rb +1 -1
- data/rbutils.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2c9dcbd049e1e8c27d2ec9f7075cccacb7f4814d23ea04def031a0f9f00957f
|
4
|
+
data.tar.gz: 1f6b5c5812beaccfbf78123004f503f9135c96aedef1097cede9df3f007c6e92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c9e85e446a91a43b0d71d855a3b5c8c601f84f77582b6f3f32c725eadc96682bed636d6987dbab210b98b64c437a92412df89e6ef4494a6b1b01d49e9aa4a79
|
7
|
+
data.tar.gz: 10f73661eccb80401be50c860b660b0be5707f60b9d864bbe6e49cd1e0e93a91ee1dc1fbcbdf99adb0f59936dcdd2115424657edb76d8345ab46c1dd95811232
|
@@ -1,66 +1,116 @@
|
|
1
|
-
require '
|
1
|
+
require 'sentry-ruby'
|
2
2
|
|
3
3
|
module Upfluence
|
4
4
|
module ErrorLogger
|
5
5
|
class Sentry
|
6
|
-
EXCLUDED_ERRORS = (
|
6
|
+
EXCLUDED_ERRORS = (::Sentry::Configuration::IGNORE_DEFAULT + ['Identity::Thrift::Forbidden'])
|
7
|
+
MAX_TAG_SIZE = 8 * 1024
|
7
8
|
|
8
9
|
def initialize
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
@tag_extractors = []
|
11
|
+
|
12
|
+
::Sentry.init do |config|
|
13
|
+
config.send_default_pii = true
|
14
|
+
config.dsn = ENV.fetch('SENTRY_DSN', nil)
|
15
|
+
config.environment = Upfluence.env
|
12
16
|
config.excluded_exceptions = EXCLUDED_ERRORS
|
13
17
|
config.logger = Upfluence.logger
|
14
|
-
config.release = "#{ENV
|
15
|
-
config.
|
16
|
-
|
17
|
-
|
18
|
-
|
18
|
+
config.release = "#{ENV.fetch('PROJECT_NAME', nil)}-#{ENV.fetch('SEMVER_VERSION', nil)}"
|
19
|
+
config.enable_tracing = false
|
20
|
+
config.auto_session_tracking = false
|
21
|
+
end
|
22
|
+
|
23
|
+
::Sentry.set_tags(
|
24
|
+
{ unit_name: unit_name, unit_type: unit_type }.select { |_, v| v }
|
25
|
+
)
|
26
|
+
|
27
|
+
::Sentry.with_scope do |scope|
|
28
|
+
scope.add_event_processor do |event, hint|
|
29
|
+
tags = @tag_extractors.map(&:extract).compact.reduce({}, &:merge)
|
30
|
+
|
31
|
+
exc = hint[:exception]
|
32
|
+
|
33
|
+
tags.merge!(exc.tags) if exc.respond_to? :tags
|
34
|
+
|
35
|
+
tx_name = transaction_name(tags)
|
36
|
+
|
37
|
+
event.transaction = tx_name if tx_name
|
38
|
+
event.extra.merge!(prepare_extra(tags))
|
39
|
+
|
40
|
+
event
|
41
|
+
end
|
19
42
|
end
|
20
43
|
end
|
21
44
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
)
|
29
|
-
|
30
|
-
|
45
|
+
def append_tag_extractors(klass)
|
46
|
+
@tag_extractors << klass
|
47
|
+
end
|
48
|
+
|
49
|
+
def notify(error, *args)
|
50
|
+
::Sentry.with_scope do |scope|
|
51
|
+
context = args.reduce({}) do |acc, arg|
|
52
|
+
v = if arg.is_a?(Hash)
|
53
|
+
arg
|
54
|
+
else
|
55
|
+
key = acc.empty? ? 'method' : "arg_#{acc.length}"
|
56
|
+
{ key => arg.inspect }
|
57
|
+
end
|
58
|
+
|
59
|
+
acc.merge(v)
|
60
|
+
end
|
61
|
+
|
62
|
+
scope.set_extras(prepare_extra(context))
|
63
|
+
|
64
|
+
::Sentry.capture_exception(error)
|
31
65
|
end
|
66
|
+
rescue ::Sentry::Error => e
|
67
|
+
Upfluence.logger.warning e.message
|
32
68
|
end
|
33
69
|
|
34
70
|
def user=(user)
|
35
|
-
|
71
|
+
::Sentry.set_user(id: user.id, email: user.email)
|
36
72
|
end
|
37
73
|
|
38
74
|
def middleware
|
39
|
-
::
|
75
|
+
::Sentry::Rack::CaptureExceptions
|
40
76
|
end
|
41
77
|
|
42
78
|
def ignore_exception(*klss)
|
43
79
|
klss.each do |kls|
|
44
|
-
puts kls
|
45
80
|
case kls.class
|
46
81
|
when Class
|
47
|
-
|
82
|
+
::Sentry.configuration.excluded_exceptions << kls.name
|
48
83
|
when String
|
49
|
-
|
84
|
+
::Sentry.configuration.excluded_exceptions << kls
|
50
85
|
else
|
51
|
-
Upfluence.logger.warn
|
86
|
+
Upfluence.logger.warn "Unexcepted argument for ignore_exception #{kls}"
|
52
87
|
end
|
53
88
|
end
|
54
89
|
end
|
55
90
|
|
56
91
|
private
|
57
92
|
|
93
|
+
def prepare_extra(tags)
|
94
|
+
tags.select { |_k, v| v.respond_to?(:size) && v.size < MAX_TAG_SIZE }
|
95
|
+
end
|
96
|
+
|
97
|
+
def transaction_name(tags)
|
98
|
+
return tags['transaction'] if tags['transaction']
|
99
|
+
|
100
|
+
svc = tags['thrift.request.service']
|
101
|
+
mth = tags['thrift.request.method']
|
102
|
+
|
103
|
+
return "#{svc}##{mth}" if svc && mth
|
104
|
+
|
105
|
+
nil
|
106
|
+
end
|
107
|
+
|
58
108
|
def unit_name
|
59
|
-
ENV['UNIT_NAME']
|
109
|
+
ENV['UNIT_NAME']&.split('.')&.first
|
60
110
|
end
|
61
111
|
|
62
112
|
def unit_type
|
63
|
-
unit_name
|
113
|
+
unit_name&.split('@')&.first
|
64
114
|
end
|
65
115
|
end
|
66
116
|
end
|
@@ -14,23 +14,26 @@ require 'upfluence/http/middleware/application_headers'
|
|
14
14
|
require 'upfluence/http/middleware/handle_exception'
|
15
15
|
require 'upfluence/http/middleware/prometheus'
|
16
16
|
require 'upfluence/http/middleware/cors'
|
17
|
+
require 'upfluence/http/middleware/request_stapler'
|
17
18
|
|
18
19
|
module Upfluence
|
19
20
|
module HTTP
|
20
21
|
class Server
|
22
|
+
REQUEST_CONTEXT_KEY = :uhtt_request_context
|
23
|
+
DEFAULT_MIDDLEWARES = []
|
21
24
|
DEFAULT_OPTIONS = {
|
22
|
-
server:
|
23
|
-
Port:
|
24
|
-
Host:
|
25
|
-
threaded:
|
26
|
-
interfaces:
|
27
|
-
push_gateway_url:
|
25
|
+
server: :puma,
|
26
|
+
Port: ENV.fetch('PORT', 8080),
|
27
|
+
Host: '0.0.0.0',
|
28
|
+
threaded: true,
|
29
|
+
interfaces: [],
|
30
|
+
push_gateway_url: ENV.fetch('PUSH_GATEWAY_URL', nil),
|
28
31
|
push_gateway_interval: 15, # sec
|
29
|
-
app_name:
|
30
|
-
unit_name:
|
31
|
-
base_processor_klass:
|
32
|
-
base_handler_klass:
|
33
|
-
debug:
|
32
|
+
app_name: ENV.fetch('APP_NAME', 'uhttp-rb-server'),
|
33
|
+
unit_name: ENV.fetch('UNIT_NAME','uhttp-rb-server-anonymous'),
|
34
|
+
base_processor_klass: nil,
|
35
|
+
base_handler_klass: nil,
|
36
|
+
debug: ENV.fetch('DEBUG', nil)
|
34
37
|
}
|
35
38
|
|
36
39
|
def initialize(options = {}, &block)
|
@@ -43,6 +46,7 @@ module Upfluence
|
|
43
46
|
end
|
44
47
|
|
45
48
|
@builder = Builder.new do
|
49
|
+
use Middleware::RequestStapler
|
46
50
|
use Middleware::Logger
|
47
51
|
use Middleware::Prometheus
|
48
52
|
use Middleware::ApplicationHeaders, base_handler
|
@@ -55,6 +59,11 @@ module Upfluence
|
|
55
59
|
use Rack::ETag
|
56
60
|
use Middleware::CORS if Upfluence.env.development?
|
57
61
|
|
62
|
+
DEFAULT_MIDDLEWARES.each do |m|
|
63
|
+
m = [m] unless m.is_a?(Array)
|
64
|
+
use(*m)
|
65
|
+
end
|
66
|
+
|
58
67
|
map '/healthcheck' do
|
59
68
|
run(opts[:healthcheck_endpoint] || Endpoint::Healthcheck.new)
|
60
69
|
end
|
@@ -87,6 +96,16 @@ module Upfluence
|
|
87
96
|
end
|
88
97
|
end
|
89
98
|
|
99
|
+
class << self
|
100
|
+
def request
|
101
|
+
Thread.current[REQUEST_CONTEXT_KEY]
|
102
|
+
end
|
103
|
+
|
104
|
+
def request=(req)
|
105
|
+
Thread.current[REQUEST_CONTEXT_KEY] = req
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
90
109
|
private
|
91
110
|
|
92
111
|
def run_prometheus_exporter
|
@@ -101,7 +120,7 @@ module Upfluence
|
|
101
120
|
|
102
121
|
begin
|
103
122
|
push.replace Prometheus::Client.registry
|
104
|
-
rescue => e
|
123
|
+
rescue StandardError => e
|
105
124
|
Upfluence.error_logger.notify(e)
|
106
125
|
end
|
107
126
|
end
|
data/rbutils.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.add_runtime_dependency 'upfluence-thrift'
|
24
24
|
spec.add_runtime_dependency 'sinatra'
|
25
25
|
spec.add_runtime_dependency 'redis'
|
26
|
-
spec.add_runtime_dependency 'sentry-
|
26
|
+
spec.add_runtime_dependency 'sentry-ruby'
|
27
27
|
spec.add_runtime_dependency 'sinatra-contrib'
|
28
28
|
spec.add_runtime_dependency 'activesupport'
|
29
29
|
spec.add_runtime_dependency 'puma'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: upfluence-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Upfluence
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,7 +95,7 @@ dependencies:
|
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
|
-
name: sentry-
|
98
|
+
name: sentry-ruby
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
@@ -267,6 +267,7 @@ files:
|
|
267
267
|
- lib/upfluence/http/middleware/handle_exception.rb
|
268
268
|
- lib/upfluence/http/middleware/logger.rb
|
269
269
|
- lib/upfluence/http/middleware/prometheus.rb
|
270
|
+
- lib/upfluence/http/middleware/request_stapler.rb
|
270
271
|
- lib/upfluence/http/server.rb
|
271
272
|
- lib/upfluence/logger.rb
|
272
273
|
- lib/upfluence/mixin/html_scrubbing.rb
|