google-cloud-logging 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c12b20d76a762cc4d57a454cc8464d4e301f31a
4
- data.tar.gz: 36cda33c662c40a95bafff6068adaad0b7c1df71
3
+ metadata.gz: ce60628d40d567770462e9e345948453b717dc33
4
+ data.tar.gz: 9a64aa5f9e7daf8c418186a89d7b38cd281ae78b
5
5
  SHA512:
6
- metadata.gz: 1bb3bbec95098e11bb58fe87c41edbb93f8b8fb36dadfbe627434fdf7540e4f1dab64d79215d6da4e97b280fc15ac923cc686e53b3a3c6f7aba0f5a657095148
7
- data.tar.gz: '059dfed7ab14d14903c65824489e968b001947e7b13be4671aa69b9719ab3c7e701c10d7cce1bed08c20c8b787d93af102c1eb583174c92ea8654b47b828155f'
6
+ metadata.gz: 5dbf4f38b4eafd181fe939fd1c77fe37a77ca4dd56faaab440aaa5c7161a25554f80df0a0cf2585038eeaceabec0dd7d315b3450b026634099035569673e6526
7
+ data.tar.gz: c45e64d63897784f481b6b1e61ff6486d430ed03bf3f25d1e78d48e4b19e5bc03a349f4b19f9075190921c802d9c7d3f01cd422e7ed79359c18a723998247aeb
@@ -15,6 +15,7 @@
15
15
 
16
16
  require "google-cloud-logging"
17
17
  require "google/cloud/logging/project"
18
+ require "stackdriver/core"
18
19
 
19
20
  module Google
20
21
  module Cloud
@@ -328,6 +329,12 @@ module Google
328
329
  # ```
329
330
  #
330
331
  module Logging
332
+ # Initialize :error_reporting as a nested Configuration under
333
+ # Google::Cloud if haven't already
334
+ unless Google::Cloud.configure.option? :logging
335
+ Google::Cloud.configure.add_options logging: :monitored_resource
336
+ end
337
+
331
338
  ##
332
339
  # Creates a new object for connecting to the Stackdriver Logging service.
333
340
  # Each call creates a new connection.
@@ -379,6 +386,23 @@ module Google
379
386
  project, credentials, timeout: timeout,
380
387
  client_config: client_config))
381
388
  end
389
+
390
+ ##
391
+ # Configure the Google::Cloud::Logging::Middleware when used in a
392
+ # Rack-based application.
393
+ #
394
+ # See the [Configuration
395
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/stackdriver/guides/instrumentation_configuration)
396
+ # for full configuration parameters.
397
+ #
398
+ # @return [Stackdriver::Core::Configuration] The configuration object
399
+ # the Google::Cloud::Logging module uses.
400
+ #
401
+ def self.configure
402
+ yield Google::Cloud.configure.logging if block_given?
403
+
404
+ Google::Cloud.configure.logging
405
+ end
382
406
  end
383
407
  end
384
408
  end
@@ -82,6 +82,11 @@ module Google
82
82
  # attribute for API compatibility with the standard Logger.
83
83
  attr_accessor :datetime_format
84
84
 
85
+ ##
86
+ # The project ID this logger is sending data to. If set, this value is
87
+ # used to set the trace field of log entries.
88
+ attr_accessor :project
89
+
85
90
  ##
86
91
  # This logger treats progname as an alias for log_name.
87
92
  def progname= name
@@ -146,6 +151,10 @@ module Google
146
151
  # Unused, but present for API compatibility
147
152
  @formatter = ::Logger::Formatter.new
148
153
  @datetime_format = ""
154
+
155
+ # The writer is usually a Project or AsyncWriter.
156
+ logging = @writer.respond_to?(:logging) ? @writer.logging : @writer
157
+ @project = logging.project if logging.respond_to? :project
149
158
  end
150
159
 
151
160
  ##
@@ -454,19 +463,34 @@ module Google
454
463
  e.payload = message
455
464
  end
456
465
 
457
- # merge input labels and request info
458
- merged_labels = {}
459
466
  actual_log_name = log_name
460
467
  info = request_info
461
468
  if info
462
469
  actual_log_name = info.log_name || actual_log_name
463
- merged_labels["traceId"] = info.trace_id unless info.trace_id.nil?
470
+ unless info.trace_id.nil? || @project.nil?
471
+ entry.trace = "projects/#{@project}/traces/#{info.trace_id}"
472
+ end
464
473
  end
465
- merged_labels = labels.merge(merged_labels) unless labels.nil?
466
474
 
467
475
  writer.write_entries entry, log_name: actual_log_name,
468
476
  resource: resource,
