lib/google/cloud/logging/logger.rb in google-cloud-logging-1.5.5 vs lib/google/cloud/logging/logger.rb in google-cloud-logging-1.5.6
- old
+ new
@@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
require "logger"
-require "monitor"
require "thread"
+require "concurrent"
module Google
module Cloud
module Logging
##
@@ -51,12 +51,10 @@
#
# payload = { "stats" => { "a" => 8, "b" => 12.5} }
# logger.info payload
#
class Logger
- include MonitorMixin
-
##
# A RequestInfo represents data about the request being handled by the
# current thread. It is used to configure logs coming from that thread.
#
# The trace_id is a String that controls the trace ID sent with the log
@@ -120,16 +118,19 @@
##
# A Hash of Thread IDs to Stackdriver request trace ID. The
# Stackdriver trace ID is a shared request identifier across all
# Stackdriver services.
#
+ # This method is deprecated and returns a Hash containing only the
+ # current Thread ID/trace_id now.
+ #
# @deprecated Use request_info
#
def trace_ids
- synchronize do
- @request_info_map.inject({}) { |r, (k, v)| r[k] = v.trace_id }
- end
+ current_request_info = request_info
+ return {} if current_request_info.nil?
+ { current_thread_id => current_request_info.trace_id }
end
##
# Create a new Logger instance.
#
@@ -170,22 +171,20 @@
@writer = writer
@log_name = log_name
@resource = resource
@labels = labels || {}
@level = 0 # DEBUG is the default behavior
- @request_info_map = {}
+ @request_info_var = Concurrent::ThreadLocalVar.new
@closed = false
# Unused, but present for API compatibility
@formatter = ::Logger::Formatter.new
@datetime_format = ""
@silencer = true
# The writer is usually a Project or AsyncWriter.
logging = @writer.respond_to?(:logging) ? @writer.logging : @writer
@project = logging.project if logging.respond_to? :project
-
- super() # to init MonitorMixin
end
##
# Log a `DEBUG` entry.
#
@@ -459,38 +458,31 @@
# available.
#
def add_request_info info: nil, env: nil, trace_id: nil, log_name: nil
info ||= RequestInfo.new trace_id, log_name, env
- synchronize do
- @request_info_map[current_thread_id] = info
+ @request_info_var.value = info
- # Start removing old entries if hash gets too large.
- # This should never happen, because middleware should automatically
- # remove entries when a request is finished
- @request_info_map.shift while @request_info_map.size > 10_000
- end
-
info
end
##
# Get the request data for the current Thread
#
# @return [RequestInfo, nil] The request data for the current thread,
# or `nil` if there is no data set.
#
def request_info
- synchronize { @request_info_map[current_thread_id] }
+ @request_info_var.value
end
##
# Untrack the RequestInfo that's associated with current Thread
#
# @return [RequestInfo] The info that's being deleted
#
def delete_request_info
- synchronize { @request_info_map.delete current_thread_id }
+ @request_info_var.value = nil
end
##
# @deprecated Use delete_request_info
alias delete_trace_id delete_request_info