google-cloud-logging 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/google-cloud-logging.rb +119 -0
- data/lib/google/cloud/logging.rb +293 -0
- data/lib/google/cloud/logging/credentials.rb +31 -0
- data/lib/google/cloud/logging/entry.rb +469 -0
- data/lib/google/cloud/logging/entry/http_request.rb +145 -0
- data/lib/google/cloud/logging/entry/list.rb +180 -0
- data/lib/google/cloud/logging/entry/operation.rb +92 -0
- data/lib/google/cloud/logging/logger.rb +309 -0
- data/lib/google/cloud/logging/metric.rb +172 -0
- data/lib/google/cloud/logging/metric/list.rb +175 -0
- data/lib/google/cloud/logging/project.rb +650 -0
- data/lib/google/cloud/logging/resource.rb +86 -0
- data/lib/google/cloud/logging/resource_descriptor.rb +139 -0
- data/lib/google/cloud/logging/resource_descriptor/list.rb +179 -0
- data/lib/google/cloud/logging/service.rb +270 -0
- data/lib/google/cloud/logging/sink.rb +230 -0
- data/lib/google/cloud/logging/sink/list.rb +173 -0
- data/lib/google/cloud/logging/v2.rb +17 -0
- data/lib/google/cloud/logging/v2/config_service_v2_api.rb +331 -0
- data/lib/google/cloud/logging/v2/config_service_v2_client_config.json +53 -0
- data/lib/google/cloud/logging/v2/logging_service_v2_api.rb +351 -0
- data/lib/google/cloud/logging/v2/logging_service_v2_client_config.json +57 -0
- data/lib/google/cloud/logging/v2/metrics_service_v2_api.rb +330 -0
- data/lib/google/cloud/logging/v2/metrics_service_v2_client_config.json +53 -0
- data/lib/google/cloud/logging/version.rb +22 -0
- data/lib/google/logging/v2/log_entry_pb.rb +44 -0
- data/lib/google/logging/v2/logging_config_pb.rb +59 -0
- data/lib/google/logging/v2/logging_config_services_pb.rb +52 -0
- data/lib/google/logging/v2/logging_metrics_pb.rb +51 -0
- data/lib/google/logging/v2/logging_metrics_services_pb.rb +51 -0
- data/lib/google/logging/v2/logging_pb.rb +59 -0
- data/lib/google/logging/v2/logging_services_pb.rb +56 -0
- metadata +260 -0
@@ -0,0 +1,86 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
# http://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"
|
34
|
+
#
|
35
|
+
# gcloud = Google::Cloud.new
|
36
|
+
# logging = gcloud.logging
|
37
|
+
# resource = logging.resource "gae_app",
|
38
|
+
# "module_id" => "1",
|
39
|
+
# "version_id" => "20150925t173233"
|
40
|
+
#
|
41
|
+
class Resource
|
42
|
+
##
|
43
|
+
# Create an empty Resource object.
|
44
|
+
def initialize
|
45
|
+
@labels = {}
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# The type of resource, as represented by a {ResourceDescriptor}.
|
50
|
+
attr_accessor :type
|
51
|
+
|
52
|
+
##
|
53
|
+
# A set of labels that can be used to describe instances of this
|
54
|
+
# monitored resource type.
|
55
|
+
attr_accessor :labels
|
56
|
+
|
57
|
+
##
|
58
|
+
# @private Determines if the Resource has any data.
|
59
|
+
def empty?
|
60
|
+
type.nil? && (labels.nil? || labels.empty?)
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# @private Exports the Resource to a Google::Api::MonitoredResource
|
65
|
+
# object.
|
66
|
+
def to_grpc
|
67
|
+
return nil if empty?
|
68
|
+
Google::Api::MonitoredResource.new(
|
69
|
+
type: type,
|
70
|
+
labels: Hash[labels.map { |k, v| [String(k), String(v)] }]
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# @private New Resource from a Google::Api::MonitoredResource object.
|
76
|
+
def self.from_grpc grpc
|
77
|
+
return new if grpc.nil?
|
78
|
+
new.tap do |r|
|
79
|
+
r.type = grpc.type
|
80
|
+
r.labels = Core::GRPCUtils.map_to_hash(grpc.labels)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
# http://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"
|
35
|
+
#
|
36
|
+
# gcloud = Google::Cloud.new
|
37
|
+
# logging = gcloud.logging
|
38
|
+
# resource_descriptor = logging.resource_descriptors.first
|
39
|
+
# resource_descriptor.type #=> "cloudsql_database"
|
40
|
+
# resource_descriptor.name #=> "Cloud SQL Database"
|
41
|
+
# resource_descriptor.labels.map &:key #=> ["database_id", "region"]
|
42
|
+
#
|
43
|
+
class ResourceDescriptor
|
44
|
+
##
|
45
|
+
# @private New ResourceDescriptor from a Google API Client object.
|
46
|
+
def initialize
|
47
|
+
@labels = []
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# The monitored resource type. For example, `cloudsql_database`.
|
52
|
+
attr_reader :type
|
53
|
+
|
54
|
+
##
|
55
|
+
# A display name for the monitored resource type. For example,
|
56
|
+
# `Cloud SQL Database`.
|
57
|
+
attr_reader :name
|
58
|
+
|
59
|
+
##
|
60
|
+
# A detailed description of the monitored resource type, which is used
|
61
|
+
# in documentation.
|
62
|
+
attr_reader :description
|
63
|
+
|
64
|
+
##
|
65
|
+
# A set of definitions of the labels that can be used to describe
|
66
|
+
# instances of this monitored resource type. For example, Cloud SQL
|
67
|
+
# databases must be labeled with their `database_id` and their `region`.
|
68
|
+
#
|
69
|
+
# @return [Array<LabelDescriptor>]
|
70
|
+
#
|
71
|
+
attr_reader :labels
|
72
|
+
|
73
|
+
##
|
74
|
+
# @private New ResourceDescriptor from a
|
75
|
+
# Google::Api::MonitoredResourceDescriptor object.
|
76
|
+
def self.from_grpc grpc
|
77
|
+
r = new
|
78
|
+
r.instance_variable_set "@type", grpc.type
|
79
|
+
r.instance_variable_set "@name", grpc.display_name
|
80
|
+
r.instance_variable_set "@description", grpc.description
|
81
|
+
labels = Array(grpc.labels).map do |g|
|
82
|
+
LabelDescriptor.from_grpc g
|
83
|
+
end
|
84
|
+
r.instance_variable_set "@labels", labels
|
85
|
+
r
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# # LabelDescriptor
|
90
|
+
#
|
91
|
+
# A definition of a label that can be used to describe instances of a
|
92
|
+
# {Resource}. For example, Cloud SQL databases must be labeled with
|
93
|
+
# their `database_id`. See {ResourceDescriptor#labels}.
|
94
|
+
#
|
95
|
+
# @example
|
96
|
+
# require "google/cloud"
|
97
|
+
#
|
98
|
+
# gcloud = Google::Cloud.new
|
99
|
+
# logging = gcloud.logging
|
100
|
+
# resource_descriptor = logging.resource_descriptors.first
|
101
|
+
# label_descriptor = resource_descriptor.labels.first
|
102
|
+
# label_descriptor.key #=> "database_id"
|
103
|
+
# label_descriptor.description #=> "The ID of the database."
|
104
|
+
#
|
105
|
+
class LabelDescriptor
|
106
|
+
##
|
107
|
+
# The key (name) of the label.
|
108
|
+
attr_reader :key
|
109
|
+
|
110
|
+
##
|
111
|
+
# The type of data that can be assigned to the label.
|
112
|
+
#
|
113
|
+
# @return [Symbol, nil] Returns `:string`, `:boolean`, `:integer`, or
|
114
|
+
# `nil` if there is no type.
|
115
|
+
#
|
116
|
+
attr_reader :type
|
117
|
+
|
118
|
+
##
|
119
|
+
# A human-readable description for the label.
|
120
|
+
attr_reader :description
|
121
|
+
|
122
|
+
##
|
123
|
+
# @private New LabelDescriptor from a Google::Api::LabelDescriptor
|
124
|
+
# object.
|
125
|
+
def self.from_grpc grpc
|
126
|
+
type_sym = { STRING: :string,
|
127
|
+
BOOL: :boolean,
|
128
|
+
INT64: :integer }[grpc.value_type]
|
129
|
+
l = new
|
130
|
+
l.instance_variable_set "@key", grpc.key
|
131
|
+
l.instance_variable_set "@type", type_sym
|
132
|
+
l.instance_variable_set "@description", grpc.description
|
133
|
+
l
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
# http://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 ResourceDescriptor
|
22
|
+
##
|
23
|
+
# ResourceDescriptor::List is a special case Array with additional
|
24
|
+
# values.
|
25
|
+
class List < DelegateClass(::Array)
|
26
|
+
##
|
27
|
+
# If not empty, indicates that there are more records that match
|
28
|
+
# the request and this value should be passed to continue.
|
29
|
+
attr_accessor :token
|
30
|
+
|
31
|
+
##
|
32
|
+
# @private Create a new ResourceDescriptor::List with an array of
|
33
|
+
# ResourceDescriptor instances.
|
34
|
+
def initialize arr = []
|
35
|
+
super arr
|
36
|
+
end
|
37
|
+
|
38
|
+
##
|
39
|
+
# Whether there is a next page of resource descriptors.
|
40
|
+
#
|
41
|
+
# @return [Boolean]
|
42
|
+
#
|
43
|
+
# @example
|
44
|
+
# require "google/cloud"
|
45
|
+
#
|
46
|
+
# gcloud = Google::Cloud.new
|
47
|
+
# logging = gcloud.logging
|
48
|
+
#
|
49
|
+
# resource_descriptors = logging.resource_descriptors
|
50
|
+
# if resource_descriptors.next?
|
51
|
+
# next_resource_descriptors = resource_descriptors.next
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
def next?
|
55
|
+
!token.nil?
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Retrieve the next page of resource descriptors.
|
60
|
+
#
|
61
|
+
# @return [Sink::List]
|
62
|
+
#
|
63
|
+
# @example
|
64
|
+
# require "google/cloud"
|
65
|
+
#
|
66
|
+
# gcloud = Google::Cloud.new
|
67
|
+
# logging = gcloud.logging
|
68
|
+
#
|
69
|
+
# resource_descriptors = logging.resource_descriptors
|
70
|
+
# if resource_descriptors.next?
|
71
|
+
# next_resource_descriptors = resource_descriptors.next
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
def next
|
75
|
+
return nil unless next?
|
76
|
+
ensure_service!
|
77
|
+
list_grpc = @service.list_resource_descriptors(
|
78
|
+
token: token, max: @max)
|
79
|
+
self.class.from_grpc list_grpc, @service
|
80
|
+
end
|
81
|
+
|
82
|
+
##
|
83
|
+
# Retrieves all resource descriptors by repeatedly loading {#next}
|
84
|
+
# until {#next?} returns `false`. Calls the given block once for each
|
85
|
+
# resource descriptor, which is passed as the parameter.
|
86
|
+
#
|
87
|
+
# An Enumerator is returned if no block is given.
|
88
|
+
#
|
89
|
+
# This method may make several API calls until all resource
|
90
|
+
# descriptors are retrieved. Be sure to use as narrow a search
|
91
|
+
# criteria as possible. Please use with caution.
|
92
|
+
#
|
93
|
+
# @param [Integer] request_limit The upper limit of API requests to
|
94
|
+
# make to load all resource descriptors. Default is no limit.
|
95
|
+
# @yield [resource_descriptor] The block for accessing each resource
|
96
|
+
# descriptor.
|
97
|
+
# @yieldparam [ResourceDescriptor] resource_descriptor The resource
|
98
|
+
# descriptor object.
|
99
|
+
#
|
100
|
+
# @return [Enumerator]
|
101
|
+
#
|
102
|
+
# @example Iterating each resource descriptor by passing a block:
|
103
|
+
# require "google/cloud"
|
104
|
+
#
|
105
|
+
# gcloud = Google::Cloud.new
|
106
|
+
# logging = gcloud.logging
|
107
|
+
# resource_descriptors = logging.resource_descriptors
|
108
|
+
#
|
109
|
+
# resource_descriptors.all do |rd|
|
110
|
+
# puts rd.type
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# @example Using the enumerator by not passing a block:
|
114
|
+
# require "google/cloud"
|
115
|
+
#
|
116
|
+
# gcloud = Google::Cloud.new
|
117
|
+
# logging = gcloud.logging
|
118
|
+
# resource_descriptors = logging.resource_descriptors
|
119
|
+
#
|
120
|
+
# all_types = resource_descriptors.all.map do |rd|
|
121
|
+
# rd.type
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# @example Limit the number of API calls made:
|
125
|
+
# require "google/cloud"
|
126
|
+
#
|
127
|
+
# gcloud = Google::Cloud.new
|
128
|
+
# logging = gcloud.logging
|
129
|
+
# resource_descriptors = logging.resource_descriptors
|
130
|
+
#
|
131
|
+
# resource_descriptors.all(request_limit: 10) do |rd|
|
132
|
+
# puts rd.type
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
def all request_limit: nil
|
136
|
+
request_limit = request_limit.to_i if request_limit
|
137
|
+
unless block_given?
|
138
|
+
return enum_for(:all, request_limit: request_limit)
|
139
|
+
end
|
140
|
+
results = self
|
141
|
+
loop do
|
142
|
+
results.each { |r| yield r }
|
143
|
+
if request_limit
|
144
|
+
request_limit -= 1
|
145
|
+
break if request_limit < 0
|
146
|
+
end
|
147
|
+
break unless results.next?
|
148
|
+
results = results.next
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
##
|
153
|
+
# @private New ResourceDescriptor::List from a
|
154
|
+
# Google::Logging::V2::ListMonitoredResourceDescriptorsResponse
|
155
|
+
# object.
|
156
|
+
def self.from_grpc grpc_list, service, max = nil
|
157
|
+
rds = new(Array(grpc_list.resource_descriptors).map do |grpc|
|
158
|
+
ResourceDescriptor.from_grpc grpc
|
159
|
+
end)
|
160
|
+
token = grpc_list.next_page_token
|
161
|
+
token = nil if token == ""
|
162
|
+
rds.instance_variable_set "@token", token
|
163
|
+
rds.instance_variable_set "@service", service
|
164
|
+
rds.instance_variable_set "@max", max
|
165
|
+
rds
|
166
|
+
end
|
167
|
+
|
168
|
+
protected
|
169
|
+
|
170
|
+
##
|
171
|
+
# Raise an error unless an active service is available.
|
172
|
+
def ensure_service!
|
173
|
+
fail "Must have active service" unless @service
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,270 @@
|
|
1
|
+
# Copyright 2016 Google Inc. All rights reserved.
|
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
|
+
# http://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/errors"
|
17
|
+
require "google/cloud/core/grpc_backoff"
|
18
|
+
require "google/logging/v2/logging_services_pb"
|
19
|
+
require "google/logging/v2/logging_config_services_pb"
|
20
|
+
require "google/logging/v2/logging_metrics_services_pb"
|
21
|
+
|
22
|
+
module Google
|
23
|
+
module Cloud
|
24
|
+
module Logging
|
25
|
+
##
|
26
|
+
# @private Represents the gRPC Logging service, including all the API
|
27
|
+
# methods.
|
28
|
+
class Service
|
29
|
+
attr_accessor :project, :credentials, :host, :retries, :timeout
|
30
|
+
|
31
|
+
##
|
32
|
+
# Creates a new Service instance.
|
33
|
+
def initialize project, credentials, host: nil, retries: nil,
|
34
|
+
timeout: nil
|
35
|
+
@project = project
|
36
|
+
@credentials = credentials
|
37
|
+
@host = host || "logging.googleapis.com"
|
38
|
+
@retries = retries
|
39
|
+
@timeout = timeout
|
40
|
+
end
|
41
|
+
|
42
|
+
def creds
|
43
|
+
GRPC::Core::ChannelCredentials.new.compose \
|
44
|
+
GRPC::Core::CallCredentials.new credentials.client.updater_proc
|
45
|
+
end
|
46
|
+
|
47
|
+
def logging
|
48
|
+
return mocked_logging if mocked_logging
|
49
|
+
@logging ||= Google::Logging::V2::LoggingServiceV2::Stub.new(
|
50
|
+
host, creds, timeout: timeout)
|
51
|
+
end
|
52
|
+
attr_accessor :mocked_logging
|
53
|
+
|
54
|
+
def sinks
|
55
|
+
return mocked_sinks if mocked_sinks
|
56
|
+
@sinks ||= Google::Logging::V2::ConfigServiceV2::Stub.new(
|
57
|
+
host, creds, timeout: timeout)
|
58
|
+
end
|
59
|
+
attr_accessor :mocked_sinks
|
60
|
+
|
61
|
+
def metrics
|
62
|
+
return mocked_metrics if mocked_metrics
|
63
|
+
@metrics ||= Google::Logging::V2::MetricsServiceV2::Stub.new(
|
64
|
+
host, creds, timeout: timeout)
|
65
|
+
end
|
66
|
+
attr_accessor :mocked_metrics
|
67
|
+
|
68
|
+
def list_entries projects: nil, filter: nil, order: nil, token: nil,
|
69
|
+
max: nil
|
70
|
+
list_params = { project_ids: Array(projects || @project),
|
71
|
+
filter: filter,
|
72
|
+
order_by: order,
|
73
|
+
page_token: token,
|
74
|
+
page_size: max
|
75
|
+
}.delete_if { |_, v| v.nil? }
|
76
|
+
|
77
|
+
list_req = Google::Logging::V2::ListLogEntriesRequest.new(list_params)
|
78
|
+
|
79
|
+
execute { logging.list_log_entries list_req }
|
80
|
+
end
|
81
|
+
|
82
|
+
def write_entries entries, log_name: nil, resource: nil, labels: nil
|
83
|
+
# Fix log names so they are the full path
|
84
|
+
entries = Array(entries).each do |entry|
|
85
|
+
entry.log_name = log_path(entry.log_name)
|
86
|
+
end
|
87
|
+
resource = resource.to_grpc if resource
|
88
|
+
labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
|
89
|
+
|
90
|
+
write_params = { entries: entries,
|
91
|
+
log_name: log_path(log_name),
|
92
|
+
resource: resource, labels: labels
|
93
|
+
}.delete_if { |_, v| v.nil? }
|
94
|
+
|
95
|
+
write_req = Google::Logging::V2::WriteLogEntriesRequest.new(
|
96
|
+
write_params)
|
97
|
+
|
98
|
+
execute { logging.write_log_entries write_req }
|
99
|
+
end
|
100
|
+
|
101
|
+
def delete_log name
|
102
|
+
delete_req = Google::Logging::V2::DeleteLogRequest.new(
|
103
|
+
log_name: log_path(name)
|
104
|
+
)
|
105
|
+
|
106
|
+
execute { logging.delete_log delete_req }
|
107
|
+
end
|
108
|
+
|
109
|
+
def list_resource_descriptors token: nil, max: nil
|
110
|
+
list_params = { page_token: token,
|
111
|
+
page_size: max
|
112
|
+
}.delete_if { |_, v| v.nil? }
|
113
|
+
|
114
|
+
list_req = \
|
115
|
+
Google::Logging::V2::ListMonitoredResourceDescriptorsRequest.new(
|
116
|
+
list_params)
|
117
|
+
|
118
|
+
execute { logging.list_monitored_resource_descriptors list_req }
|
119
|
+
end
|
120
|
+
|
121
|
+
def list_sinks token: nil, max: nil
|
122
|
+
list_params = { parent: project_path,
|
123
|
+
page_token: token,
|
124
|
+
page_size: max
|
125
|
+
}.delete_if { |_, v| v.nil? }
|
126
|
+
|
127
|
+
list_req = Google::Logging::V2::ListSinksRequest.new(list_params)
|
128
|
+
|
129
|
+
execute { sinks.list_sinks list_req }
|
130
|
+
end
|
131
|
+
|
132
|
+
def create_sink name, destination, filter, version
|
133
|
+
sink_params = {
|
134
|
+
name: name, destination: destination,
|
135
|
+
filter: filter, output_version_format: version
|
136
|
+
}.delete_if { |_, v| v.nil? }
|
137
|
+
|
138
|
+
create_req = Google::Logging::V2::CreateSinkRequest.new(
|
139
|
+
parent: project_path,
|
140
|
+
sink: Google::Logging::V2::LogSink.new(sink_params)
|
141
|
+
)
|
142
|
+
|
143
|
+
execute { sinks.create_sink create_req }
|
144
|
+
end
|
145
|
+
|
146
|
+
def get_sink name
|
147
|
+
get_req = Google::Logging::V2::GetSinkRequest.new(
|
148
|
+
sink_name: sink_path(name)
|
149
|
+
)
|
150
|
+
|
151
|
+
execute { sinks.get_sink get_req }
|
152
|
+
end
|
153
|
+
|
154
|
+
def update_sink name, destination, filter, version
|
155
|
+
sink_params = {
|
156
|
+
name: name, destination: destination,
|
157
|
+
filter: filter, output_version_format: version
|
158
|
+
}.delete_if { |_, v| v.nil? }
|
159
|
+
|
160
|
+
update_req = Google::Logging::V2::UpdateSinkRequest.new(
|
161
|
+
sink_name: sink_path(name),
|
162
|
+
sink: Google::Logging::V2::LogSink.new(sink_params)
|
163
|
+
)
|
164
|
+
|
165
|
+
execute { sinks.update_sink update_req }
|
166
|
+
end
|
167
|
+
|
168
|
+
def delete_sink name
|
169
|
+
delete_req = Google::Logging::V2::DeleteSinkRequest.new(
|
170
|
+
sink_name: sink_path(name)
|
171
|
+
)
|
172
|
+
|
173
|
+
execute { sinks.delete_sink delete_req }
|
174
|
+
end
|
175
|
+
|
176
|
+
def list_metrics token: nil, max: nil
|
177
|
+
list_params = { parent: project_path,
|
178
|
+
page_token: token,
|
179
|
+
page_size: max
|
180
|
+
}.delete_if { |_, v| v.nil? }
|
181
|
+
|
182
|
+
list_req = Google::Logging::V2::ListLogMetricsRequest.new(list_params)
|
183
|
+
|
184
|
+
execute { metrics.list_log_metrics list_req }
|
185
|
+
end
|
186
|
+
|
187
|
+
def create_metric name, filter, description
|
188
|
+
metric_params = {
|
189
|
+
name: name,
|
190
|
+
description: description,
|
191
|
+
filter: filter
|
192
|
+
}.delete_if { |_, v| v.nil? }
|
193
|
+
|
194
|
+
create_req = Google::Logging::V2::CreateLogMetricRequest.new(
|
195
|
+
parent: project_path,
|
196
|
+
metric: Google::Logging::V2::LogMetric.new(metric_params)
|
197
|
+
)
|
198
|
+
|
199
|
+
execute { metrics.create_log_metric create_req }
|
200
|
+
end
|
201
|
+
|
202
|
+
def get_metric name
|
203
|
+
get_req = Google::Logging::V2::GetLogMetricRequest.new(
|
204
|
+
metric_name: metric_path(name)
|
205
|
+
)
|
206
|
+
|
207
|
+
execute { metrics.get_log_metric get_req }
|
208
|
+
end
|
209
|
+
|
210
|
+
def update_metric name, description, filter
|
211
|
+
metric_params = {
|
212
|
+
name: name,
|
213
|
+
description: description,
|
214
|
+
filter: filter
|
215
|
+
}.delete_if { |_, v| v.nil? }
|
216
|
+
|
217
|
+
update_req = Google::Logging::V2::UpdateLogMetricRequest.new(
|
218
|
+
metric_name: metric_path(name),
|
219
|
+
metric: Google::Logging::V2::LogMetric.new(metric_params)
|
220
|
+
)
|
221
|
+
|
222
|
+
execute { metrics.update_log_metric update_req }
|
223
|
+
end
|
224
|
+
|
225
|
+
def delete_metric name
|
226
|
+
delete_req = Google::Logging::V2::DeleteLogMetricRequest.new(
|
227
|
+
metric_name: metric_path(name)
|
228
|
+
)
|
229
|
+
|
230
|
+
execute { metrics.delete_log_metric delete_req }
|
231
|
+
end
|
232
|
+
|
233
|
+
def inspect
|
234
|
+
"#{self.class}(#{@project})"
|
235
|
+
end
|
236
|
+
|
237
|
+
protected
|
238
|
+
|
239
|
+
def project_path
|
240
|
+
"projects/#{@project}"
|
241
|
+
end
|
242
|
+
|
243
|
+
def log_path log_name
|
244
|
+
return nil if log_name.nil?
|
245
|
+
return log_name if log_name.empty?
|
246
|
+
return log_name if log_name.to_s.include? "/"
|
247
|
+
"#{project_path}/logs/#{log_name}"
|
248
|
+
end
|
249
|
+
|
250
|
+
def sink_path sink_name
|
251
|
+
return sink_name if sink_name.to_s.include? "/"
|
252
|
+
"#{project_path}/sinks/#{sink_name}"
|
253
|
+
end
|
254
|
+
|
255
|
+
def metric_path metric_name
|
256
|
+
return metric_name if metric_name.to_s.include? "/"
|
257
|
+
"#{project_path}/metrics/#{metric_name}"
|
258
|
+
end
|
259
|
+
|
260
|
+
def execute
|
261
|
+
Google::Cloud::Core::GrpcBackoff.new(retries: retries).execute do
|
262
|
+
yield
|
263
|
+
end
|
264
|
+
rescue GRPC::BadStatus => e
|
265
|
+
raise Google::Cloud::Error.from_error(e)
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|