469
- labels: merged_labels
477
+ labels: entry_labels
478
+ end
479
+
480
+ ##
481
+ # @private generate the labels hash for a log entry.
482
+ def entry_labels
483
+ merged_labels = {}
484
+ info = request_info
485
+
486
+ if info && !info.trace_id.nil?
487
+ merged_labels["traceId"] = info.trace_id
488
+ if Google::Cloud.env.app_engine?
489
+ merged_labels["appengine.googleapis.com/trace_id"] = info.trace_id
490
+ end
491
+ end
492
+
493
+ labels.nil? ? merged_labels : labels.merge(merged_labels)
470
494
  end
471
495
 
472
496
  ##
@@ -42,34 +42,30 @@ module Google
42
42
  # to track Stackdriver request trace ID. It also properly sets
43
43
  # env["rack.logger"] to this assigned logger for accessing. If not
44
44
  # specified, a default logger with be used.
45
- # @param [String] project_id Project identifier for the Stackdriver
46
- # Logging service. Used to create a default logger if one isn't
47
- # specified and a service account is used for authentication.
48
- # Optional.
49
- # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
50
- # file path the file must be readable. Used to create a default logger
51
- # if one isn't specified and a service account is used for
52
- # authentication. Optional.
53
- # @param [Hash] log_name_map A map from URI path to log name override.
54
- # The path may be a string or regex. If a request path matches an
55
- # entry in this map, the log name is set accordingly, otherwise the
56
- # logger's default log_name is used.
45
+ # @param [Hash] *kwargs Hash of configuration settings. Used for
46
+ # backward API compatibility. See the [Configuration
47
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/stackdriver/guides/instrumentation_configuration)
48
+ # for the prefered way to set configuration parameters.
57
49
  #
58
50
  # @return [Google::Cloud::Logging::Middleware] A new
59
51
  # Google::Cloud::Logging::Middleware instance
60
52
  #
61
- def initialize app, logger: nil, project_id: nil, keyfile: nil,
62
- log_name_map: DEFAULT_LOG_NAME_MAP
53
+ def initialize app, logger: nil, **kwargs
63
54
  @app = app
55
+
56
+ load_config kwargs
57
+
64
58
  if logger
65
59
  @logger = logger
66
60
  else
67
- logging = Google::Cloud::Logging.new project: project_id,
68
- keyfile: keyfile
69
- resource = Middleware.build_monitored_resource
70
- @logger = logging.logger DEFAULT_LOG_NAME, resource
61
+ log_name = configuration.log_name
62
+ logging = Logging.new project: configuration.project_id,
63
+ keyfile: configuration.keyfile
64
+ resource = Middleware.build_monitored_resource(
65
+ configuration.monitored_resource.type,
66
+ configuration.monitored_resource.labels)
67
+ @logger = logging.logger log_name, resource
71
68
  end
72
- @log_name_map = log_name_map
73
69
  end
74
70
 
75
71
  ##
@@ -116,10 +112,11 @@ module Google
116
112
  # no override.
117
113
  #
118
114
  def get_log_name env
119
- return nil unless @log_name_map
115
+ log_name_map = configuration.log_name_map
116
+ return nil unless log_name_map
120
117
  path = "#{env['SCRIPT_NAME']}#{env['PATH_INFO']}"
121
118
  path = "/#{path}" unless path.start_with? "/"
122
- @log_name_map.each do |k, v|
119
+ log_name_map.each do |k, v|
123
120
  return v if k === path
124
121
  end
125
122
  nil
@@ -249,6 +246,40 @@ module Google
249
246
  end
250
247
 
251
248
  private_class_method :default_monitored_resource
249
+
250
+ private
251
+
252
+ ##
253
+ # Consolidate configurations from various sources. Also set
254
+ # instrumentation config parameters to default values if not set
255
+ # already.
256
+ #
257
+ def load_config **kwargs
258
+ configuration.project_id = kwargs[:project_id] ||
259
+ configuration.project_id
260
+ configuration.keyfile = kwargs[:keyfile] ||
261
+ configuration.keyfile
262
+ configuration.log_name_map ||= kwargs[:log_name_map] ||
263
+ configuration.log_name_map
264
+
265
+ init_default_config
266
+ end
267
+
268
+ ##
269
+ # Fallback to default configuration values if not defined already
270
+ def init_default_config
271
+ configuration.project_id ||= Cloud.configure.project_id ||
272
+ Logging::Project.default_project
273
+ configuration.keyfile ||= Cloud.configure.keyfile
274
+ configuration.log_name ||= DEFAULT_LOG_NAME
275
+ configuration.log_name_map ||= DEFAULT_LOG_NAME_MAP
276
+ end
277
+
278
+ ##
279
+ # @private Get Google::Cloud::Logging.configure
280
+ def configuration
281
+ Google::Cloud::Logging.configure
282
+ end
252
283
  end
253
284
  end
254
285
  end
@@ -34,25 +34,10 @@ module Google
34
34
  #
