google-cloud-logging 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +18 -0
  3. data/AUTHENTICATION.md +178 -0
  4. data/CHANGELOG.md +407 -0
  5. data/CODE_OF_CONDUCT.md +40 -0
  6. data/CONTRIBUTING.md +188 -0
  7. data/INSTRUMENTATION.md +71 -0
  8. data/LICENSE +201 -0
  9. data/LOGGING.md +32 -0
  10. data/OVERVIEW.md +321 -0
  11. data/TROUBLESHOOTING.md +31 -0
  12. data/lib/google-cloud-logging.rb +161 -0
  13. data/lib/google/cloud/logging.rb +188 -0
  14. data/lib/google/cloud/logging/async_writer.rb +513 -0
  15. data/lib/google/cloud/logging/convert.rb +70 -0
  16. data/lib/google/cloud/logging/credentials.rb +44 -0
  17. data/lib/google/cloud/logging/entry.rb +528 -0
  18. data/lib/google/cloud/logging/entry/http_request.rb +167 -0
  19. data/lib/google/cloud/logging/entry/list.rb +178 -0
  20. data/lib/google/cloud/logging/entry/operation.rb +91 -0
  21. data/lib/google/cloud/logging/entry/source_location.rb +85 -0
  22. data/lib/google/cloud/logging/errors.rb +101 -0
  23. data/lib/google/cloud/logging/log/list.rb +156 -0
  24. data/lib/google/cloud/logging/logger.rb +633 -0
  25. data/lib/google/cloud/logging/metric.rb +168 -0
  26. data/lib/google/cloud/logging/metric/list.rb +170 -0
  27. data/lib/google/cloud/logging/middleware.rb +307 -0
  28. data/lib/google/cloud/logging/project.rb +838 -0
  29. data/lib/google/cloud/logging/rails.rb +232 -0
  30. data/lib/google/cloud/logging/resource.rb +85 -0
  31. data/lib/google/cloud/logging/resource_descriptor.rb +137 -0
  32. data/lib/google/cloud/logging/resource_descriptor/list.rb +175 -0
  33. data/lib/google/cloud/logging/service.rb +239 -0
  34. data/lib/google/cloud/logging/sink.rb +315 -0
  35. data/lib/google/cloud/logging/sink/list.rb +168 -0
  36. data/lib/google/cloud/logging/version.rb +22 -0
  37. metadata +304 -0
