kirei 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 788221745d889864fe2ec7075cce289f6d44361dece7711fbe91ac24c16f693b
4
- data.tar.gz: e03e3cf0c4c6db69bc0106107ccb002b8f472ba077ef457d88a84b92b188371f
3
+ metadata.gz: 86e3ce57ba0d801b5279632d302837a42195565c3daa66a2b5fbd6adb31f0176
4
+ data.tar.gz: a3ba957501c2603cc8cfba22282ab0c281ccf8227f99fedad78af0c980f3edd3
5
5
  SHA512:
6
- metadata.gz: 30bef2a1458e1aeeebd774d577df1dc231b0cfa46fb7483b076b69ac05de1529527442599dca18bb278fec42e1a17888eee849feb1b3f81dd502fd92284dd879
7
- data.tar.gz: 181df5735f94c222fc29e9bb3c18aa33340995925a324a18a0bb28e1001eaeaee24f8c7442cc5852adf31ae7e5e7a707743441e1c64a585e27039e7b50ba96da
6
+ metadata.gz: '078c2a848f346f8e25c0b9fcacc72d0d2e3b8f82620df2cffe4fee683e9d44aba43d3de635dd918449369617aa97362d2f66adba26d52c625565ff614eafe5d9'
7
+ data.tar.gz: a57e28a79ff8fe6bf47060550d11521b14112f22e15088a365bf7965ef0caa1a9f56a80e55571477cea8807c63775513fdd04eb63bda355f677ce5aba95d032d
@@ -15,6 +15,9 @@ module Cli
15
15
  # frozen_string_literal: true
16
16
 
17
17
  require "rake"
18
+ require_relative "app"
19
+
20
+ Dir.glob("#{Kirei::GEM_ROOT}/lib/tasks/**/*.rake").each { import(_1) }
18
21
 
19
22
  Dir.glob("lib/tasks/**/*.rake").each { import(_1) }
20
23
 
@@ -8,6 +8,8 @@ module Kirei
8
8
  class Base
9
9
  extend T::Sig
10
10
 
11
+ NOT_FOUND = T.let([404, {}, ["Not Found"]], RackResponseType) # rubocop:disable Style/MutableConstant
12
+
11
13
  sig { params(params: T::Hash[String, T.untyped]).void }
12
14
  def initialize(params: {})
13
15
  @router = T.let(Router.instance, Router)
@@ -23,6 +25,7 @@ module Kirei
23
25
  sig { params(env: RackEnvType).returns(RackResponseType) }
24
26
  def call(env)
25
27
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
28
+ status = 500 # we use it in the "ensure" block, so we need to define early (Sorbet doesn't like `status ||= 418`)
26
29
 
27
30
  http_verb = Verb.deserialize(env.fetch("REQUEST_METHOD"))
28
31
  req_path = T.cast(env.fetch("REQUEST_PATH"), String)
@@ -33,7 +36,7 @@ module Kirei
33
36
  #
34
37
 
35
38
  route = router.get(http_verb, req_path)
36
- return [404, {}, ["Not Found"]] if route.nil?
39
+ return NOT_FOUND if route.nil?
37
40
 
38
41
  params = case route.verb
39
42
  when Verb::GET
@@ -66,7 +69,13 @@ module Kirei
66
69
  Kirei::Logging::Logger.call(
67
70
  level: Kirei::Logging::Level::INFO,
68
71
  label: "Request Started",
69
- meta: params,
72
+ meta: {
73
+ "http.method" => route.verb.serialize,
74
+ "http.route" => route.path,
75
+ "http.host" => env.fetch("HTTP_HOST"),
76
+ "http.request_params" => params,
77
+ "http.client_ip" => env.fetch("CF-Connecting-IP", env.fetch("REMOTE_ADDR")),
78
+ },
70
79
  )
71
80
 
72
81
  statsd_timing_tags = {
@@ -101,7 +110,7 @@ module Kirei
101
110
  ::StatsD.measure("request", latency_in_ms, tags: statsd_timing_tags)
102
111
 
103
112
  Kirei::Logging::Logger.call(
104
- level: Kirei::Logging::Level::INFO,
113
+ level: status >= 500 ? Kirei::Logging::Level::ERROR : Kirei::Logging::Level::INFO,
105
114
  label: "Request Finished",
106
115
  meta: { "response.body" => response_body, "response.latency_in_ms" => latency_in_ms },
107
116
  )
@@ -10,12 +10,12 @@ module Kirei
10
10
  sig do
11
11
  type_parameters(:T)
12
12
  .params(
13
- class_name: String,
13
+ class_name: T.untyped,
14
14
  log_tags: T::Hash[String, T.untyped],
15
- _: T.proc.returns(T.type_parameter(:T)),
15
+ block: T.proc.returns(T.type_parameter(:T)),
16
16
  ).returns(T.type_parameter(:T))
17
17
  end
18
- def self.call(class_name, log_tags: {}, &_)
18
+ def self.call(class_name, log_tags: {}, &block)
19
19
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
20
20
  service = yield
21
21
 
@@ -35,13 +35,20 @@ module Kirei
35
35
  ::StatsD.measure(class_name, latency_in_ms, tags: metric_tags)
36
36
 
37
37
  logtags = {
38
- "service.name" => class_name,
38
+ "service.name" => class_name.to_s,
39
39
  "service.latency_in_ms" => latency_in_ms,
40
40
  "service.result" => result,
41
+ "service.source_location" => source_location(block),
41
42
  }
42
43
  logtags.merge!(log_tags)
44
+
43
45
  Logging::Logger.call(level: Logging::Level::INFO, label: "Service Finished", meta: logtags)
44
46
  end
47
+
48
+ sig { params(proc: T.proc.returns(T.untyped)).returns(String) }
49
+ private_class_method def self.source_location(proc)
50
+ proc.source_location.join(":").gsub(App.root.to_s, "")
51
+ end
45
52
  end
46
53
  end
47
54
  end
data/lib/kirei/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Kirei
5
- VERSION = "0.4.0"
5
+ VERSION = "0.4.1"
6
6
  end
@@ -0,0 +1,26 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ require_relative("../kirei")
5
+
6
+ namespace :kirei do
7
+ desc "Prints all available routes"
8
+ task :routes do
9
+ router = Kirei::Routing::Router.instance
10
+
11
+ longest_path = router.routes.keys.map(&:length).max
12
+
13
+ routes_by_controller = router.routes.values.group_by(&:controller)
14
+
15
+ puts "\n"
16
+
17
+ routes_by_controller.each do |controller, routes|
18
+ puts "#{controller}:"
19
+ routes.each do |route|
20
+ verb = route.verb.serialize.upcase.ljust(7 + 3) # 7 is the length of the longest verb
21
+ puts "#{verb} #{route.path.ljust(longest_path + 1)} => ##{route.action}"
22
+ end
23
+ puts "\n"
24
+ end
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kirei
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ludwig Reinmiedl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-19 00:00:00.000000000 Z
11
+ date: 2024-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -203,6 +203,7 @@ files:
203
203
  - lib/kirei/services/result.rb
204
204
  - lib/kirei/services/runner.rb
205
205
  - lib/kirei/version.rb
206
+ - lib/tasks/routes.rake
206
207
  - sorbet/rbi/shims/base_model.rbi
207
208
  - sorbet/rbi/shims/ruby.rbi
208
209
  homepage: https://github.com/swiknaba/kirei