Class: OpenCensus::Tags::TagMap

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/opencensus/tags/tag_map.rb

Overview

TagMap

Collection of tag key and value.

Examples:


tag_map = OpenCensus::Tags::OpenCensus.new

# Add or update
tag_map["key1"] = "value1"
tag_map["key2"] = "value2"

# Get value
tag_map["key1"] # value1

# Delete
tag_map.delete "key1"

# Iterate
tag_map.each do |key, value|
  p key
  p value
end

# Length
tag_map.length # 1

Create tag map from hash


tag_map = OpenCensus::Tags::OpenCensus.new({ "key1" => "value1"})

Defined Under Namespace

Classes: InvalidTagError

Constant Summary collapse

MAX_LENGTH =

The maximum length for a tag key and tag value

255

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tags = {}) ⇒ TagMap

Create a tag map. It is a map of tags from key to value.

Parameters:

  • tags (Hash{String=>String}) (defaults to: {})

    Tags hash with string key and value.


47
48
49
50
51
52
53
# File 'lib/opencensus/tags/tag_map.rb', line 47

def initialize tags = {}
  @tags = {}

  tags.each do |key, value|
    self[key] = value
  end
end

Class Method Details

.from_binary(data) ⇒ TagMap

Create a tag map from the binary string.

Parameters:

  • data (String)

    Binary string data

Returns:


80
81
82
# File 'lib/opencensus/tags/tag_map.rb', line 80

def self.from_binary data
  Formatters::Binary.new.deserialize data
end

Instance Method Details

#[]Object

See Also:

  • Hash#[]

98
99
# File 'lib/opencensus/tags/tag_map.rb', line 98

def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
:to_h, :empty?

#[]=(key, value) ⇒ Object

Set tag key value

Parameters:

  • key (String)

    Tag key

  • value (String)

    Tag value

Raises:


61
62
63
64
65
66
# File 'lib/opencensus/tags/tag_map.rb', line 61

def []= key, value
  validate_key! key
  validate_value! value

  @tags[key] = value
end

#deleteObject

See Also:

  • Hash#delete

98
99
# File 'lib/opencensus/tags/tag_map.rb', line 98

def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
:to_h, :empty?

#delete_ifObject

See Also:

  • Hash#delete_if

98
99
# File 'lib/opencensus/tags/tag_map.rb', line 98

def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
:to_h, :empty?

#eachObject

See Also:

  • Hash#each

98
99
# File 'lib/opencensus/tags/tag_map.rb', line 98

def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
:to_h, :empty?

#empty?Object

See Also:

  • Hash#empty?

98
99
# File 'lib/opencensus/tags/tag_map.rb', line 98

def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
:to_h, :empty?

#lengthObject

See Also:

  • Hash#length

98
99
# File 'lib/opencensus/tags/tag_map.rb', line 98

def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
:to_h, :empty?

#to_binaryString

Convert tag map to binary string format.


72
73
74
# File 'lib/opencensus/tags/tag_map.rb', line 72

def to_binary
  Formatters::Binary.new.serialize self
end

#to_hObject

See Also:

  • Hash#to_h

98
99
# File 'lib/opencensus/tags/tag_map.rb', line 98

def_delegators :@tags, :[], :each, :delete, :delete_if, :length, \
:to_h, :empty?