35
35
  # When loaded, the {Google::Cloud::Logging::Middleware} will be inserted
36
36
  # before the `Rails::Rack::Logger Middleware`, which allows it to set the
37
- # `env['rack.logger']` in place of Rails's default logger. The Railtie
38
- # will also initialize the logger with correct GCP `project_id`
39
- # and `keyfile` if they are defined in the Rails `environment.rb` file as
40
- # follows:
41
- #
42
- # ```ruby
43
- # config.google_cloud.logging.project_id = "my-gcp-project"
44
- # config.google_cloud.logging.keyfile = "/path/to/secret.json"
45
- # ```
46
- #
47
- # or
48
- #
49
- # ```ruby
50
- # config.google_cloud.project_id = "my-gcp-project"
51
- # config.google_cloud.keyfile = "/path/to/secret.json"
52
- # ```
53
- #
54
- # If omitted, project_id will be initialized with default environment
55
- # variables.
37
+ # `env['rack.logger']` in place of Rails's default logger.
38
+ # See the [Configuration
39
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/stackdriver/guides/instrumentation_configuration)
40
+ # on how to configure the Railtie and Middleware.
56
41
  #
57
42
  class Railtie < ::Rails::Railtie
58
43
  config.google_cloud = ::ActiveSupport::OrderedOptions.new unless
@@ -64,60 +49,94 @@ module Google
64
49
  ::ActiveSupport::OrderedOptions.new
65
50
 
66
51
  initializer "Stackdriver.Logging", before: :initialize_logger do |app|
67
- if self.class.use_logging? app.config
68
- logging_config = Railtie.parse_rails_config config
69
-
70
- project_id = logging_config[:project_id]
71
- keyfile = logging_config[:keyfile]
72
- resource_type = logging_config[:resource_type]
73
- resource_labels = logging_config[:resource_labels]
74
- log_name = logging_config[:log_name]
75
-
76
- logging = Google::Cloud::Logging.new project: project_id,
77
- keyfile: keyfile
78
- resource =
79
- Logging::Middleware.build_monitored_resource resource_type,
80
- resource_labels
81
-
82
- app.config.logger = logging.logger log_name, resource
83
- app.middleware.insert_before Rails::Rack::Logger,
84
- Google::Cloud::Logging::Middleware,
85
- logger: app.config.logger
86
- end
52
+ self.class.consolidate_rails_config app.config
53
+
54
+ self.class.init_middleware app if Cloud.configure.use_logging
87
55
  end
88
56
 
89
57
  ##
90
- # Determine whether to use Stackdriver Logging or not.
91
- #
92
- # Returns `true` if Stackdriver Logging is enabled for this application.
93
- # That is, if all of the following are true:
94
- #
95
- # * A valid GCP `project_id` is available, either because the
96
- # application is hosted on Google Cloud or because it is set in the
97
- # configuration.
98
- # * The API is able to authenticate, again either because the
99
- # application is hosted on Google Cloud or because an appropriate
100
- # keyfile is provided in the configuration.
101
- # * Either the Rails environment is set to `production` or the
102
- # `config.google_cloud.use_logging` configuration is explicitly set to
103
- # `true`.
58
+ # @private Init Logging integration for Rails. Setup configuration and
59
+ # insert the Middleware.
60
+ def self.init_middleware app
61
+ project_id = Logging.configure.project_id
62
+ keyfile = Logging.configure.keyfile
63
+ resource_type = Logging.configure.monitored_resource.type
64
+ resource_labels = Logging.configure.monitored_resource.labels
65
+ log_name = Logging.configure.log_name
66
+
67
+ logging = Google::Cloud::Logging.new project: project_id,
68
+ keyfile: keyfile
69
+ resource =
70
+ Logging::Middleware.build_monitored_resource resource_type,
71
+ resource_labels
72
+
73
+ app.config.logger = logging.logger log_name, resource
74
+ app.middleware.insert_before Rails::Rack::Logger,
75
+ Google::Cloud::Logging::Middleware,
76
+ logger: app.config.logger
77
+ end
78
+
79
+ ##
80
+ # @private Consolidate Rails configuration into Logging instrumentation
81
+ # configuration. Also consolidate the `use_logging` setting by verifying
82
+ # credentials and Rails environment. The `use_logging` setting will be
83
+ # true if credentials are valid, and the setting is manually set to true
84
+ # or Rails is in production environment.
104
85
  #
105
86
  # @param [Rails::Railtie::Configuration] config The
106
- # `Rails.application.config`
107
- #
108
- # @return [Boolean] Whether to use Stackdriver Logging
87
+ # Rails.application.config
109
88
  #
110
- def self.use_logging? config
111
- logging_config = Railtie.parse_rails_config config
89
+ def self.consolidate_rails_config config
90
+ merge_rails_config config
112
91
 
