google-cloud-logging 2.0.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 +7 -0
- data/.yardopts +18 -0
- data/AUTHENTICATION.md +178 -0
- data/CHANGELOG.md +407 -0
- data/CODE_OF_CONDUCT.md +40 -0
- data/CONTRIBUTING.md +188 -0
- data/INSTRUMENTATION.md +71 -0
- data/LICENSE +201 -0
- data/LOGGING.md +32 -0
- data/OVERVIEW.md +321 -0
- data/TROUBLESHOOTING.md +31 -0
- data/lib/google-cloud-logging.rb +161 -0
- data/lib/google/cloud/logging.rb +188 -0
- data/lib/google/cloud/logging/async_writer.rb +513 -0
- data/lib/google/cloud/logging/convert.rb +70 -0
- data/lib/google/cloud/logging/credentials.rb +44 -0
- data/lib/google/cloud/logging/entry.rb +528 -0
- data/lib/google/cloud/logging/entry/http_request.rb +167 -0
- data/lib/google/cloud/logging/entry/list.rb +178 -0
- data/lib/google/cloud/logging/entry/operation.rb +91 -0
- data/lib/google/cloud/logging/entry/source_location.rb +85 -0
- data/lib/google/cloud/logging/errors.rb +101 -0
- data/lib/google/cloud/logging/log/list.rb +156 -0
- data/lib/google/cloud/logging/logger.rb +633 -0
- data/lib/google/cloud/logging/metric.rb +168 -0
- data/lib/google/cloud/logging/metric/list.rb +170 -0
- data/lib/google/cloud/logging/middleware.rb +307 -0
- data/lib/google/cloud/logging/project.rb +838 -0
- data/lib/google/cloud/logging/rails.rb +232 -0
- data/lib/google/cloud/logging/resource.rb +85 -0
- data/lib/google/cloud/logging/resource_descriptor.rb +137 -0
- data/lib/google/cloud/logging/resource_descriptor/list.rb +175 -0
- data/lib/google/cloud/logging/service.rb +239 -0
- data/lib/google/cloud/logging/sink.rb +315 -0
- data/lib/google/cloud/logging/sink/list.rb +168 -0
- data/lib/google/cloud/logging/version.rb +22 -0
- metadata +304 -0
@@ -0,0 +1,232 @@
|
|
1
|
+
# Copyright 2016 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
require "google/cloud/logging"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Logging
|
21
|
+
##
|
22
|
+
# Default log name to be used for Stackdriver Logging
|
23
|
+
DEFAULT_LOG_NAME = Middleware::DEFAULT_LOG_NAME
|
24
|
+
|
25
|
+
##
|
26
|
+
# Railtie
|
27
|
+
#
|
28
|
+
# Adds the {Google::Cloud::Logging::Middleware} to Rack in a Rails
|
29
|
+
# environment. The middleware will set `env['rack.logger']` to a
|
30
|
+
# {Google::Cloud::Logging::Logger} instance to be used by the Rails
|
31
|
+
# application.
|
32
|
+
#
|
33
|
+
# The middleware is loaded only when certain conditions are met. These
|
34
|
+
# conditions are when the configuration
|
35
|
+
# `Google::Cloud.configure.use_logging` (also available as
|
36
|
+
# `Rails.application.config.google_cloud.use_logging` for a Rails
|
37
|
+
# application) is set to `true`, or, if the configuration is left unset
|
38
|
+
# but `Rails.env.production?` is `true`.
|
39
|
+
#
|
40
|
+
# When loaded, the {Google::Cloud::Logging::Middleware} will be inserted
|
41
|
+
# before the `Rails::Rack::Logger Middleware`, which allows it to set the
|
42
|
+
# `env['rack.logger']` in place of Rails's default logger.
|
43
|
+
# See the [Configuration
|
44
|
+
# Guide](https://googleapis.dev/ruby/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION.html)
|
45
|
+
# on how to configure the Railtie and Middleware.
|
46
|
+
#
|
47
|
+
class Railtie < ::Rails::Railtie
|
48
|
+
config.google_cloud = ::ActiveSupport::OrderedOptions.new unless
|
49
|
+
config.respond_to? :google_cloud
|
50
|
+
config.google_cloud.logging = ::ActiveSupport::OrderedOptions.new
|
51
|
+
config.google_cloud.logging.monitored_resource =
|
52
|
+
::ActiveSupport::OrderedOptions.new
|
53
|
+
config.google_cloud.logging.monitored_resource.labels =
|
54
|
+
::ActiveSupport::OrderedOptions.new
|
55
|
+
|
56
|
+
initializer "Stackdriver.Logging", before: :initialize_logger do |app|
|
57
|
+
self.class.consolidate_rails_config app.config
|
58
|
+
|
59
|
+
self.class.init_middleware app if Cloud.configure.use_logging
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# @private Init Logging integration for Rails. Setup configuration and
|
64
|
+
# insert the Middleware.
|
65
|
+
def self.init_middleware app
|
66
|
+
project_id = Logging.configure.project_id
|
67
|
+
credentials = Logging.configure.credentials
|
68
|
+
resource_type = Logging.configure.monitored_resource.type
|
69
|
+
resource_labels = Logging.configure.monitored_resource.labels
|
70
|
+
log_name = Logging.configure.log_name
|
71
|
+
labels = Logging.configure.labels
|
72
|
+
|
73
|
+
logging = Google::Cloud::Logging.new project_id: project_id,
|
74
|
+
credentials: credentials
|
75
|
+
resource =
|
76
|
+
Logging::Middleware.build_monitored_resource resource_type,
|
77
|
+
resource_labels
|
78
|
+
|
79
|
+
Middleware.logger = logging.logger log_name, resource, labels
|
80
|
+
# Set the default Rails logger
|
81
|
+
if Logging.configure.set_default_logger_on_rails_init
|
82
|
+
set_default_logger
|
83
|
+
end
|
84
|
+
init_callback = -> { set_default_logger }
|
85
|
+
app.middleware.insert_before Rails::Rack::Logger,
|
86
|
+
Google::Cloud::Logging::Middleware,
|
87
|
+
logger: Middleware.logger,
|
88
|
+
on_init: init_callback
|
89
|
+
end
|
90
|
+
|
91
|
+
##
|
92
|
+
# This should be called once the application determines that it is safe
|
93
|
+
# to start background threads and open gRPC connections. It informs the
|
94
|
+
# middleware system that it is safe to use Google Cloud Logging. This is
|
95
|
+
# called during Rails initialization when the
|
96
|
+
# `set_default_logger_on_rails_init` configuration is set.
|
97
|
+
#
|
98
|
+
# Generally, this matters if the application forks worker processes;
|
99
|
+
# this method should be called only after workers are forked, since
|
100
|
+
# threads and network connections interact badly with fork. For example,
|
101
|
+
# when running Puma in [clustered
|
102
|
+
# mode](https://github.com/puma/puma#clustered-mode), this method should
|
103
|
+
# be called in an `on_worker_boot` block.
|
104
|
+
#
|
105
|
+
# If the application does no forking, this method can be called any time
|
106
|
+
# early in the application initialization process. Or by setting the
|
107
|
+
# `set_default_logger_on_rails_init` configuration.
|
108
|
+
#
|
109
|
+
# If the `set_default_logger_on_rails_init` configuration is not set,
|
110
|
+
# and {Railtie.set_default_logger} is not called in a post-fork hook,
|
111
|
+
# the default Rails logger object will not be set to use the Google
|
112
|
+
# Cloud Logging Logger object. For best results, an application should
|
113
|
+
# call this method at the appropriate time, such as a post-fork hook.
|
114
|
+
#
|
115
|
+
def self.set_default_logger
|
116
|
+
return if Middleware.logger.nil?
|
117
|
+
return if Rails.logger.is_a? Google::Cloud::Logging::Logger
|
118
|
+
|
119
|
+
# configure the Middleware logger to use the same settings as Rails
|
120
|
+
Middleware.logger.level = Rails.logger.level
|
121
|
+
# TODO: are there more settings to be set here?
|
122
|
+
|
123
|
+
# Replace the Rails default logger
|
124
|
+
Rails.application.config.logger = Middleware.logger
|
125
|
+
Rails.logger = Middleware.logger
|
126
|
+
end
|
127
|
+
|
128
|
+
##
|
129
|
+
# @private Consolidate Rails configuration into Logging instrumentation
|
130
|
+
# configuration. Also consolidate the `use_logging` setting by verifying
|
131
|
+
# credentials and Rails environment. The `use_logging` setting will be
|
132
|
+
# true if credentials are valid, and the setting is manually set to true
|
133
|
+
# or Rails is in production environment.
|
134
|
+
#
|
135
|
+
# @param [Rails::Railtie::Configuration] config The
|
136
|
+
# Rails.application.config
|
137
|
+
#
|
138
|
+
def self.consolidate_rails_config config
|
139
|
+
merge_rails_config config
|
140
|
+
|
141
|
+
init_default_config
|
142
|
+
|
143
|
+
# Done if Google::Cloud.configure.use_logging is explicitly false
|
144
|
+
return if Google::Cloud.configure.use_logging == false
|
145
|
+
|
146
|
+
# Verify credentials and set use_logging to false if
|
147
|
+
# credentials are invalid
|
148
|
+
unless valid_credentials? Logging.configure.project_id,
|
149
|
+
Logging.configure.keyfile
|
150
|
+
Cloud.configure.use_logging = false
|
151
|
+
return
|
152
|
+
end
|
153
|
+
|
154
|
+
# Otherwise set use_logging to true if Rails is running in production
|
155
|
+
Google::Cloud.configure.use_logging ||= Rails.env.production?
|
156
|
+
end
|
157
|
+
|
158
|
+
##
|
159
|
+
# @private Merge Rails configuration into Logging instrumentation
|
160
|
+
# configuration.
|
161
|
+
def self.merge_rails_config rails_config # rubocop:disable AbcSize
|
162
|
+
gcp_config = rails_config.google_cloud
|
163
|
+
log_config = gcp_config.logging
|
164
|
+
|
165
|
+
if Cloud.configure.use_logging.nil?
|
166
|
+
Cloud.configure.use_logging = gcp_config.use_logging
|
167
|
+
end
|
168
|
+
Logging.configure do |config|
|
169
|
+
config.project_id ||= config.project
|
170
|
+
config.project_id ||= log_config.project_id || log_config.project
|
171
|
+
config.project_id ||= gcp_config.project_id || gcp_config.project
|
172
|
+
config.credentials ||= config.keyfile
|
173
|
+
config.credentials ||= log_config.credentials || log_config.keyfile
|
174
|
+
config.credentials ||= gcp_config.credentials || gcp_config.keyfile
|
175
|
+
config.log_name ||= log_config.log_name
|
176
|
+
config.labels ||= log_config.labels
|
177
|
+
config.log_name_map ||= log_config.log_name_map
|
178
|
+
config.monitored_resource.type ||=
|
179
|
+
log_config.monitored_resource.type
|
180
|
+
config.monitored_resource.labels ||=
|
181
|
+
log_config.monitored_resource.labels.to_h
|
182
|
+
if config.set_default_logger_on_rails_init.nil?
|
183
|
+
config.set_default_logger_on_rails_init = \
|
184
|
+
log_config.set_default_logger_on_rails_init
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
##
|
190
|
+
# Fallback to default config values if config parameters not provided.
|
191
|
+
def self.init_default_config
|
192
|
+
Logging.configure.project_id ||= Logging.default_project_id
|
193
|
+
Logging.configure.log_name ||= Middleware::DEFAULT_LOG_NAME
|
194
|
+
end
|
195
|
+
|
196
|
+
##
|
197
|
+
# @private Verify credentials
|
198
|
+
def self.valid_credentials? project_id, credentials
|
199
|
+
# Try authenticate authorize client API. Return false if unable to
|
200
|
+
# authorize.
|
201
|
+
begin
|
202
|
+
# if credentials is nil, get default
|
203
|
+
credentials ||= Logging::Credentials.default
|
204
|
+
# only create a new Credentials object if the val isn't one already
|
205
|
+
unless credentials.is_a? Google::Auth::Credentials
|
206
|
+
# if credentials is not a Credentials object, create one
|
207
|
+
Logging::Credentials.new credentials
|
208
|
+
end
|
209
|
+
rescue Exception => e
|
210
|
+
STDOUT.puts "Note: Google::Cloud::Logging is disabled because " \
|
211
|
+
"it failed to authorize with the service. (#{e.message}) " \
|
212
|
+
"Falling back to the default Rails logger."
|
213
|
+
return false
|
214
|
+
end
|
215
|
+
|
216
|
+
if project_id.to_s.empty?
|
217
|
+
STDOUT.puts "Note: Google::Cloud::Logging is disabled because " \
|
218
|
+
"the project ID could not be determined. " \
|
219
|
+
"Falling back to the default Rails logger."
|
220
|
+
return false
|
221
|
+
end
|
222
|
+
|
223
|
+
true
|
224
|
+
end
|
225
|
+
|
226
|
+
private_class_method :merge_rails_config,
|
227
|
+
:init_default_config,
|
228
|
+
:valid_credentials?
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# Copyright 2016 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
module Google
|
17
|
+
module Cloud
|
18
|
+
module Logging
|
19
|
+
##
|
20
|
+
# # Resource
|
21
|
+
#
|
22
|
+
# A monitored resource is an abstraction used to characterize many kinds
|
23
|
+
# of objects in your cloud infrastructure, including Google Cloud SQL
|
24
|
+
# databases, Google App Engine apps, Google Compute Engine virtual machine
|
25
|
+
# instances, and so forth. Each of those kinds of objects is described by
|
26
|
+
# an instance of {ResourceDescriptor}.
|
27
|
+
#
|
28
|
+
# For use with {Google::Cloud::Logging::Entry#resource},
|
29
|
+
# {Google::Cloud::Logging::Project#resource}, and
|
30
|
+
# {Google::Cloud::Logging::Project#write_entries}.
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# require "google/cloud/logging"
|
34
|
+
#
|
35
|
+
# logging = Google::Cloud::Logging.new
|
36
|
+
# resource = logging.resource "gae_app",
|
37
|
+
# "module_id" => "1",
|
38
|
+
# "version_id" => "20150925t173233"
|
39
|
+
#
|
40
|
+
class Resource
|
41
|
+
##
|
42
|
+
# Create an empty Resource object.
|
43
|
+
def initialize
|
44
|
+
@labels = {}
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# The type of resource, as represented by a {ResourceDescriptor}.
|
49
|
+
attr_accessor :type
|
50
|
+
|
51
|
+
##
|
52
|
+
# A set of labels that can be used to describe instances of this
|
53
|
+
# monitored resource type.
|
54
|
+
attr_accessor :labels
|
55
|
+
|
56
|
+
##
|
57
|
+
# @private Determines if the Resource has any data.
|
58
|
+
def empty?
|
59
|
+
type.nil? && (labels.nil? || labels.empty?)
|
60
|
+
end
|
61
|
+
|
62
|
+
##
|
63
|
+
# @private Exports the Resource to a Google::Api::MonitoredResource
|
64
|
+
# object.
|
65
|
+
def to_grpc
|
66
|
+
return nil if empty?
|
67
|
+
Google::Api::MonitoredResource.new(
|
68
|
+
type: type,
|
69
|
+
labels: Hash[labels.map { |k, v| [String(k), String(v)] }]
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# @private New Resource from a Google::Api::MonitoredResource object.
|
75
|
+
def self.from_grpc grpc
|
76
|
+
return new if grpc.nil?
|
77
|
+
new.tap do |r|
|
78
|
+
r.type = grpc.type
|
79
|
+
r.labels = Convert.map_to_hash grpc.labels
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# Copyright 2016 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
|
16
|
+
require "google/cloud/logging/resource_descriptor/list"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Logging
|
21
|
+
##
|
22
|
+
# # ResourceDescriptor
|
23
|
+
#
|
24
|
+
# Describes a type of monitored resource supported by Stackdriver Logging.
|
25
|
+
# Each ResourceDescriptor has a type name, such as `cloudsql_database`,
|
26
|
+
# `gae_app`, or `gce_instance`. It also specifies a set of labels that
|
27
|
+
# must all be given values in a {Resource} instance to represent an actual
|
28
|
+
# instance of the type.
|
29
|
+
#
|
30
|
+
# ResourceDescriptor instances are read-only. You cannot create your own
|
31
|
+
# instances, but you can list them with {Project#resource_descriptors}.
|
32
|
+
#
|
33
|
+
# @example
|
34
|
+
# require "google/cloud/logging"
|
35
|
+
#
|
36
|
+
# logging = Google::Cloud::Logging.new
|
37
|
+
# resource_descriptor = logging.resource_descriptors.first
|
38
|
+
# resource_descriptor.type #=> "cloudsql_database"
|
39
|
+
# resource_descriptor.name #=> "Cloud SQL Database"
|
40
|
+
# resource_descriptor.labels.map &:key #=> ["database_id", "zone"]
|
41
|
+
#
|
42
|
+
class ResourceDescriptor
|
43
|
+
##
|
44
|
+
# @private New ResourceDescriptor from a Google API Client object.
|
45
|
+
def initialize
|
46
|
+
@labels = []
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# The monitored resource type. For example, `cloudsql_database`.
|
51
|
+
attr_reader :type
|
52
|
+
|
53
|
+
##
|
54
|
+
# A display name for the monitored resource type. For example,
|
55
|
+
# `Cloud SQL Database`.
|
56
|
+
attr_reader :name
|
57
|
+
|
58
|
+
##
|
59
|
+
# A detailed description of the monitored resource type, which is used
|
60
|
+
# in documentation.
|
61
|
+
attr_reader :description
|
62
|
+
|
63
|
+
##
|
64
|
+
# A set of definitions of the labels that can be used to describe
|
65
|
+
# instances of this monitored resource type. For example, Cloud SQL
|
66
|
+
# databases must be labeled with their `database_id` and their `region`.
|
67
|
+
#
|
68
|
+
# @return [Array<LabelDescriptor>]
|
69
|
+
#
|
70
|
+
attr_reader :labels
|
71
|
+
|
72
|
+
##
|
73
|
+
# @private New ResourceDescriptor from a
|
74
|
+
# Google::Api::MonitoredResourceDescriptor object.
|
75
|
+
def self.from_grpc grpc
|
76
|
+
labels = Array(grpc.labels).map do |g|
|
77
|
+
LabelDescriptor.from_grpc g
|
78
|
+
end
|
79
|
+
new.tap do |r|
|
80
|
+
r.instance_variable_set :@type, grpc.type
|
81
|
+
r.instance_variable_set :@name, grpc.display_name
|
82
|
+
r.instance_variable_set :@description, grpc.description
|
83
|
+
r.instance_variable_set :@labels, labels
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# # LabelDescriptor
|
89
|
+
#
|
90
|
+
# A definition of a label that can be used to describe instances of a
|
91
|
+
# {Resource}. For example, Cloud SQL databases must be labeled with
|
92
|
+
# their `database_id`. See {ResourceDescriptor#labels}.
|
93
|
+
#
|
94
|
+
# @example
|
95
|
+
# require "google/cloud/logging"
|
96
|
+
#
|
97
|
+
# logging = Google::Cloud::Logging.new
|
98
|
+
# resource_descriptor = logging.resource_descriptors.first
|
99
|
+
# label_descriptor = resource_descriptor.labels.first
|
100
|
+
# label_descriptor.key #=> "database_id"
|
101
|
+
# label_descriptor.description #=> "The ID of the database."
|
102
|
+
#
|
103
|
+
class LabelDescriptor
|
104
|
+
##
|
105
|
+
# The key (name) of the label.
|
106
|
+
attr_reader :key
|
107
|
+
|
108
|
+
##
|
109
|
+
# The type of data that can be assigned to the label.
|
110
|
+
#
|
111
|
+
# @return [Symbol, nil] Returns `:string`, `:boolean`, `:integer`, or
|
112
|
+
# `nil` if there is no type.
|
113
|
+
#
|
114
|
+
attr_reader :type
|
115
|
+
|
116
|
+
##
|
117
|
+
# A human-readable description for the label.
|
118
|
+
attr_reader :description
|
119
|
+
|
120
|
+
##
|
121
|
+
# @private New LabelDescriptor from a Google::Api::LabelDescriptor
|
122
|
+
# object.
|
123
|
+
def self.from_grpc grpc
|
124
|
+
type_sym = { STRING: :string,
|
125
|
+
BOOL: :boolean,
|
126
|
+
INT64: :integer }[grpc.value_type]
|
127
|
+
new.tap do |l|
|
128
|
+
l.instance_variable_set :@key, grpc.key
|
129
|
+
l.instance_variable_set :@type, type_sym
|
130
|
+
l.instance_variable_set :@description, grpc.description
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|