Skip to content

Commit f36a306

Browse files
committed
chore(PubSub): Make logging safe in service.rb
1 parent eadaa7e commit f36a306

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

google-cloud-pubsub/lib/google/cloud/pubsub/logger_helper.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def self.logger name = ""
3131
end
3232
end
3333

34-
private
35-
3634
def self.is_logging_enabled
3735
packages = ENV["GOOGLE_SDK_RUBY_LOGGING"]&.split(",") || []
3836
packages.include?("pubsub") || packages.include?("all")
@@ -48,7 +46,17 @@ def log_batch logger_name, reason, type, num_messages, total_bytes
4846
"#{reason} triggered #{type} batch of #{num_messages} messages, a total of #{total_bytes} bytes"
4947
)
5048
end
51-
49+
50+
def log_ack_nack_safely ack_ids, type
51+
return unless Google::Cloud::PubSub.is_logging_enabled
52+
begin
53+
ack_ids.each do |ack_id|
54+
Google::Cloud::PubSub.logger("ack-nack").info "message (ackID #{ack_id}) #{type}"
55+
end
56+
rescue StandardError
57+
# Ignore all logging errors.
58+
end
59+
end
5260
end
5361
end
5462
end

google-cloud-pubsub/lib/google/cloud/pubsub/service.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module PubSub
2828
##
2929
# @private Represents the Pub/Sub service API, including IAM mixins.
3030
class Service
31+
include LoggerHelper
32+
3133
attr_accessor :project
3234
attr_accessor :credentials
3335
attr_accessor :host
@@ -142,9 +144,7 @@ def streaming_pull request_enum, options = {}
142144
##
143145
# Acknowledges receipt of a message.
144146
def acknowledge subscription, *ack_ids
145-
ack_ids.each do |ack_id|
146-
Google::Cloud::PubSub.logger("ack-nack").info "message (ackID #{ack_id}) ack"
147-
end
147+
log_ack_nack_safely ack_ids, "ack"
148148
subscription_admin.acknowledge_internal subscription: subscription_path(subscription),
149149
ack_ids: ack_ids
150150
end
@@ -153,9 +153,7 @@ def acknowledge subscription, *ack_ids
153153
# Modifies the ack deadline for a specific message.
154154
def modify_ack_deadline subscription, ids, deadline
155155
if deadline.zero?
156-
Array(ids).each do |ack_id|
157-
Google::Cloud::PubSub.logger("ack-nack").info "message (ackID #{ack_id}) nack"
158-
end
156+
log_ack_nack_safely Array(ids), "nack"
159157
end
160158
subscription_admin.modify_ack_deadline_internal subscription: subscription_path(subscription),
161159
ack_ids: Array(ids),

0 commit comments

Comments
 (0)