113
- # Return false if config.google_cloud.use_logging is explicitly false
114
- use_logging = logging_config[:use_logging]
115
- return false if use_logging == false
92
+ init_default_config
116
93
 
117
- project_id = logging_config[:project_id] ||
118
- Google::Cloud::Logging::Project.default_project
119
- keyfile = logging_config[:keyfile]
94
+ # Done if Google::Cloud.configure.use_logging is explicitly false
95
+ return if Google::Cloud.configure.use_logging == false
96
+
97
+ # Verify credentials and set use_error_reporting to false if
98
+ # credentials are invalid
99
+ unless valid_credentials? Logging.configure.project_id,
100
+ Logging.configure.keyfile
101
+ Cloud.configure.use_logging = false
102
+ return
103
+ end
120
104
 
105
+ # Otherwise set use_logging to true if Rails is running in production
106
+ Google::Cloud.configure.use_logging ||= Rails.env.production?
107
+ end
108
+
109
+ ##
110
+ # @private Merge Rails configuration into Logging instrumentation
111
+ # configuration.
112
+ def self.merge_rails_config rails_config
113
+ gcp_config = rails_config.google_cloud
114
+ logging_config = gcp_config.logging
115
+
116
+ Cloud.configure.use_logging ||= gcp_config.use_logging
117
+ Logging.configure do |config|
118
+ config.project_id ||= logging_config.project_id ||
119
+ gcp_config.project_id
120
+ config.keyfile ||= logging_config.keyfile || gcp_config.keyfile
121
+ config.log_name ||= logging_config.log_name
122
+ config.log_name_map ||= logging_config.log_name_map
123
+ config.monitored_resource.type ||=
124
+ logging_config.monitored_resource.type
125
+ config.monitored_resource.labels ||=
126
+ logging_config.monitored_resource.labels.to_h
127
+ end
128
+ end
129
+
130
+ ##
131
+ # Fallback to default config values if config parameters not provided.
132
+ def self.init_default_config
133
+ Logging.configure.project_id ||= Logging::Project.default_project
134
+ Logging.configure.log_name ||= Middleware::DEFAULT_LOG_NAME
135
+ end
136
+
137
+ ##
138
+ # @private Verify credentials
139
+ def self.valid_credentials? project_id, keyfile
121
140
  # Try authenticate authorize client API. Return false if unable to
122
141
  # authorize.
123
142
  begin
@@ -136,27 +155,12 @@ module Google
136
155
  return false
137
156
  end
138
157
 
139
- # Otherwise default to true if Rails is running in production or
140
- # config.google_cloud.use_logging is true
141
- Rails.env.production? || use_logging
158
+ true
142
159
  end
143
160
 
144
- ##
145
- # @private Helper method to parse rails config into a flattened hash
146
- def self.parse_rails_config config
147
- gcp_config = config.google_cloud
148
- logging_config = gcp_config.logging
149
- use_logging =
150
- gcp_config.key?(:use_logging) ? gcp_config.use_logging : nil
151
- {
152
- project_id: logging_config.project_id || gcp_config.project_id,
153
- keyfile: logging_config.keyfile || gcp_config.keyfile,
154
- resource_type: logging_config.monitored_resource.type,
155
- resource_labels: logging_config.monitored_resource.labels,
156
- log_name: logging_config.log_name || Middleware::DEFAULT_LOG_NAME,
157
- use_logging: use_logging
158
- }
159
- end
161
+ private_class_method :merge_rails_config,
162
+ :init_default_config,
163
+ :valid_credentials?
160
164
  end
161
165
  end
162
166
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Logging
19
- VERSION = "1.0.1"
19
+ VERSION = "1.1.0"
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-21 00:00:00.000000000 Z
12
+ date: 2017-05-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '1.0'
34
+ version: '1.1'
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '1.0'
41
+ version: '1.1'
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: google-gax
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -266,7 +266,7 @@ files:
266
266
  - lib/google/logging/v2/logging_metrics_services_pb.rb
267
267
  - lib/google/logging/v2/logging_pb.rb
268
268
  - lib/google/logging/v2/logging_services_pb.rb
269
- homepage: http://googlecloudplatform.github.io/google-cloud-ruby/
269
+ homepage: https://github.com/GoogleCloudPlatform/google-cloud-ruby/tree/master/google-cloud-logging
270
270
  licenses:
271
271
  - Apache-2.0
272
272
  metadata: {}
@@ -286,7 +286,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
286
286
  version: '0'
287
287
  requirements: []
288
288
  rubyforge_project:
289
- rubygems_version: 2.6.11
289
+ rubygems_version: 2.6.12
290
290
  signing_key:
291
291
  specification_version: 4
292
292
  summary: API Client library for Stackdriver Logging