@@ -0,0 +1,167 @@
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
+ class Entry
20
+ ##
21
+ # # Http Request
22
+ #
23
+ # HTTP request data associated with a log entry.
24
+ #
25
+ # See also {Google::Cloud::Logging::Entry#http_request}.
26
+ #
27
+ class HttpRequest
28
+ ##
29
+ # @private Create an empty HttpRequest object.
30
+ def initialize; end
31
+
32
+ ##
33
+ # The request method. Examples: `"GET"`, `"HEAD"`, `"PUT"`, `"POST"`.
34
+ # (String)
35
+ attr_accessor :request_method
36
+
37
+ ##
38
+ # @overload method()
39
+ # Deprecated. Use {#request_method} instead.
40
+ #
41
+ # The request method. Examples: `"GET"`, `"HEAD"`, `"PUT"`,
42
+ # `"POST"`. (String)
43
+ def method *args
44
+ # Call Object#method when args are present.
45
+ return super unless args.empty?
46
+
47
+ request_method
48
+ end
49
+
50
+ ##
51
+ # @overload method()
52
+ # Deprecated. Use {#request_method=} instead.
53
+ #
54
+ # The request method. Examples: `"GET"`, `"HEAD"`, `"PUT"`,
55
+ # `"POST"`. (String)
56
+ def method= new_request_method
57
+ self.request_method = new_request_method
58
+ end
59
+
60
+ ##
61
+ # The URL. The scheme (http, https), the host name, the path and the
62
+ # query portion of the URL that was requested. Example:
63
+ # `"http://example.com/some/info?color=red"`. (String)
64
+ attr_accessor :url
65
+
66
+ ##
67
+ # The size of the HTTP request message in bytes, including the request
68
+ # headers and the request body. (Integer)
69
+ attr_accessor :size
70
+
71
+ ##
72
+ # The response code indicating the status of response. Examples:
73
+ # `200`, `404`. (Integer)
74
+ attr_accessor :status
75
+
76
+ ##
77
+ # The size of the HTTP response message sent back to the client, in
78
+ # bytes, including the response headers and the response body.
79
+ # (Integer)
80
+ attr_accessor :response_size
81
+
82
+ ##
83
+ # The user agent sent by the client. Example: `"Mozilla/4.0
84
+ # (compatible; MSIE 6.0; Windows 98; Q312461; .NET CLR 1.0.3705)"`.
85
+ # (String)
86
+ attr_accessor :user_agent
87
+
88
+ ##
89
+ # The IP address (IPv4 or IPv6) of the client that issued the HTTP
90
+ # request. Examples: `"192.168.1.1"`, `"FE80::0202:B3FF:FE1E:8329"`.
91
+ # (String)
92
+ attr_accessor :remote_ip
93
+
94
+ ##
95
+ # The referer URL of the request, as defined in [HTTP/1.1 Header Field
96
+ # Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
97
+ # (String)
98
+ attr_accessor :referer
99
+
100
+ ##
101
+ # Whether an entity was served from cache (with or without
102
+ # validation). (Boolean)
103
+ attr_accessor :cache_hit
104
+
105
+ ##
106
+ # Whether the response was validated with the origin server before
107
+ # being served from cache. This field is only meaningful if
108
+ # `cache_hit` is `true`. (Boolean)
109
+ attr_accessor :validated
110
+
111
+ ##
112
+ # @private Determines if the HttpRequest has any data.
113
+ def empty?
114
+ method.nil? &&
115
+ url.nil? &&
116
+ size.nil? &&
117
+ status.nil? &&
118
+ response_size.nil? &&
119
+ user_agent.nil? &&
120
+ remote_ip.nil? &&
121
+ referer.nil? &&
122
+ cache_hit.nil? &&
123
+ validated.nil?
124
+ end
125
+
126
+ ##
127
+ # @private Exports the HttpRequest to a
128
+ # Google::Cloud::Logging::Type::HttpRequest object.
129
+ def to_grpc
130
+ return nil if empty?
131
+ Google::Cloud::Logging::Type::HttpRequest.new(
132
+ request_method: request_method.to_s,
133
+ request_url: url.to_s,
134
+ request_size: size.to_i,
135
+ status: status.to_i,
136
+ response_size: response_size.to_i,
137
+ user_agent: user_agent.to_s,
138
+ remote_ip: remote_ip.to_s,
139
+ referer: referer.to_s,
140
+ cache_hit: !(!cache_hit),
141
+ cache_validated_with_origin_server: !(!validated)
142
+ )
143
+ end
144
+
145
+ ##
146
+ # @private New HttpRequest from a Google::Cloud::Logging::Type::HttpRequest
147
+ # object.
148
+ def self.from_grpc grpc
149
+ return new if grpc.nil?
150
+ new.tap do |h|
151
+ h.request_method = grpc.request_method
152
+ h.url = grpc.request_url
153
+ h.size = grpc.request_size
154
+ h.status = grpc.status
155
+ h.response_size = grpc.response_size
156
+ h.user_agent = grpc.user_agent
157
+ h.remote_ip = grpc.remote_ip
158
+ h.referer = grpc.referer
159
+ h.cache_hit = grpc.cache_hit
160
+ h.validated = grpc.cache_validated_with_origin_server
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,178 @@
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 Entry
22
+ ##
23
+ # Entry::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 Entry::List with an array of Entry instances.
32
+ def initialize arr = []
33
+ super arr
34
+ end
35
+
36
+ ##
37
+ # Whether there is a next page of entries.
38
+ #
39
+ # @return [Boolean]
40
+ #
41
+ # @example
42
+ # require "google/cloud/logging"
43
+ #
44
+ # logging = Google::Cloud::Logging.new
45
+ #
46
+ # entries = logging.entries
47
+ # if entries.next?
48
+ # next_entries = entries.next
49
+ # end
50
+ #
51
+ def next?
52
+ !token.nil?
53
+ end
54
+
55
+ ##
56
+ # Retrieve the next page of entries.
57
+ #
58
+ # @return [Sink::List]
59
+ #
60
+ # @example
61
+ # require "google/cloud/logging"
62
+ #
63
+ # logging = Google::Cloud::Logging.new
64
+ #
65
+ # entries = logging.entries
66
+ # if entries.next?
67
+ # next_entries = entries.next
68
+ # end
69
+ #
70
+ def next
71
+ return nil unless next?
72
+ ensure_service!
73
+ grpc = @service.list_entries token: token, resources: @resources,
74
+ filter: @filter, order: @order,
75
+ max: @max, projects: @projects
76
+ self.class.from_grpc grpc, @service, resources: @resources,
77
+ filter: @filter, order: @order,
78
+ max: @max, projects: @projects
79
+ end
80
+
81
+ ##
82
+ # Retrieves remaining results by repeatedly invoking {#next} until
83
+ # {#next?} returns `false`. Calls the given block once for each
84
+ # result, which is passed as the argument to the block.
85
+ #
86
+ # An Enumerator is returned if no block is given.
87
+ #
88
+ # This method will make repeated API calls until all remaining results
89
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
90
+ # over the results returned by a single API call.) Use with caution.
91
+ #
92
+ # @param [Integer] request_limit The upper limit of API requests to
93
+ # make to load all log entries. Default is no limit.
94
+ # @yield [entry] The block for accessing each log entry.
95
+ # @yieldparam [Entry] entry The log entry object.
96
+ #
97
+ # @return [Enumerator]
98
+ #
99
+ # @example Iterating each log entry by passing a block:
100
+ # require "google/cloud/logging"
101
+ #
102
+ # logging = Google::Cloud::Logging.new
103
+ # entries = logging.entries order: "timestamp desc"
104
+ #
105
+ # entries.all do |e|
106
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
107
+ # end
108
+ #
109
+ # @example Using the enumerator by not passing a block:
110
+ # require "google/cloud/logging"
111
+ #
112
+ # logging = Google::Cloud::Logging.new
113
+ # entries = logging.entries order: "timestamp desc"
114
+ #
115
+ # all_payloads = entries.all.map do |entry|
116
+ # entry.payload
117
+ # end
118
+ #
119
+ # @example Limit the number of API calls made:
120
+ # require "google/cloud/logging"
121
+ #
122
+ # logging = Google::Cloud::Logging.new
123
+ # entries = logging.entries order: "timestamp desc"
124
+ #
125
+ # entries.all(request_limit: 10) do |e|
126
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
127
+ # end
128
+ #
129
+ def all request_limit: nil
130
+ request_limit = request_limit.to_i if request_limit
131
+ unless block_given?
132
+ return enum_for :all, request_limit: request_limit
133
+ end
134
+ results = self
135
+ loop do
136
+ results.each { |r| yield r }
137
+ if request_limit
138
+ request_limit -= 1
139
+ break if request_limit < 0
140
+ end
141
+ break unless results.next?
142
+ results = results.next
143
+ end
144
+ end
145
+
146
+ ##
147
+ # @private New Entry::List from a
148
+ # Google::Cloud::Logging::V2::ListLogEntryResponse object.
149
+ def self.from_grpc grpc_list, service, resources: nil, filter: nil,
150
+ order: nil, max: nil, projects: nil
151
+ entries = new(Array(grpc_list.entries).map do |grpc_entry|
152
+ Entry.from_grpc grpc_entry
153
+ end)
154
+ token = grpc_list.next_page_token
155
+ token = nil if token == "".freeze
156
+ entries.instance_variable_set :@token, token
157
+ entries.instance_variable_set :@service, service
158
+ entries.instance_variable_set :@projects, projects
159
+ entries.instance_variable_set :@resources, resources
160
+ entries.instance_variable_set :@filter, filter
161
+ entries.instance_variable_set :@order, order
162
+ entries.instance_variable_set :@max, max
163
+ entries
164
+ end
165
+
166
+ protected
167
+
168
+ ##
169
+ # @private Raise an error unless an active connection to the service
170
+ # is available.
171
+ def ensure_service!
172
+ raise "Must have active connection to service" unless @service
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,91 @@
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
+ class Entry
20
+ ##
21
+ # # Operation
22
+ #
23
+ # Additional information about a potentially long-running operation with
24
+ # which a log entry is associated.
25
+ #
26
+ # See also {Google::Cloud::Logging::Entry#operation}.
27
+ #
28
+ class Operation
29
+ ##
30
+ # @private Create an empty Operation object.
31
+ def initialize; end
32
+
33
+ ##
34
+ # An arbitrary operation identifier. Log entries with the same
35
+ # identifier are assumed to be part of the same operation.
36
+ attr_accessor :id
37
+
38
+ ##
39
+ # An arbitrary producer identifier. The combination of `id` and
40
+ # `producer` must be globally unique. Examples for `producer`:
41
+ # `"MyDivision.MyBigCompany.com"`,
42
+ # `"github.com/MyProject/MyApplication"`.
43
+ attr_accessor :producer
44
+
45
+ ##
46
+ # Set this to `true` if this is the first log entry in the operation.
47
+ attr_accessor :first
48
+
49
+ ##
50
+ # Set this to `true` if this is the last log entry in the operation.
51
+ attr_accessor :last
52
+
53
+ ##
54
+ # @private Determines if the Operation has any data.
55
+ def empty?
56
+ id.nil? &&
57
+ producer.nil? &&
58
+ first.nil? &&
59
+ last.nil?
60
+ end
61
+
62
+ ##
63
+ # @private Exports the Operation to a
64
+ # Google::Cloud::Logging::V2::LogEntryOperation object.
65
+ def to_grpc
66
+ return nil if empty?
67
+ Google::Cloud::Logging::V2::LogEntryOperation.new(
68
+ id: id.to_s,
69
+ producer: producer.to_s,
70
+ first: !(!first),
71
+ last: !(!last)
72
+ )
73
+ end
74
+
75
+ ##
76
+ # @private New Google::Cloud::Logging::Entry::Operation from a
77
+ # Google::Cloud::Logging::V2::LogEntryOperation object.
78
+ def self.from_grpc grpc
79
+ return new if grpc.nil?
80
+ new.tap do |o|
81
+ o.id = grpc.id
82
+ o.producer = grpc.producer
83
+ o.first = grpc.first
84
+ o.last = grpc.last
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,85 @@
1
+ # Copyright 2017 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
+ class Entry
20
+ ##
21
+ # # SourceLocation
22
+ #
23
+ # Additional information about the source code location that produced
24
+ # the log entry.
25
+ #
26
+ # See also {Google::Cloud::Logging::Entry#source_location}.
27
+ #
28
+ class SourceLocation
29
+ ##
30
+ # @private Create an empty SourceLocation object.
31
+ def initialize; end
32
+
33
+ ##
34
+ # Source file name. Depending on the runtime environment, this might
35
+ # be a simple name or a fully-qualified name. Optional.
36
+ attr_accessor :file
37
+
38
+ ##
39
+ # Line within the source file. 1-based; `0` indicates no line number
40
+ # available. Optional.
41
+ attr_accessor :line
42
+
43
+ ##
44
+ # Human-readable name of the function or method being invoked, with
45
+ # optional context such as the class or package name. This information
46
+ # may be used in contexts such as the logs viewer, where a file and
47
+ # line number are less meaningful. Optional.
48
+ attr_accessor :function
49
+
50
+ ##
51
+ # @private Determines if the SourceLocation has any data.
52
+ def empty?
53
+ file.nil? &&
54
+ line.nil? &&
55
+ function.nil?
56
+ end
57
+
58
+ ##
59
+ # @private Exports the SourceLocation to a
60
+ # Google::Cloud::Logging::V2::LogEntrySourceLocation object.
61
+ def to_grpc
62
+ return nil if empty?
63
+ Google::Cloud::Logging::V2::LogEntrySourceLocation.new(
64
+ file: file.to_s,
65
+ line: line,
66
+ function: function.to_s
67
+ )
68
+ end
69
+
70
+ ##
71
+ # @private New Google::Cloud::Logging::Entry::SourceLocation from a
72
+ # Google::Cloud::Logging::V2::LogEntrySourceLocation object.
73
+ def self.from_grpc grpc
74
+ return new if grpc.nil?
75
+ new.tap do |o|
76
+ o.file = grpc.file
77
+ o.line = grpc.line
78
+ o.function = grpc.function
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end