google-cloud-logging 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- 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,168 @@
|
|
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/metric/list"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Logging
|
21
|
+
##
|
22
|
+
# # Metric
|
23
|
+
#
|
24
|
+
# A logs-based [Google Cloud
|
25
|
+
# Monitoring](https://cloud.google.com/monitoring/docs) metric. A metric
|
26
|
+
# is a measured value that can be used to assess a system. The basis of a
|
27
|
+
# logs-based metric is the collection of log entries that match a logs
|
28
|
+
# filter.
|
29
|
+
#
|
30
|
+
# @see https://cloud.google.com/logging/docs/view/logs_based_metrics
|
31
|
+
# Logs-based Metrics
|
32
|
+
# @see https://cloud.google.com/monitoring/docs Google Cloud Monitoring
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# require "google/cloud/logging"
|
36
|
+
#
|
37
|
+
# logging = Google::Cloud::Logging.new
|
38
|
+
# metric = logging.create_metric "errors", "severity>=ERROR"
|
39
|
+
#
|
40
|
+
class Metric
|
41
|
+
##
|
42
|
+
# @private The gRPC Service object.
|
43
|
+
attr_accessor :service
|
44
|
+
|
45
|
+
##
|
46
|
+
# @private The gRPC Google::Cloud::Logging::V2::LogMetric object.
|
47
|
+
attr_accessor :grpc
|
48
|
+
|
49
|
+
##
|
50
|
+
# @private Create an empty Metric object.
|
51
|
+
def initialize
|
52
|
+
@service = nil
|
53
|
+
@grpc = Google::Cloud::Logging::V2::LogMetric.new
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# The client-assigned metric identifier. Metric identifiers are limited
|
58
|
+
# to 1000 characters and can include only the following characters:
|
59
|
+
# `A-Z`, `a-z`, `0-9`, and the special characters `_-.,+!*',()%/\`. The
|
60
|
+
# forward-slash character (`/`) denotes a hierarchy of name pieces, and
|
61
|
+
# it cannot be the first character of the name.
|
62
|
+
def name
|
63
|
+
grpc.name
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# The description of this metric, which is used in documentation.
|
68
|
+
def description
|
69
|
+
grpc.description
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# Updates the description of this metric, which is used in
|
74
|
+
# documentation.
|
75
|
+
def description= description
|
76
|
+
grpc.description = description
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# An [advanced logs
|
81
|
+
# filter](https://cloud.google.com/logging/docs/view/advanced_filters).
|
82
|
+
def filter
|
83
|
+
grpc.filter
|
84
|
+
end
|
85
|
+
|
86
|
+
##
|
87
|
+
# Updates the [advanced logs
|
88
|
+
# filter](https://cloud.google.com/logging/docs/view/advanced_filters).
|
89
|
+
def filter= filter
|
90
|
+
grpc.filter = filter
|
91
|
+
end
|
92
|
+
|
93
|
+
##
|
94
|
+
# Updates the logs-based metric.
|
95
|
+
#
|
96
|
+
# @example
|
97
|
+
# require "google/cloud/logging"
|
98
|
+
#
|
99
|
+
# logging = Google::Cloud::Logging.new
|
100
|
+
# metric = logging.metric "severe_errors"
|
101
|
+
# metric.filter = "logName:syslog AND severity>=ERROR"
|
102
|
+
# metric.save
|
103
|
+
#
|
104
|
+
def save
|
105
|
+
ensure_service!
|
106
|
+
@grpc = service.update_metric name, description, filter
|
107
|
+
true
|
108
|
+
end
|
109
|
+
|
110
|
+
##
|
111
|
+
# Reloads the logs-based metric with current data from the Logging
|
112
|
+
# service.
|
113
|
+
#
|
114
|
+
# @example
|
115
|
+
# require "google/cloud/logging"
|
116
|
+
#
|
117
|
+
# logging = Google::Cloud::Logging.new
|
118
|
+
# metric = logging.metric "severe_errors"
|
119
|
+
# metric.filter = "Unwanted value"
|
120
|
+
# metric.reload!
|
121
|
+
# metric.filter #=> "logName:syslog"
|
122
|
+
#
|
123
|
+
def reload!
|
124
|
+
ensure_service!
|
125
|
+
@grpc = service.get_metric name
|
126
|
+
true
|
127
|
+
end
|
128
|
+
alias refresh! reload!
|
129
|
+
|
130
|
+
##
|
131
|
+
# Permanently deletes the logs-based metric.
|
132
|
+
#
|
133
|
+
# @return [Boolean] Returns `true` if the metric was deleted.
|
134
|
+
#
|
135
|
+
# @example
|
136
|
+
# require "google/cloud/logging"
|
137
|
+
#
|
138
|
+
# logging = Google::Cloud::Logging.new
|
139
|
+
# metric = logging.metric "severe_errors"
|
140
|
+
# metric.delete
|
141
|
+
#
|
142
|
+
def delete
|
143
|
+
ensure_service!
|
144
|
+
service.delete_metric name
|
145
|
+
true
|
146
|
+
end
|
147
|
+
|
148
|
+
##
|
149
|
+
# @private New Metric from a gRPC object.
|
150
|
+
def self.from_grpc grpc, service
|
151
|
+
new.tap do |m|
|
152
|
+
m.grpc = grpc
|
153
|
+
m.service = service
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
protected
|
158
|
+
|
159
|
+
##
|
160
|
+
# @private Raise an error unless an active connection to the service is
|
161
|
+
# available.
|
162
|
+
def ensure_service!
|
163
|
+
raise "Must have active connection to service" unless service
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,170 @@
|
|
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 "delegate"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Logging
|
21
|
+
class Metric
|
22
|
+
##
|
23
|
+
# Metric::List is a special case Array with additional values.
|
24
|
+
class List < DelegateClass(::Array)
|
25
|
+
##
|
26
|
+
# If not empty, indicates that there are more records that match
|
27
|
+
# the request and this value should be passed to continue.
|
28
|
+
attr_accessor :token
|
29
|
+
|
30
|
+
##
|
31
|
+
# @private Create a new Metric::List with an array of Metric
|
32
|
+
# instances.
|
33
|
+
def initialize arr = []
|
34
|
+
super arr
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Whether there is a next page of metrics.
|
39
|
+
#
|
40
|
+
# @return [Boolean]
|
41
|
+
#
|
42
|
+
# @example
|
43
|
+
# require "google/cloud/logging"
|
44
|
+
#
|
45
|
+
# logging = Google::Cloud::Logging.new
|
46
|
+
#
|
47
|
+
# metrics = logging.metrics
|
48
|
+
# if metrics.next?
|
49
|
+
# next_metrics = metrics.next
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
def next?
|
53
|
+
!token.nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# Retrieve the next page of metrics.
|
58
|
+
#
|
59
|
+
# @return [Sink::List]
|
60
|
+
#
|
61
|
+
# @example
|
62
|
+
# require "google/cloud/logging"
|
63
|
+
#
|
64
|
+
# logging = Google::Cloud::Logging.new
|
65
|
+
#
|
66
|
+
# metrics = logging.metrics
|
67
|
+
# if metrics.next?
|
68
|
+
# next_metrics = metrics.next
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
def next
|
72
|
+
return nil unless next?
|
73
|
+
ensure_service!
|
74
|
+
grpc = @service.list_metrics token: token, max: @max
|
75
|
+
self.class.from_grpc grpc, @service, @max
|
76
|
+
end
|
77
|
+
|
78
|
+
##
|
79
|
+
# Retrieves remaining results by repeatedly invoking {#next} until
|
80
|
+
# {#next?} returns `false`. Calls the given block once for each
|
81
|
+
# result, which is passed as the argument to the block.
|
82
|
+
#
|
83
|
+
# An Enumerator is returned if no block is given.
|
84
|
+
#
|
85
|
+
# This method will make repeated API calls until all remaining results
|
86
|
+
# are retrieved. (Unlike `#each`, for example, which merely iterates
|
87
|
+
# over the results returned by a single API call.) Use with caution.
|
88
|
+
#
|
89
|
+
# @param [Integer] request_limit The upper limit of API requests to
|
90
|
+
# make to load all metrics. Default is no limit.
|
91
|
+
# @yield [metric] The block for accessing each metric.
|
92
|
+
# @yieldparam [Metric] metric The metric object.
|
93
|
+
#
|
94
|
+
# @return [Enumerator]
|
95
|
+
#
|
96
|
+
# @example Iterating each metric by passing a block:
|
97
|
+
# require "google/cloud/logging"
|
98
|
+
#
|
99
|
+
# logging = Google::Cloud::Logging.new
|
100
|
+
# metrics = logging.metrics
|
101
|
+
#
|
102
|
+
# metrics.all do |metric|
|
103
|
+
# puts "#{metric.name}: #{metric.filter}"
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# @example Using the enumerator by not passing a block:
|
107
|
+
# require "google/cloud/logging"
|
108
|
+
#
|
109
|
+
# logging = Google::Cloud::Logging.new
|
110
|
+
# metrics = logging.metrics
|
111
|
+
#
|
112
|
+
# all_names = metrics.all.map do |metric|
|
113
|
+
# metric.name
|
114
|
+
# end
|
115
|
+
#
|
116
|
+
# @example Limit the number of API calls made:
|
117
|
+
# require "google/cloud/logging"
|
118
|
+
#
|
119
|
+
# logging = Google::Cloud::Logging.new
|
120
|
+
# metrics = logging.metrics
|
121
|
+
#
|
122
|
+
# metrics.all(request_limit: 10) do |metric|
|
123
|
+
# puts "#{metric.name}: #{metric.filter}"
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
def all request_limit: nil
|
127
|
+
request_limit = request_limit.to_i if request_limit
|
128
|
+
unless block_given?
|
129
|
+
return enum_for :all, request_limit: request_limit
|
130
|
+
end
|
131
|
+
results = self
|
132
|
+
loop do
|
133
|
+
results.each { |r| yield r }
|
134
|
+
if request_limit
|
135
|
+
request_limit -= 1
|
136
|
+
break if request_limit < 0
|
137
|
+
end
|
138
|
+
break unless results.next?
|
139
|
+
results = results.next
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
##
|
144
|
+
# @private New Metric::List from a
|
145
|
+
# Google::Cloud::Logging::V2::ListLogMetricsResponse object.
|
146
|
+
def self.from_grpc grpc_list, service, max = nil
|
147
|
+
metrics = new(Array(grpc_list.metrics).map do |grpc_metric|
|
148
|
+
Metric.from_grpc grpc_metric, service
|
149
|
+
end)
|
150
|
+
token = grpc_list.next_page_token
|
151
|
+
token = nil if token == "".freeze
|
152
|
+
metrics.instance_variable_set :@token, token
|
153
|
+
metrics.instance_variable_set :@service, service
|
154
|
+
metrics.instance_variable_set :@max, max
|
155
|
+
metrics
|
156
|
+
end
|
157
|
+
|
158
|
+
protected
|
159
|
+
|
160
|
+
##
|
161
|
+
# @private Raise an error unless an active connection to the service
|
162
|
+
# is available.
|
163
|
+
def ensure_service!
|
164
|
+
raise "Must have active connection to service" unless @service
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,307 @@
|
|
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 "stackdriver/core"
|
17
|
+
|
18
|
+
module Google
|
19
|
+
module Cloud
|
20
|
+
module Logging
|
21
|
+
class Middleware
|
22
|
+
##
|
23
|
+
# The default log name used to instantiate the default logger if one
|
24
|
+
# isn't provided.
|
25
|
+
DEFAULT_LOG_NAME = "ruby_app_log".freeze
|
26
|
+
|
27
|
+
##
|
28
|
+
# A default value for the log_name_map argument. Directs health check
|
29
|
+
# logs to a separate log name so they don't spam the main log.
|
30
|
+
DEFAULT_LOG_NAME_MAP =
|
31
|
+
{ "/_ah/health" => "ruby_health_check_log",
|
32
|
+
"/healthz" => "ruby_health_check_log" }.freeze
|
33
|
+
|
34
|
+
##
|
35
|
+
# The Google::Cloud::Logging::Logger instance
|
36
|
+
attr_reader :logger
|
37
|
+
|
38
|
+
##
|
39
|
+
# Create a new AppEngine logging Middleware.
|
40
|
+
#
|
41
|
+
# @param [Rack Application] app Rack application
|
42
|
+
# @param [Google::Cloud::Logging::Logger] logger A logger to be used by
|
43
|
+
# this middleware. The middleware will be interacting with the logger
|
44
|
+
# to track Stackdriver request trace ID. It also properly sets
|
45
|
+
# env["rack.logger"] to this assigned logger for accessing. If not
|
46
|
+
# specified, a default logger with be used.
|
47
|
+
# @param [Proc] on_init A callback to be invoked when the middleware is
|
48
|
+
# initialized. The callback takes no arguments. Optional.
|
49
|
+
# @param [Hash] kwargs Hash of configuration settings. Used for
|
50
|
+
# backward API compatibility. See the [Configuration
|
51
|
+
# Guide](https://googleapis.dev/ruby/stackdriver/latest/file.INSTRUMENTATION_CONFIGURATION.html)
|
52
|
+
# for the prefered way to set configuration parameters.
|
53
|
+
#
|
54
|
+
# @return [Google::Cloud::Logging::Middleware] A new
|
55
|
+
# Google::Cloud::Logging::Middleware instance
|
56
|
+
#
|
57
|
+
def initialize app, logger: nil, on_init: nil, **kwargs
|
58
|
+
@app = app
|
59
|
+
|
60
|
+
load_config kwargs
|
61
|
+
|
62
|
+
logger ||= Middleware.logger
|
63
|
+
logger ||= begin
|
64
|
+
log_name = configuration.log_name
|
65
|
+
logging = Logging.new project_id: configuration.project_id,
|
66
|
+
credentials: configuration.credentials
|
67
|
+
resource = Middleware.build_monitored_resource(
|
68
|
+
configuration.monitored_resource.type,
|
69
|
+
configuration.monitored_resource.labels
|
70
|
+
)
|
71
|
+
Middleware.logger = logging.logger log_name, resource
|
72
|
+
end
|
73
|
+
|
74
|
+
on_init.call if on_init
|
75
|
+
|
76
|
+
@logger = logger
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Rack middleware entry point. In most Rack based frameworks, a request
|
81
|
+
# is served by one thread. So entry point, we associate the GCP request
|
82
|
+
# trace_id with the current thread's object_id in logger. All the logs
|
83
|
+
# written by logger beyond this point will carry this request's
|
84
|
+
# trace_id. Untrack the trace_id with this thread upon exiting.
|
85
|
+
#
|
86
|
+
# @param [Hash] env Rack environment hash
|
87
|
+
#
|
88
|
+
# @return [Rack::Response] The response from downstream Rack app
|
89
|
+
#
|
90
|
+
def call env
|
91
|
+
env["rack.logger"] = logger
|
92
|
+
trace_id = get_trace_id env
|
93
|
+
log_name = get_log_name env
|
94
|
+
logger.add_request_info trace_id: trace_id, log_name: log_name,
|
95
|
+
env: env
|
96
|
+
begin
|
97
|
+
@app.call env
|
98
|
+
ensure
|
99
|
+
logger.delete_request_info
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# Determine the trace ID for this request.
|
105
|
+
#
|
106
|
+
# @private
|
107
|
+
# @param [Hash] env The Rack environment.
|
108
|
+
# @return [String] The trace ID.
|
109
|
+
#
|
110
|
+
def get_trace_id env
|
111
|
+
trace_context = Stackdriver::Core::TraceContext.parse_rack_env env
|
112
|
+
trace_context.trace_id
|
113
|
+
end
|
114
|
+
|
115
|
+
##
|
116
|
+
# Determine the log name override for this request, if any.
|
117
|
+
#
|
118
|
+
# @private
|
119
|
+
# @param [Hash] env The Rack environment.
|
120
|
+
# @return [String, nil] The log name override, or `nil` if there is
|
121
|
+
# no override.
|
122
|
+
#
|
123
|
+
def get_log_name env
|
124
|
+
log_name_map = configuration.log_name_map
|
125
|
+
return nil unless log_name_map
|
126
|
+
path = "#{env['SCRIPT_NAME']}#{env['PATH_INFO']}"
|
127
|
+
path = "/#{path}" unless path.start_with? "/"
|
128
|
+
log_name_map.each do |k, v|
|
129
|
+
return v if k === path
|
130
|
+
end
|
131
|
+
nil
|
132
|
+
end
|
133
|
+
|
134
|
+
##
|
135
|
+
# Construct a monitored resource based on the given type and label if
|
136
|
+
# both are provided. Otherwise, construct a default monitored resource
|
137
|
+
# based on the current environment.
|
138
|
+
#
|
139
|
+
# @param [String] type Type of Google::Cloud::Logging::Resource
|
140
|
+
# @param [Hash<String, String>] labels Metadata lebels of
|
141
|
+
# Google::Cloud::Logging::Resource
|
142
|
+
#
|
143
|
+
# @return [Google::Cloud::Logging::Resource] An Resource object with
|
144
|
+
# type and labels
|
145
|
+
#
|
146
|
+
# @see https://cloud.google.com/logging/docs/api/v2/resource-list
|
147
|
+
# Monitored Resources and Services
|
148
|
+
#
|
149
|
+
# @example If both type and labels are provided, it returns resource:
|
150
|
+
# rc = Google::Cloud::Logging::Middleware.build_monitored_resource(
|
151
|
+
# "aws_ec2_instance",
|
152
|
+
# {
|
153
|
+
# instance_id: "ec2-id",
|
154
|
+
# aws_account: "aws-id"
|
155
|
+
# }
|
156
|
+
# )
|
157
|
+
# rc.type #=> "aws_ec2_instance"
|
158
|
+
# rc.labels #=> { instance_id: "ec2-id", aws_account: "aws-id" }
|
159
|
+
#
|
160
|
+
# @example If running from GAE, returns default resource:
|
161
|
+
# rc = Google::Cloud::Logging::Middleware.build_monitored_resource
|
162
|
+
# rc.type #=> "gae_app"
|
163
|
+
# rc.labels # { module_id: [GAE module name],
|
164
|
+
# # version_id: [GAE module version] }
|
165
|
+
#
|
166
|
+
# @example If running from GKE, returns default resource:
|
167
|
+
# rc = Google::Cloud::Logging::Middleware.build_monitored_resource
|
168
|
+
# rc.type #=> "container"
|
169
|
+
# rc.labels # { cluster_name: [GKE cluster name],
|
170
|
+
# # namespace_id: [GKE namespace_id] }
|
171
|
+
#
|
172
|
+
# @example If running from GCE, return default resource:
|
173
|
+
# rc = Google::Cloud::Logging::Middleware.build_monitored_resource
|
174
|
+
# rc.type #=> "gce_instance"
|
175
|
+
# rc.labels # { instance_id: [GCE VM instance id],
|
176
|
+
# # zone: [GCE vm group zone] }
|
177
|
+
#
|
178
|
+
# @example Otherwise default to generic "global" type:
|
179
|
+
# rc = Google::Cloud::Logging::Middleware.build_monitored_resource
|
180
|
+
# rc.type #=> "global"
|
181
|
+
# rc.labels #=> {}
|
182
|
+
#
|
183
|
+
def self.build_monitored_resource type = nil, labels = nil
|
184
|
+
if type && labels
|
185
|
+
Google::Cloud::Logging::Resource.new.tap do |r|
|
186
|
+
r.type = type
|
187
|
+
r.labels = labels
|
188
|
+
end
|
189
|
+
else
|
190
|
+
default_monitored_resource
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
class << self
|
195
|
+
##
|
196
|
+
# @private Global logger object
|
197
|
+
attr_accessor :logger
|
198
|
+
end
|
199
|
+
|
200
|
+
##
|
201
|
+
# @private Extract information from current environment and construct
|
202
|
+
# the correct monitoring resource types and labels.
|
203
|
+
#
|
204
|
+
# @return [Google::Cloud::Logging::Resource] An Resource object with
|
205
|
+
# correct type and labels
|
206
|
+
#
|
207
|
+
# @see https://cloud.google.com/logging/docs/api/v2/resource-list
|
208
|
+
# Monitored Resources and Services
|
209
|
+
#
|
210
|
+
# @example If running from GAE, returns default resource:
|
211
|
+
# rc = Google::Cloud::Logging::Middleware.send \
|
212
|
+
# :default_monitored_resource
|
213
|
+
# rc.type #=> "gae_app"
|
214
|
+
# rc.labels # { module_id: [GAE module name],
|
215
|
+
# # version_id: [GAE module version] }
|
216
|
+
#
|
217
|
+
# @example If running from GKE, returns default resource:
|
218
|
+
# rc = Google::Cloud::Logging::Middleware.send \
|
219
|
+
# :default_monitored_resource
|
220
|
+
# rc.type #=> "container"
|
221
|
+
# rc.labels # { cluster_name: [GKE cluster name],
|
222
|
+
# # namespace_id: [GKE namespace_id] }
|
223
|
+
#
|
224
|
+
# @example If running from GCE, return default resource:
|
225
|
+
# rc = Google::Cloud::Logging::Middleware.send \
|
226
|
+
# :default_monitored_resource
|
227
|
+
# rc.type #=> "gce_instance"
|
228
|
+
# rc.labels # { instance_id: [GCE VM instance id],
|
229
|
+
# # zone: [GCE vm group zone] }
|
230
|
+
#
|
231
|
+
# @example Otherwise default to generic "global" type:
|
232
|
+
# rc = Google::Cloud::Logging::Middleware.send \
|
233
|
+
# :default_monitored_resource
|
234
|
+
# rc.type #=> "global"
|
235
|
+
# rc.labels #=> {}
|
236
|
+
#
|
237
|
+
def self.default_monitored_resource
|
238
|
+
type, labels =
|
239
|
+
if Google::Cloud.env.app_engine?
|
240
|
+
["gae_app", {
|
241
|
+
module_id: Google::Cloud.env.app_engine_service_id,
|
242
|
+
version_id: Google::Cloud.env.app_engine_service_version
|
243
|
+
}]
|
244
|
+
elsif Google::Cloud.env.container_engine?
|
245
|
+
namespace_id = Google::Cloud.env.container_engine_namespace_id
|
246
|
+
namespace_id ||= "default"
|
247
|
+
["container", {
|
248
|
+
cluster_name: Google::Cloud.env.container_engine_cluster_name,
|
249
|
+
namespace_id: namespace_id
|
250
|
+
}]
|
251
|
+
elsif Google::Cloud.env.compute_engine?
|
252
|
+
["gce_instance", {
|
253
|
+
instance_id: Google::Cloud.env.instance_name,
|
254
|
+
zone: Google::Cloud.env.instance_zone
|
255
|
+
}]
|
256
|
+
else
|
257
|
+
["global", {}]
|
258
|
+
end
|
259
|
+
|
260
|
+
Google::Cloud::Logging::Resource.new.tap do |r|
|
261
|
+
r.type = type
|
262
|
+
r.labels = labels
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
private_class_method :default_monitored_resource
|
267
|
+
|
268
|
+
private
|
269
|
+
|
270
|
+
##
|
271
|
+
# Consolidate configurations from various sources. Also set
|
272
|
+
# instrumentation config parameters to default values if not set
|
273
|
+
# already.
|
274
|
+
#
|
275
|
+
def load_config **kwargs
|
276
|
+
project_id = kwargs[:project] || kwargs[:project_id]
|
277
|
+
configuration.project_id = project_id unless project_id.nil?
|
278
|
+
|
279
|
+
creds = kwargs[:credentials] || kwargs[:keyfile]
|
280
|
+
configuration.credentials = creds unless creds.nil?
|
281
|
+
|
282
|
+
log_name_map = kwargs[:log_name_map]
|
283
|
+
configuration.log_name_map = log_name_map unless log_name_map.nil?
|
284
|
+
|
285
|
+
init_default_config
|
286
|
+
end
|
287
|
+
|
288
|
+
##
|
289
|
+
# Fallback to default configuration values if not defined already
|
290
|
+
def init_default_config
|
291
|
+
configuration.project_id ||= begin
|
292
|
+
(Cloud.configure.project_id || Logging.default_project_id)
|
293
|
+
end
|
294
|
+
configuration.credentials ||= Cloud.configure.credentials
|
295
|
+
configuration.log_name ||= DEFAULT_LOG_NAME
|
296
|
+
configuration.log_name_map ||= DEFAULT_LOG_NAME_MAP
|
297
|
+
end
|
298
|
+
|
299
|
+
##
|
300
|
+
# @private Get Google::Cloud::Logging.configure
|
301
|
+
def configuration
|
302
|
+
Google::Cloud::Logging.configure
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|