mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Combine rackstash helpers into one Rackstash::Utils module
This commit is contained in:
parent
9b20c3b385
commit
ef57352e1f
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 Holger Just
|
# Copyright 2017-2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
@ -12,7 +12,7 @@ require 'time'
|
|||||||
require 'rackstash/encoder'
|
require 'rackstash/encoder'
|
||||||
require 'rackstash/encoder/helper/fields_map'
|
require 'rackstash/encoder/helper/fields_map'
|
||||||
require 'rackstash/encoder/helper/message'
|
require 'rackstash/encoder/helper/message'
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Encoder
|
module Encoder
|
||||||
@ -34,7 +34,7 @@ module Rackstash
|
|||||||
class GELF
|
class GELF
|
||||||
include Rackstash::Encoder::Helper::FieldsMap
|
include Rackstash::Encoder::Helper::FieldsMap
|
||||||
include Rackstash::Encoder::Helper::Message
|
include Rackstash::Encoder::Helper::Message
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# The default mapping of GELF fields (the keys) to fields in the final
|
# The default mapping of GELF fields (the keys) to fields in the final
|
||||||
# Rackstash event hash (the value). You can overwrite this mapping by
|
# Rackstash event hash (the value). You can overwrite this mapping by
|
||||||
@ -92,7 +92,7 @@ module Rackstash
|
|||||||
# > the name of the host, source or application that sent this message;
|
# > the name of the host, source or application that sent this message;
|
||||||
# > MUST be set by client library.
|
# > MUST be set by client library.
|
||||||
host = extract_field(:host, event) { Socket.gethostname }
|
host = extract_field(:host, event) { Socket.gethostname }
|
||||||
gelf['host'] = utf8_encode(host)
|
gelf['host'] = utf8(host)
|
||||||
|
|
||||||
# > Seconds since UNIX epoch with optional decimal places for
|
# > Seconds since UNIX epoch with optional decimal places for
|
||||||
# > milliseconds; SHOULD be set by client library. Will be set to the
|
# > milliseconds; SHOULD be set by client library. Will be set to the
|
||||||
@ -116,14 +116,14 @@ module Rackstash
|
|||||||
|
|
||||||
# > a short descriptive message; MUST be set by client library.
|
# > a short descriptive message; MUST be set by client library.
|
||||||
short_message = extract_field(:short_message, event) { EMPTY_STRING }
|
short_message = extract_field(:short_message, event) { EMPTY_STRING }
|
||||||
gelf['short_message'] = utf8_encode(short_message)
|
gelf['short_message'] = utf8(short_message)
|
||||||
|
|
||||||
# > a long message that can i.e. contain a backtrace; optional.
|
# > a long message that can i.e. contain a backtrace; optional.
|
||||||
#
|
#
|
||||||
# Since the field is optional, we only write this field if there is a
|
# Since the field is optional, we only write this field if there is a
|
||||||
# value in our event hash
|
# value in our event hash
|
||||||
full_message = extract_field(:full_message, event)
|
full_message = extract_field(:full_message, event)
|
||||||
gelf['full_message'] = utf8_encode(full_message) if full_message
|
gelf['full_message'] = utf8(full_message) if full_message
|
||||||
|
|
||||||
gelf.merge! additional_fields(event)
|
gelf.merge! additional_fields(event)
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2018 Holger Just
|
# Copyright 2018-2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Encoder
|
module Encoder
|
||||||
@ -13,7 +13,7 @@ module Rackstash
|
|||||||
# Some useful helper methods for {Rackstash::Encoder}s which help in
|
# Some useful helper methods for {Rackstash::Encoder}s which help in
|
||||||
# normalizing and handling the message list in the event Hash.
|
# normalizing and handling the message list in the event Hash.
|
||||||
module FieldsMap
|
module FieldsMap
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
@ -21,11 +21,11 @@ module Rackstash
|
|||||||
@fields_map ||= {}
|
@fields_map ||= {}
|
||||||
default.each do |key, value|
|
default.each do |key, value|
|
||||||
if fields.key?(key)
|
if fields.key?(key)
|
||||||
@fields_map[key] = utf8_encode(fields[key])
|
@fields_map[key] = utf8(fields[key])
|
||||||
else
|
else
|
||||||
# Preserve existing mappings which might have been set by a
|
# Preserve existing mappings which might have been set by a
|
||||||
# previous call to {#set_fields_mapping}
|
# previous call to {#set_fields_mapping}
|
||||||
@fields_map[key] ||= utf8_encode(value)
|
@fields_map[key] ||= utf8(value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 - 2018 Holger Just
|
# Copyright 2017 - 2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
@ -10,7 +10,7 @@ require 'time'
|
|||||||
|
|
||||||
require 'rackstash/encoder'
|
require 'rackstash/encoder'
|
||||||
require 'rackstash/encoder/helper/message'
|
require 'rackstash/encoder/helper/message'
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Encoder
|
module Encoder
|
||||||
@ -34,7 +34,7 @@ module Rackstash
|
|||||||
# encoder.encode(event)
|
# encoder.encode(event)
|
||||||
# # Logs "[foo,123] [127.0.0.1] Hello\n[foo,123] [127.0.0.1] World\n"
|
# # Logs "[foo,123] [127.0.0.1] Hello\n[foo,123] [127.0.0.1] World\n"
|
||||||
class Message
|
class Message
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
include Rackstash::Encoder::Helper::Message
|
include Rackstash::Encoder::Helper::Message
|
||||||
include Rackstash::Encoder::Helper::Timestamp
|
include Rackstash::Encoder::Helper::Timestamp
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ module Rackstash
|
|||||||
# @param tagged [Array<#to_s>] An array of field names whose values are
|
# @param tagged [Array<#to_s>] An array of field names whose values are
|
||||||
# added in front of each message line on {#encode}
|
# added in front of each message line on {#encode}
|
||||||
def initialize(tagged: [])
|
def initialize(tagged: [])
|
||||||
@tagged = Array(tagged).map { |tag| utf8_encode(tag) }.freeze
|
@tagged = Array(tagged).map { |tag| utf8(tag) }.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return the formatted message of the given event.
|
# Return the formatted message of the given event.
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 - 2018 Holger Just
|
# Copyright 2017 - 2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
@ -11,12 +11,12 @@ require 'uri'
|
|||||||
|
|
||||||
require 'concurrent'
|
require 'concurrent'
|
||||||
|
|
||||||
require 'rackstash/helpers'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Fields
|
module Fields
|
||||||
class AbstractCollection
|
class AbstractCollection
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# Equality - Two collections are equal if they are of exactly the same
|
# Equality - Two collections are equal if they are of exactly the same
|
||||||
# class and contain the same raw data according to `Object#==`.
|
# class and contain the same raw data according to `Object#==`.
|
||||||
@ -115,9 +115,9 @@ module Rackstash
|
|||||||
|
|
||||||
case value
|
case value
|
||||||
when ::String
|
when ::String
|
||||||
utf8_encode(value)
|
utf8(value)
|
||||||
when ::Symbol
|
when ::Symbol
|
||||||
utf8_encode(value.to_s.freeze)
|
utf8(value.to_s.freeze)
|
||||||
when ::Integer, ::Float
|
when ::Integer, ::Float
|
||||||
value
|
value
|
||||||
when true, false, nil
|
when true, false, nil
|
||||||
@ -130,7 +130,7 @@ module Rackstash
|
|||||||
when ::Hash
|
when ::Hash
|
||||||
hash = {}
|
hash = {}
|
||||||
value.each_pair do |k, v|
|
value.each_pair do |k, v|
|
||||||
hash[utf8_encode(k)] = normalize(v, scope: scope)
|
hash[utf8(k)] = normalize(v, scope: scope)
|
||||||
end
|
end
|
||||||
if wrap
|
if wrap
|
||||||
hash = Rackstash::Fields::Hash.new.tap do |hash_field|
|
hash = Rackstash::Fields::Hash.new.tap do |hash_field|
|
||||||
@ -153,11 +153,11 @@ module Rackstash
|
|||||||
when ::Date
|
when ::Date
|
||||||
value.iso8601.encode!(Encoding::UTF_8).freeze
|
value.iso8601.encode!(Encoding::UTF_8).freeze
|
||||||
when ::Regexp, ::Range, ::URI::Generic, ::Pathname
|
when ::Regexp, ::Range, ::URI::Generic, ::Pathname
|
||||||
utf8_encode(value.to_s.freeze)
|
utf8(value.to_s.freeze)
|
||||||
when Exception
|
when Exception
|
||||||
exception = "#{value.message} (#{value.class})"
|
exception = "#{value.message} (#{value.class})"
|
||||||
exception = [exception, *value.backtrace].join("\n") if value.backtrace
|
exception = [exception, *value.backtrace].join("\n") if value.backtrace
|
||||||
utf8_encode(exception.freeze)
|
utf8(exception.freeze)
|
||||||
when ::BigDecimal
|
when ::BigDecimal
|
||||||
# A BigDecimal would be naturally represented as a JSON number. Most
|
# A BigDecimal would be naturally represented as a JSON number. Most
|
||||||
# libraries, however, parse non-integer JSON numbers directly as
|
# libraries, however, parse non-integer JSON numbers directly as
|
||||||
@ -168,7 +168,7 @@ module Rackstash
|
|||||||
when ::Complex, ::Rational
|
when ::Complex, ::Rational
|
||||||
# A complex number can not reliably converted to a float or rational,
|
# A complex number can not reliably converted to a float or rational,
|
||||||
# thus we always transform it to a String
|
# thus we always transform it to a String
|
||||||
utf8_encode(value)
|
utf8(value)
|
||||||
else
|
else
|
||||||
# Try to convert the value to a known basic type and recurse
|
# Try to convert the value to a known basic type and recurse
|
||||||
converted = UNDEFINED
|
converted = UNDEFINED
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 Holger Just
|
# Copyright 2017-2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
@ -24,7 +24,7 @@ module Rackstash
|
|||||||
unless forbidden_keys.is_a?(Set) &&
|
unless forbidden_keys.is_a?(Set) &&
|
||||||
forbidden_keys.frozen? &&
|
forbidden_keys.frozen? &&
|
||||||
forbidden_keys.all? { |key| String === key && key.frozen? }
|
forbidden_keys.all? { |key| String === key && key.frozen? }
|
||||||
forbidden_keys = Set.new(forbidden_keys) { |key| utf8_encode key }
|
forbidden_keys = Set.new(forbidden_keys) { |key| utf8 key }
|
||||||
forbidden_keys.freeze
|
forbidden_keys.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ module Rackstash
|
|||||||
# @return [Object, nil] the current value of the field or `nil` if the
|
# @return [Object, nil] the current value of the field or `nil` if the
|
||||||
# field wasn't set (yet)
|
# field wasn't set (yet)
|
||||||
def [](key)
|
def [](key)
|
||||||
@raw[utf8_encode(key)]
|
@raw[utf8(key)]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Set the value of a key to the supplied value
|
# Set the value of a key to the supplied value
|
||||||
@ -55,7 +55,7 @@ module Rackstash
|
|||||||
# @raise [ArgumentError] if you attempt to set one of the forbidden keys.
|
# @raise [ArgumentError] if you attempt to set one of the forbidden keys.
|
||||||
# @return [value]
|
# @return [value]
|
||||||
def []=(key, value)
|
def []=(key, value)
|
||||||
key = utf8_encode(key)
|
key = utf8(key)
|
||||||
raise ArgumentError, "Forbidden field #{key}" if forbidden_key?(key)
|
raise ArgumentError, "Forbidden field #{key}" if forbidden_key?(key)
|
||||||
|
|
||||||
@raw[key] = normalize(value)
|
@raw[key] = normalize(value)
|
||||||
@ -305,7 +305,7 @@ module Rackstash
|
|||||||
# was not found, we return the `default` value or the value of the given
|
# was not found, we return the `default` value or the value of the given
|
||||||
# block.
|
# block.
|
||||||
def fetch(key, default = UNDEFINED, &block)
|
def fetch(key, default = UNDEFINED, &block)
|
||||||
key = utf8_encode(key)
|
key = utf8(key)
|
||||||
if UNDEFINED.equal? default
|
if UNDEFINED.equal? default
|
||||||
@raw.fetch(key, &block)
|
@raw.fetch(key, &block)
|
||||||
else
|
else
|
||||||
@ -332,7 +332,7 @@ module Rackstash
|
|||||||
# UTF-8 string before being checked.
|
# UTF-8 string before being checked.
|
||||||
# @return [Boolean] `true` if the normalized key is present in `self`
|
# @return [Boolean] `true` if the normalized key is present in `self`
|
||||||
def key?(key)
|
def key?(key)
|
||||||
@raw.key? utf8_encode(key)
|
@raw.key? utf8(key)
|
||||||
end
|
end
|
||||||
alias has_key? key?
|
alias has_key? key?
|
||||||
alias include? key?
|
alias include? key?
|
||||||
@ -529,7 +529,7 @@ module Rackstash
|
|||||||
# insertion happened. Note that `nil` is also a valid value to insert
|
# insertion happened. Note that `nil` is also a valid value to insert
|
||||||
# into the hash.
|
# into the hash.
|
||||||
def set(key, force: true)
|
def set(key, force: true)
|
||||||
key = utf8_encode(key)
|
key = utf8(key)
|
||||||
|
|
||||||
if force
|
if force
|
||||||
raise ArgumentError, "Forbidden field #{key}" if forbidden_key?(key)
|
raise ArgumentError, "Forbidden field #{key}" if forbidden_key?(key)
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 Holger Just
|
# Copyright 2017-2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
@ -18,7 +18,7 @@ module Rackstash
|
|||||||
|
|
||||||
def <<(tag)
|
def <<(tag)
|
||||||
tag = resolve_value(tag)
|
tag = resolve_value(tag)
|
||||||
tag = utf8_encode(tag)
|
tag = utf8(tag)
|
||||||
@raw[tag] = true unless tag.empty?
|
@raw[tag] = true unless tag.empty?
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
@ -51,7 +51,7 @@ module Rackstash
|
|||||||
end
|
end
|
||||||
|
|
||||||
def tagged?(tag)
|
def tagged?(tag)
|
||||||
@raw.key? utf8_encode(tag)
|
@raw.key? utf8(tag)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_set
|
def to_set
|
||||||
@ -76,7 +76,7 @@ module Rackstash
|
|||||||
value.flatten!
|
value.flatten!
|
||||||
value
|
value
|
||||||
else
|
else
|
||||||
utf8_encode(value.to_s.strip.freeze)
|
utf8(value.to_s.strip.freeze)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2018 Holger Just
|
# Copyright 2018-2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
@ -8,7 +8,7 @@
|
|||||||
require 'ipaddr'
|
require 'ipaddr'
|
||||||
|
|
||||||
require 'rackstash/filter'
|
require 'rackstash/filter'
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Filter
|
module Filter
|
||||||
@ -42,7 +42,7 @@ module Rackstash
|
|||||||
# filter :anonymize_ip_mask, {'source_ip' => 'source_ip'}
|
# filter :anonymize_ip_mask, {'source_ip' => 'source_ip'}
|
||||||
# end
|
# end
|
||||||
class AnonymizeIPMask
|
class AnonymizeIPMask
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# @param field_spec [Hash<#to_s => #to_s>] a `Hash` specifying which
|
# @param field_spec [Hash<#to_s => #to_s>] a `Hash` specifying which
|
||||||
# fields should be anonymized and where the result should be stored. The
|
# fields should be anonymized and where the result should be stored. The
|
||||||
@ -57,7 +57,7 @@ module Rackstash
|
|||||||
def initialize(field_spec, ipv4_mask: 8, ipv6_mask: 80)
|
def initialize(field_spec, ipv4_mask: 8, ipv6_mask: 80)
|
||||||
@fields = {}
|
@fields = {}
|
||||||
Hash(field_spec).each_pair do |key, value|
|
Hash(field_spec).each_pair do |key, value|
|
||||||
@fields[utf8_encode(key)] = utf8_encode(value)
|
@fields[utf8(key)] = utf8(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ipv4_mask = Integer(ipv4_mask)
|
@ipv4_mask = Integer(ipv4_mask)
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2018 Holger Just
|
# Copyright 2018-2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
require 'rackstash/filter'
|
require 'rackstash/filter'
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Filter
|
module Filter
|
||||||
@ -19,7 +19,7 @@ module Rackstash
|
|||||||
# # ^^^^^^^ You can also use :delete here
|
# # ^^^^^^^ You can also use :delete here
|
||||||
# end
|
# end
|
||||||
class Remove
|
class Remove
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# @param field_matchers [Array<String,Symbol,Regexp,Proc,#===>] the fields
|
# @param field_matchers [Array<String,Symbol,Regexp,Proc,#===>] the fields
|
||||||
# to remove from the event. You can specify this in a varienty of ways,
|
# to remove from the event. You can specify this in a varienty of ways,
|
||||||
@ -33,7 +33,7 @@ module Rackstash
|
|||||||
field.is_a?(String) || field.is_a?(Symbol)
|
field.is_a?(String) || field.is_a?(Symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
@keys = Set[*keys.map! { |key| utf8_encode(key) }]
|
@keys = Set[*keys.map! { |key| utf8(key) }]
|
||||||
@matchers = matchers
|
@matchers = matchers
|
||||||
@matchers << block if block_given?
|
@matchers << block if block_given?
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 - 2018 Holger Just
|
# Copyright 2017 - 2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
require 'rackstash/filter'
|
require 'rackstash/filter'
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Filter
|
module Filter
|
||||||
@ -18,7 +18,7 @@ module Rackstash
|
|||||||
# filter :rename, "HOST_OR_IP" => "client_ip"
|
# filter :rename, "HOST_OR_IP" => "client_ip"
|
||||||
# end
|
# end
|
||||||
class Rename
|
class Rename
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# @param spec [Hash<#to_s => #to_s>] a `Hash` specifying how fields should
|
# @param spec [Hash<#to_s => #to_s>] a `Hash` specifying how fields should
|
||||||
# be renamed, with the existing field name as a hash key and the new
|
# be renamed, with the existing field name as a hash key and the new
|
||||||
@ -26,7 +26,7 @@ module Rackstash
|
|||||||
def initialize(spec)
|
def initialize(spec)
|
||||||
@rename = {}
|
@rename = {}
|
||||||
Hash(spec).each_pair do |key, value|
|
Hash(spec).each_pair do |key, value|
|
||||||
@rename[utf8_encode(key)] = utf8_encode(value)
|
@rename[utf8(key)] = utf8(value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 - 2018 Holger Just
|
# Copyright 2017 - 2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
require 'rackstash/filter'
|
require 'rackstash/filter'
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Filter
|
module Filter
|
||||||
@ -31,7 +31,7 @@ module Rackstash
|
|||||||
# here, namely `String`, `Integer`, `Float`, `Hash`, `Array`, `nil`, `true`,
|
# here, namely `String`, `Integer`, `Float`, `Hash`, `Array`, `nil`, `true`,
|
||||||
# or `false`.
|
# or `false`.
|
||||||
class Replace
|
class Replace
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# @param spec [Hash<#to_s => #call,Object>] a `Hash` specifying new field
|
# @param spec [Hash<#to_s => #call,Object>] a `Hash` specifying new field
|
||||||
# values for the named keys. Values can be given in the form of a fixed
|
# values for the named keys. Values can be given in the form of a fixed
|
||||||
@ -40,7 +40,7 @@ module Rackstash
|
|||||||
def initialize(spec)
|
def initialize(spec)
|
||||||
@replace = {}
|
@replace = {}
|
||||||
Hash(spec).each_pair do |key, value|
|
Hash(spec).each_pair do |key, value|
|
||||||
@replace[utf8_encode(key)] = value
|
@replace[utf8(key)] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2018 Holger Just
|
# Copyright 2018-2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
@ -8,7 +8,7 @@
|
|||||||
require 'set'
|
require 'set'
|
||||||
|
|
||||||
require 'rackstash/filter'
|
require 'rackstash/filter'
|
||||||
require 'rackstash/helpers/utf8'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
module Filter
|
module Filter
|
||||||
@ -33,7 +33,7 @@ module Rackstash
|
|||||||
# `"user_name"`, `"webserver"`, or `"robot_arm"` will be removed from the
|
# `"user_name"`, `"webserver"`, or `"robot_arm"` will be removed from the
|
||||||
# event however since they don't match any of the configured matchers.
|
# event however since they don't match any of the configured matchers.
|
||||||
class Select
|
class Select
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# @param field_matchers [Array<String,Symbol,Regexp,Proc,#===>] the fields
|
# @param field_matchers [Array<String,Symbol,Regexp,Proc,#===>] the fields
|
||||||
# to keep in the event. You can specify this in a varienty of ways,
|
# to keep in the event. You can specify this in a varienty of ways,
|
||||||
@ -47,7 +47,7 @@ module Rackstash
|
|||||||
field.is_a?(String) || field.is_a?(Symbol)
|
field.is_a?(String) || field.is_a?(Symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
@keys = Set[*keys.map! { |key| utf8_encode(key) }]
|
@keys = Set[*keys.map! { |key| utf8(key) }]
|
||||||
@matchers = matchers
|
@matchers = matchers
|
||||||
@matchers << block if block_given?
|
@matchers << block if block_given?
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,15 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
#
|
|
||||||
# Copyright 2017 Holger Just
|
|
||||||
#
|
|
||||||
# This software may be modified and distributed under the terms
|
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
|
||||||
|
|
||||||
module Rackstash
|
|
||||||
# Some utility function which are used throughout Rackstash.
|
|
||||||
module Helpers
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require 'rackstash/helpers/utf8'
|
|
||||||
require 'rackstash/helpers/time'
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
#
|
|
||||||
# Copyright 2017 Holger Just
|
|
||||||
#
|
|
||||||
# This software may be modified and distributed under the terms
|
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
|
||||||
|
|
||||||
module Rackstash
|
|
||||||
module Helpers
|
|
||||||
module Time
|
|
||||||
protected
|
|
||||||
|
|
||||||
if defined?(Process::CLOCK_MONOTONIC)
|
|
||||||
# Get the current timestamp as a numeric value. If supported by the
|
|
||||||
# current platform, we use a monitonic clock.
|
|
||||||
#
|
|
||||||
# @return [Float] the current timestamp
|
|
||||||
def clock_time
|
|
||||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
# Get the current timestamp as a numeric value
|
|
||||||
#
|
|
||||||
# @return [Float] the current timestamp
|
|
||||||
def clock_time
|
|
||||||
::Time.now.to_f
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,31 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
#
|
|
||||||
# Copyright 2017 Holger Just
|
|
||||||
#
|
|
||||||
# This software may be modified and distributed under the terms
|
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
|
||||||
|
|
||||||
module Rackstash
|
|
||||||
module Helpers
|
|
||||||
# Provide helper functions to help with UTF8 handling of Strings.
|
|
||||||
module UTF8
|
|
||||||
protected
|
|
||||||
|
|
||||||
# Encode the given String in UTF-8. If the given `str` is already
|
|
||||||
# correctly encoded and frozen, we just return it unchanged. In all other
|
|
||||||
# cases we return a UTF-8 encoded and frozen copy of the string.
|
|
||||||
#
|
|
||||||
# @param str [String, #to_s]
|
|
||||||
# @return [String]
|
|
||||||
def utf8_encode(str)
|
|
||||||
if str.instance_of?(String) && str.encoding == Encoding::UTF_8 && str.valid_encoding?
|
|
||||||
str.frozen? ? str : str.dup.freeze
|
|
||||||
else
|
|
||||||
str = str.to_s
|
|
||||||
str = str.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
|
||||||
str.freeze
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,11 +1,11 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 Holger Just
|
# Copyright 2017 - 2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
require 'rackstash/helpers'
|
require 'rackstash/utils'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
# A Message wraps a single logged message created by the {Logger}. Here, we
|
# A Message wraps a single logged message created by the {Logger}. Here, we
|
||||||
@ -19,7 +19,7 @@ module Rackstash
|
|||||||
# All `Message` objects and their respective data are immutable after
|
# All `Message` objects and their respective data are immutable after
|
||||||
# initialization.
|
# initialization.
|
||||||
class Message
|
class Message
|
||||||
include Rackstash::Helpers::UTF8
|
include Rackstash::Utils
|
||||||
|
|
||||||
# @return [String] the logged message string. It usually is already
|
# @return [String] the logged message string. It usually is already
|
||||||
# formatted by the {Logger}'s formatter
|
# formatted by the {Logger}'s formatter
|
||||||
@ -53,7 +53,7 @@ module Rackstash
|
|||||||
@progname = dup_freeze(progname)
|
@progname = dup_freeze(progname)
|
||||||
|
|
||||||
message = message.inspect unless String === message
|
message = message.inspect unless String === message
|
||||||
@message = utf8_encode(message)
|
@message = utf8(message)
|
||||||
|
|
||||||
freeze
|
freeze
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 - 2018 Holger Just
|
# Copyright 2017 - 2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
require 'rack'
|
require 'rack'
|
||||||
|
|
||||||
require 'rackstash/helpers/time'
|
require 'rackstash/utils'
|
||||||
require 'rackstash/rack/errors'
|
require 'rackstash/rack/errors'
|
||||||
|
|
||||||
module Rackstash
|
module Rackstash
|
||||||
@ -97,7 +97,7 @@ module Rackstash
|
|||||||
# all added request fields and tags as well as the `"status"` field (set to
|
# all added request fields and tags as well as the `"status"` field (set to
|
||||||
# `500`) and the duration of the request so far.
|
# `500`) and the duration of the request so far.
|
||||||
class Middleware
|
class Middleware
|
||||||
include Rackstash::Helpers::Time
|
include Rackstash::Utils
|
||||||
|
|
||||||
# @return [Rackstash::Logger] the Rackstash logger used to log the
|
# @return [Rackstash::Logger] the Rackstash logger used to log the
|
||||||
# request details
|
# request details
|
||||||
|
|||||||
64
lib/rackstash/utils.rb
Normal file
64
lib/rackstash/utils.rb
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
#
|
||||||
|
# Copyright 2017-2020 Holger Just
|
||||||
|
#
|
||||||
|
# This software may be modified and distributed under the terms
|
||||||
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
|
module Rackstash
|
||||||
|
module Utils
|
||||||
|
module_function
|
||||||
|
|
||||||
|
# Get a UTF-8 encoded frozen string representation of the given object. If
|
||||||
|
# the object is already a correctly encoded and frozen String, we just
|
||||||
|
# return it unchanged. In all other cases we return a UTF-8 encoded and
|
||||||
|
# frozen copy of the string.
|
||||||
|
#
|
||||||
|
# @param str [String, #to_s]
|
||||||
|
# @return [String]
|
||||||
|
def utf8(obj)
|
||||||
|
if obj.instance_of?(String) && obj.encoding == Encoding::UTF_8 && obj.valid_encoding?
|
||||||
|
obj.frozen? ? obj : obj.dup.freeze
|
||||||
|
else
|
||||||
|
obj = obj.to_s
|
||||||
|
obj = obj.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
||||||
|
obj.freeze
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if defined?(Process::CLOCK_MONOTONIC)
|
||||||
|
# Get the current timestamp as a numeric value.
|
||||||
|
#
|
||||||
|
# @return [Float] the current timestamp
|
||||||
|
def clock_time
|
||||||
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
|
end
|
||||||
|
elsif Gem.win_platform?
|
||||||
|
require 'fiddle'
|
||||||
|
|
||||||
|
# GetTickCount64 is available since Windows Vista / Windows Server 2008
|
||||||
|
# It retrieves the number of milliseconds that have elapsed since the
|
||||||
|
# system was started.
|
||||||
|
# https://docs.microsoft.com/en-gb/windows/win32/api/sysinfoapi/nf-sysinfoapi-gettickcount64
|
||||||
|
GetTickCount64 = Fiddle::Function.new(
|
||||||
|
Fiddle.dlopen('kernel32.dll')['GetTickCount64'],
|
||||||
|
[],
|
||||||
|
-Fiddle::TYPE_LONG_LONG # unsigned long long
|
||||||
|
)
|
||||||
|
|
||||||
|
# Get the current timestamp as a numeric value.
|
||||||
|
#
|
||||||
|
# @return [Float] the current timestamp
|
||||||
|
def clock_time
|
||||||
|
GetTickCount64.call / 1000.0
|
||||||
|
end
|
||||||
|
else
|
||||||
|
# Get the current timestamp as a numeric value
|
||||||
|
#
|
||||||
|
# @return [Float] the current timestamp
|
||||||
|
def clock_time
|
||||||
|
::Time.now.to_f
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
|
|||||||
|
|
||||||
# We require at least Ruby 2.1 due to the use of:
|
# We require at least Ruby 2.1 due to the use of:
|
||||||
# * implicit String#scrub during String#encode as used in
|
# * implicit String#scrub during String#encode as used in
|
||||||
# Rackstash::Helpers::UTF8#utf8_encode.
|
# Rackstash::Utils#utf8.
|
||||||
# We might be able to use the string-scrub gem as a polyfill and explicitly
|
# We might be able to use the string-scrub gem as a polyfill and explicitly
|
||||||
# call scrub on lower versions though.
|
# call scrub on lower versions though.
|
||||||
# * mandatory keyword arguments
|
# * mandatory keyword arguments
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
#
|
#
|
||||||
# Copyright 2017 - 2018 Holger Just
|
# Copyright 2017 - 2020 Holger Just
|
||||||
#
|
#
|
||||||
# This software may be modified and distributed under the terms
|
# This software may be modified and distributed under the terms
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|||||||
@ -1,53 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
#
|
|
||||||
# Copyright 2017 - 2018 Holger Just
|
|
||||||
#
|
|
||||||
# This software may be modified and distributed under the terms
|
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
|
||||||
|
|
||||||
require 'spec_helper'
|
|
||||||
|
|
||||||
require 'rackstash/helpers/time'
|
|
||||||
|
|
||||||
RSpec.describe Rackstash::Helpers::Time do
|
|
||||||
it 'only defines protected methods' do
|
|
||||||
expect(described_class.public_instance_methods(false)).to be_empty
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#clock_time' do
|
|
||||||
def clock_time(*args)
|
|
||||||
Object.new.extend(described_class).send(:clock_time, *args)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns the numeric timestamp' do
|
|
||||||
expect(::Process::CLOCK_MONOTONIC).to_not be_nil
|
|
||||||
expect(::Time).not_to receive(:now)
|
|
||||||
expect(clock_time).to be_a Float
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is monotinically increasing' do
|
|
||||||
expect(clock_time).to be < clock_time
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'without a monotonic clock' do
|
|
||||||
around do |example|
|
|
||||||
clock_monotic = ::Process.send(:remove_const, :CLOCK_MONOTONIC)
|
|
||||||
verbose, $VERBOSE = $VERBOSE, false
|
|
||||||
load File.expand_path('../../../lib/rackstash/helpers/time.rb', __dir__)
|
|
||||||
$VERBOSE = verbose
|
|
||||||
|
|
||||||
example.run
|
|
||||||
|
|
||||||
::Process::CLOCK_MONOTONIC = clock_monotic
|
|
||||||
verbose, $VERBOSE = $VERBOSE, false
|
|
||||||
load File.expand_path('../../../lib/rackstash/helpers/time.rb', __dir__)
|
|
||||||
$VERBOSE = verbose
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'returns a float' do
|
|
||||||
expect(::Time).to receive(:now).and_call_original
|
|
||||||
expect(clock_time).to be_a Float
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,63 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
#
|
|
||||||
# Copyright 2017 - 2018 Holger Just
|
|
||||||
#
|
|
||||||
# This software may be modified and distributed under the terms
|
|
||||||
# of the MIT license. See the LICENSE.txt file for details.
|
|
||||||
|
|
||||||
require 'spec_helper'
|
|
||||||
|
|
||||||
require 'rackstash/helpers/utf8'
|
|
||||||
|
|
||||||
RSpec.describe Rackstash::Helpers::UTF8 do
|
|
||||||
it 'only defines protected methods' do
|
|
||||||
expect(described_class.public_instance_methods(false)).to be_empty
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#utf8_encode' do
|
|
||||||
def utf8_encode(*args)
|
|
||||||
Object.new.extend(described_class).send(:utf8_encode, *args)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'transforms encoding to UTF-8' do
|
|
||||||
utf8_str = 'Dönerstraße'
|
|
||||||
latin_str = utf8_str.encode(Encoding::ISO8859_9)
|
|
||||||
expect(latin_str.encoding).to eql Encoding::ISO8859_9
|
|
||||||
|
|
||||||
expect(utf8_encode(latin_str)).to eql utf8_str
|
|
||||||
expect(utf8_encode(latin_str).encoding).to eql Encoding::UTF_8
|
|
||||||
expect(utf8_encode(latin_str)).to be_frozen
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'replaces invalid characters in correctly encoded strings' do
|
|
||||||
binary = Digest::SHA256.digest('string')
|
|
||||||
|
|
||||||
expect(utf8_encode(binary)).to include '<27>'
|
|
||||||
expect(utf8_encode(binary).encoding).to eql Encoding::UTF_8
|
|
||||||
expect(utf8_encode(binary)).to be_frozen
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'replaces invalid characters in incorrectly encoded strings' do
|
|
||||||
strange = Digest::SHA256.digest('string').force_encoding(Encoding::UTF_8)
|
|
||||||
|
|
||||||
expect(utf8_encode(strange)).to include '<27>'
|
|
||||||
expect(utf8_encode(strange).encoding).to eql Encoding::UTF_8
|
|
||||||
expect(utf8_encode(strange)).to be_frozen
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'dups and freezes valid strings' do
|
|
||||||
valid = String.new('Dönerstraße')
|
|
||||||
expect(valid).to_not be_frozen
|
|
||||||
|
|
||||||
expect(utf8_encode(valid)).to eql(valid)
|
|
||||||
# Not object-equal since the string was dup'ed
|
|
||||||
expect(utf8_encode(valid)).not_to equal valid
|
|
||||||
expect(utf8_encode(valid)).to be_frozen
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'does not alter valid frozen strings' do
|
|
||||||
valid = 'Dönerstraße'.freeze
|
|
||||||
expect(utf8_encode(valid)).to equal(valid)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
98
spec/rackstash/utils_spec.rb
Normal file
98
spec/rackstash/utils_spec.rb
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
#
|
||||||
|
# Copyright 2017 - 2020 Holger Just
|
||||||
|
#
|
||||||
|
# This software may be modified and distributed under the terms
|
||||||
|
# of the MIT license. See the LICENSE.txt file for details.
|
||||||
|
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
require 'rackstash/utils'
|
||||||
|
require 'openssl'
|
||||||
|
|
||||||
|
RSpec.describe Rackstash::Utils do
|
||||||
|
describe '#utf8' do
|
||||||
|
it 'transforms encoding to UTF-8' do
|
||||||
|
utf8_str = 'Dönerstraße'
|
||||||
|
latin_str = utf8_str.encode(Encoding::ISO8859_9)
|
||||||
|
expect(latin_str.encoding).to eql Encoding::ISO8859_9
|
||||||
|
|
||||||
|
expect(described_class.utf8(latin_str)).to eql utf8_str
|
||||||
|
expect(described_class.utf8(latin_str).encoding).to eql Encoding::UTF_8
|
||||||
|
expect(described_class.utf8(latin_str)).to be_frozen
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'replaces invalid characters in correctly encoded strings' do
|
||||||
|
binary = OpenSSL::Digest::SHA256.digest('string')
|
||||||
|
|
||||||
|
expect(described_class.utf8(binary)).to include '<27>'
|
||||||
|
expect(described_class.utf8(binary).encoding).to eql Encoding::UTF_8
|
||||||
|
expect(described_class.utf8(binary)).to be_frozen
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'replaces invalid characters in incorrectly encoded strings' do
|
||||||
|
strange = OpenSSL::Digest::SHA256.digest('string').force_encoding(Encoding::UTF_8)
|
||||||
|
|
||||||
|
expect(described_class.utf8(strange)).to include '<27>'
|
||||||
|
expect(described_class.utf8(strange).encoding).to eql Encoding::UTF_8
|
||||||
|
expect(described_class.utf8(strange)).to be_frozen
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'dups and freezes valid strings' do
|
||||||
|
valid = String.new('Dönerstraße')
|
||||||
|
expect(valid).to_not be_frozen
|
||||||
|
|
||||||
|
expect(described_class.utf8(valid)).to eql(valid)
|
||||||
|
# Not object-equal since the string was dup'ed
|
||||||
|
expect(described_class.utf8(valid)).not_to equal valid
|
||||||
|
expect(described_class.utf8(valid)).to be_frozen
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not alter valid frozen strings' do
|
||||||
|
valid = 'Dönerstraße'.freeze
|
||||||
|
expect(described_class.utf8(valid)).to equal(valid)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#clock_time' do
|
||||||
|
it 'returns the numeric timestamp' do
|
||||||
|
expect(described_class.clock_time).to be_a Float
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'is monotonically increasing' do
|
||||||
|
expect(described_class.clock_time).to be < described_class.clock_time
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'on Windows' do
|
||||||
|
it 'fetches the GetTickCount64 function' do
|
||||||
|
expect(described_class::GetTickCount64).to be_a Fiddle::Function
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a float' do
|
||||||
|
expect(described_class::GetTickCount64).to receive(:call).and_call_original
|
||||||
|
expect(described_class.clock_time).to be_a(Float)
|
||||||
|
end
|
||||||
|
end if Gem.win_platform?
|
||||||
|
|
||||||
|
context 'without a monotonic clock' do
|
||||||
|
around do |example|
|
||||||
|
clock_monotic = ::Process.send(:remove_const, :CLOCK_MONOTONIC)
|
||||||
|
verbose, $VERBOSE = $VERBOSE, false
|
||||||
|
load File.expand_path('../../lib/rackstash/utils.rb', __dir__)
|
||||||
|
$VERBOSE = verbose
|
||||||
|
|
||||||
|
example.run
|
||||||
|
|
||||||
|
::Process::CLOCK_MONOTONIC = clock_monotic
|
||||||
|
verbose, $VERBOSE = $VERBOSE, false
|
||||||
|
load File.expand_path('../../lib/rackstash/utils.rb', __dir__)
|
||||||
|
$VERBOSE = verbose
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a float' do
|
||||||
|
expect(::Time).to receive(:now).and_call_original
|
||||||
|
expect(described_class.clock_time).to be_a Float
|
||||||
|
end
|
||||||
|
end unless Gem.win_platform?
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user