Skip to content

Commit

Permalink
Remove invalid characters from tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 12, 2024
1 parent 93757cf commit 82416c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/metrics/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

module Metrics
module Tags
INVALID_TAG_CHARACTERS = /[^a-z0-9\-_\.:\\]/i

def self.normalize(tags)
return nil unless tags&.any?

if tags.is_a?(Hash)
tags = tags.map{|key, value| "#{key}:#{value}"}
tags = tags.map do |key, value|
"#{key}:#{value}".gsub(INVALID_TAG_CHARACTERS, "_").slice(0, 127)
end
end

return Array(tags)
Expand Down
14 changes: 14 additions & 0 deletions test/metrics/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require "metrics/tags"

describe Metrics::Tags do
it "can normalize an ipv6 address" do
tags = subject.normalize({"ip" => "[::1]"})

expect(tags).to be == ["ip:_::1_"]
end
end

0 comments on commit 82416c6

Please sign in to comment.