google_cloud_logging_extension 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 44415cf20a26f3450f298d5071b7817126c693f0f2d43f2157124306d2169e06
4
+ data.tar.gz: 0be5fd106d23db06709a09ac284d5d1733c9fbcb04b6a0be23acc8a3a666fded
5
+ SHA512:
6
+ metadata.gz: 85350a0b3b8e487c8c516584cde20941fa9efe259c4df4e29b4d41638fb2d0e3d37dfdd7d40451bee0e0a5a265047003ecac1377c16f43a4a3e3dbdb388ed5c6
7
+ data.tar.gz: 1e42ab6bc5547c0d5cb2e1e0a5e58db9fc10f89766a3a4b12ee684b648989bc821c5638fdb009471bd3a8520dde0c5675d12ec47d08599c2a56c0f6a51891aeb
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in google_cloud_logging_extension.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ group :development, :test do
11
+ gem 'solargraph', require: false
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google_cloud_logging_extension (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ backport (1.2.0)
11
+ benchmark (0.2.0)
12
+ diff-lcs (1.5.0)
13
+ e2mmap (0.1.0)
14
+ jaro_winkler (1.5.4)
15
+ kramdown (2.4.0)
16
+ rexml
17
+ kramdown-parser-gfm (1.1.0)
18
+ kramdown (~> 2.0)
19
+ nokogiri (1.13.6-arm64-darwin)
20
+ racc (~> 1.4)
21
+ parallel (1.22.1)
22
+ parser (3.1.2.0)
23
+ ast (~> 2.4.1)
24
+ racc (1.6.0)
25
+ rainbow (3.1.1)
26
+ rake (13.0.6)
27
+ regexp_parser (2.5.0)
28
+ reverse_markdown (2.1.1)
29
+ nokogiri
30
+ rexml (3.2.5)
31
+ rubocop (1.30.0)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.1.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml (>= 3.2.5, < 4.0)
37
+ rubocop-ast (>= 1.18.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.18.0)
41
+ parser (>= 3.1.1.0)
42
+ ruby-progressbar (1.11.0)
43
+ solargraph (0.45.0)
44
+ backport (~> 1.2)
45
+ benchmark
46
+ bundler (>= 1.17.2)
47
+ diff-lcs (~> 1.4)
48
+ e2mmap
49
+ jaro_winkler (~> 1.5)
50
+ kramdown (~> 2.3)
51
+ kramdown-parser-gfm (~> 1.1)
52
+ parser (~> 3.0)
53
+ reverse_markdown (>= 1.0.5, < 3)
54
+ rubocop (>= 0.52)
55
+ thor (~> 1.0)
56
+ tilt (~> 2.0)
57
+ yard (~> 0.9, >= 0.9.24)
58
+ thor (1.2.1)
59
+ tilt (2.0.10)
60
+ unicode-display_width (2.1.0)
61
+ webrick (1.7.0)
62
+ yard (0.9.27)
63
+ webrick (~> 1.7.0)
64
+
65
+ PLATFORMS
66
+ arm64-darwin-21
67
+
68
+ DEPENDENCIES
69
+ google_cloud_logging_extension!
70
+ rake (~> 13.0)
71
+ solargraph
72
+
73
+ BUNDLED WITH
74
+ 2.3.9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ishotihadus
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # GoogleCloudLoggingExtension
2
+
3
+ Extension of google-cloud-logging gem.
4
+
5
+ This extension provides `Google::Cloud::Logging.create` method.
6
+
7
+ This gem also converts exceptions into json payload.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add google_cloud_logging_extension
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install google_cloud_logging_extension
18
+
19
+ ## Usage
20
+
21
+ ### Use default extension
22
+
23
+ ```ruby
24
+ require 'google_cloud_logging_extension'
25
+
26
+ logger = Google::Cloud::Logging.create('test')
27
+ # logger = Google::Cloud::Logging.create('test', 'gce_project', hoge: 'fuga')
28
+
29
+ logger.info('hoge')
30
+
31
+ begin
32
+ 5 / 0
33
+ rescue
34
+ logger.error($!)
35
+ end
36
+ ```
37
+
38
+ ### Use roda plugin
39
+
40
+ ```ruby
41
+ require 'google_cloud_logging_extension/roda_plugin'
42
+
43
+ class App < Roda
44
+ plugin :google_cloud_logging, log_name: 'name', labels: { hoge: 'fuga' }
45
+ end
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ishotihadus/google_cloud_logging_extension.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: %i[]
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/google_cloud_logging_extension/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'google_cloud_logging_extension'
7
+ spec.version = GoogleCloudLoggingExtension::VERSION
8
+ spec.authors = ['Ishotihadus']
9
+ spec.email = ['hanachan.pao@gmail.com']
10
+
11
+ spec.summary = 'Extension of google-cloud-logging-gem'
12
+ spec.description = 'Extension of google-cloud-logging-gem.'
13
+ spec.homepage = 'https://github.com/ishotihadus/google_cloud_logging_extension'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.6.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/ishotihadus/google_cloud_logging_extension'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) {|f| File.basename(f)}
29
+ spec.require_paths = ['lib']
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Google
4
+ module Cloud
5
+ module Logging
6
+ def self.create(log_name, resource_type = 'gce_project', **labels)
7
+ logging = Google::Cloud::Logging.new
8
+ writer = logging.async_writer(max_queue_size: 1000)
9
+ resource = logging.resource(resource_type)
10
+ if labels.key?(:level)
11
+ level = labels[:level]
12
+ labels.delete(:level)
13
+ end
14
+ logger = Google::Cloud::Logging::Logger.new(writer, log_name, resource, labels)
15
+ logger.level = level if level
16
+ logger
17
+ end
18
+
19
+ class Entry
20
+ # @param [Exception] exception
21
+ def create_exception_payload(exception)
22
+ {
23
+ class: exception.class.name,
24
+ message: exception.message,
25
+ backtrace: exception.backtrace,
26
+ cause: exception.cause.is_a?(Exception) ? create_exception_payload(exception.cause) : exception
27
+ }
28
+ end
29
+
30
+ def payload=(payload)
31
+ @payload = case payload
32
+ when Exception
33
+ create_exception_payload(payload)
34
+ else
35
+ payload
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,127 @@
1
+ # frozen-string-literal: true
2
+
3
+ require 'google/cloud/logging'
4
+
5
+ module Google
6
+ module Cloud
7
+ module Logging
8
+ class Logger
9
+ def write_entry_with_http_request(severity, message, http_request)
10
+ entry = Entry.new.tap do |e|
11
+ e.timestamp = Time.now
12
+ e.severity = gcloud_severity severity
13
+ e.payload = message
14
+ end
15
+ entry.instance_variable_set(:@http_request, http_request)
16
+
17
+ actual_log_name = log_name
18
+ info = request_info
19
+ if info
20
+ actual_log_name = info.log_name || actual_log_name
21
+ entry.trace = "projects/#{@project}/traces/#{info.trace_id}" unless info.trace_id.nil? || @project.nil?
22
+ entry.trace_sampled = info.trace_sampled if entry.trace_sampled.nil?
23
+ end
24
+
25
+ writer.write_entries entry, log_name: actual_log_name, resource:, labels: entry_labels(info)
26
+ end
27
+ end
28
+
29
+ class Entry
30
+ class HttpRequest
31
+ attr_accessor :server_ip, :latency, :protocol
32
+
33
+ def to_grpc
34
+ return nil if empty?
35
+
36
+ Google::Cloud::Logging::Type::HttpRequest.new(
37
+ request_method: request_method.to_s,
38
+ request_url: url.to_s,
39
+ request_size: size.to_i,
40
+ status: status.to_i,
41
+ response_size: response_size.to_i,
42
+ user_agent: user_agent.to_s,
43
+ remote_ip: remote_ip.to_s,
44
+ referer: referer.to_s,
45
+ server_ip: server_ip.to_s,
46
+ latency:,
47
+ protocol: protocol.to_s,
48
+ cache_hit: !(!cache_hit),
49
+ cache_validated_with_origin_server: !(!validated)
50
+ )
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ class Roda
59
+ module RodaPlugins
60
+ module GoogleCloudLogging
61
+ def self.configure(app, opts = {})
62
+ if opts[:writer].nil? && opts[:resource].nil?
63
+ logging = Google::Cloud::Logging.new
64
+ opts[:writer] = logging.async_writer(max_queue_size: opts[:max_queue_size] || 1000)
65
+ opts[:resource] = logging.resource(opts[:resource_id] || 'gce_project')
66
+ end
67
+ opts[:log_name] ||= 'roda'
68
+ logger = Google::Cloud::Logging::Logger.new(opts[:writer], opts[:log_name], opts[:resource], opts[:labels])
69
+ logger.level = opts[:level] if opts[:level]
70
+ app.opts[:google_cloud_logger] = logger
71
+ end
72
+
73
+ module InstanceMethods
74
+ def cloud_logging_set_payload(key, value)
75
+ @google_cloud_logging_custom_payload ||= {}
76
+ @google_cloud_logging_custom_payload[key] = value
77
+ end
78
+
79
+ def cloud_logging_set_severity(value)
80
+ @google_cloud_logging_severity = value
81
+ end
82
+
83
+ private
84
+
85
+ def _roda_before_00__google_cloud_logging
86
+ return unless Process.const_defined?(:CLOCK_MONOTONIC_RAW)
87
+
88
+ @google_cloud_logging_clock = Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW)
89
+ end
90
+
91
+ def _roda_after_99__google_cloud_logging(res)
92
+ server_ip = "#{env['SERVER_NAME']}:#{env['SERVER_PORT']}"
93
+ latency = @google_cloud_logging_clock &&
94
+ (Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW) - @google_cloud_logging_clock)
95
+ latency_fraction = latency - latency.to_i
96
+
97
+ latency_duration = Google::Protobuf::Duration.new(seconds: latency.to_i,
98
+ nanos: (latency_fraction * 1000 * 1000 * 1000).to_i)
99
+
100
+ http_request = Google::Cloud::Logging::Entry::HttpRequest.new
101
+ http_request.request_method = env['REQUEST_METHOD']
102
+ http_request.url = "#{env['rack.url_scheme']}://#{env['HTTP_HOST'] || server_ip}#{env['REQUEST_URI']}"
103
+ http_request.status = res[0]
104
+ http_request.user_agent = env['HTTP_USER_AGENT']
105
+ http_request.remote_ip = env['HTTP_X_REAL_IP'] || env['REMOTE_ADDR']
106
+ http_request.server_ip = server_ip
107
+ http_request.referer = env['HTTP_REFERER']
108
+ http_request.latency = latency_duration
109
+ http_request.protocol = env['SERVER_PROTOCOL']
110
+
111
+ severity = @google_cloud_logging_severity || (res[0] >= 500 ? ::Logger::ERROR : ::Logger::INFO)
112
+
113
+ message = "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']} #{env['SERVER_PROTOCOL']}"
114
+
115
+ payload = { message: }
116
+ payload.merge!(@google_cloud_logging_custom_payload) if @google_cloud_logging_custom_payload
117
+
118
+ opts[:google_cloud_logger]&.write_entry_with_http_request(severity, payload, http_request)
119
+ rescue StandardError
120
+ # pass
121
+ end
122
+ end
123
+ end
124
+
125
+ register_plugin(:google_cloud_logging, GoogleCloudLogging)
126
+ end
127
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GoogleCloudLoggingExtension
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'google_cloud_logging_extension/google/cloud/logging'
4
+ require_relative 'google_cloud_logging_extension/version'
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_cloud_logging_extension
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ishotihadus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Extension of google-cloud-logging-gem.
14
+ email:
15
+ - hanachan.pao@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - google_cloud_logging_extension.gemspec
26
+ - lib/google_cloud_logging_extension.rb
27
+ - lib/google_cloud_logging_extension/google/cloud/logging.rb
28
+ - lib/google_cloud_logging_extension/roda_plugin.rb
29
+ - lib/google_cloud_logging_extension/version.rb
30
+ homepage: https://github.com/ishotihadus/google_cloud_logging_extension
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/ishotihadus/google_cloud_logging_extension
35
+ source_code_uri: https://github.com/ishotihadus/google_cloud_logging_extension
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.3.3
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Extension of google-cloud-logging-gem
55
+ test_files: []