Skip to content

Commit

Permalink
Merge pull request #48 from logicmonitor/key-changed-for-device-less-…
Browse files Browse the repository at this point in the history
…logs

Key changed for deviceless logs to comply with otel
  • Loading branch information
ahsan13jan authored May 16, 2022
2 parents 843af87 + 9937db8 commit 6582c87
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fluent-plugin-lm-logs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Gem::Specification.new do |spec|
spec.name = "fluent-plugin-lm-logs"
spec.version = '1.0.3'
spec.version = '1.0.4'
spec.authors = ["LogicMonitor"]
spec.email = "rubygems@logicmonitor.com"
spec.summary = "LogicMonitor logs fluentd output plugin"
Expand Down
21 changes: 15 additions & 6 deletions lib/fluent/plugin/out_lm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ module Fluent
class LmOutput < BufferedOutput
Fluent::Plugin.register_output('lm', self)

RESOURCE_MAPPING_KEY = "_lm.resourceId".freeze
DEVICELESS_KEY_SERVICE = "resource.service.name".freeze
DEVICELESS_KEY_NAMESPACE = "resource.service.namespace".freeze

# config_param defines a parameter. You can refer a parameter via @path instance variable

config_param :access_id, :string, :default => "access_id"
Expand Down Expand Up @@ -91,7 +95,8 @@ def process_record(tag, time, record)
end

if !@device_less_logs
if record["_lm.resourceId"] == nil
# With devices
if record[RESOURCE_MAPPING_KEY] == nil
@resource_mapping.each do |key, value|
k = value
nestedVal = record
Expand All @@ -100,17 +105,21 @@ def process_record(tag, time, record)
resource_map[k] = nestedVal
end
end
lm_event["_lm.resourceId"] = resource_map
lm_event[RESOURCE_MAPPING_KEY] = resource_map
else
lm_event["_lm.resourceId"] = record["_lm.resourceId"]
lm_event[RESOURCE_MAPPING_KEY] = record[RESOURCE_MAPPING_KEY]
end
else
if record["service"]==nil
# Device less
if record[DEVICELESS_KEY_SERVICE]==nil
log.error "When device_less_logs is set \'true\', record must have \'service\'. Ignoring this event #{lm_event}."
return nil
else
lm_event["service"] = encode_if_necessary(record["service"])
end
lm_event[DEVICELESS_KEY_SERVICE] = encode_if_necessary(record[DEVICELESS_KEY_SERVICE])
if record[DEVICELESS_KEY_NAMESPACE]!=nil
lm_event[DEVICELESS_KEY_NAMESPACE] = encode_if_necessary(record[DEVICELESS_KEY_NAMESPACE])
end
end
end

if record["timestamp"] != nil
Expand Down
42 changes: 37 additions & 5 deletions test/plugin/test_device_less_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ def create_valid_subject
"tag" => "lm.test",
"meta1" => "metadata1",
"meta2" => "metadata2",
"service" => "lm-service",
"namespace" => "lm-namepsace",
"resource.service.name" => "lm-service",
"resource.service.namespace" => "lm-namepsace",
"someProp" => "someVal"}

result = plugin.process_record(tag, time, record)

expected = {
"message" => "Hello from test",
"timestamp" => "2020-10-30T00:29:08.629701504Z",
"service" => "lm-service"
"resource.service.name" => "lm-service",
"resource.service.namespace" => "lm-namepsace"
}
assert_equal expected, result
end
Expand All @@ -62,7 +63,7 @@ def create_valid_subject
"meta3" => "testMeta3",
"meta4" => "testMeta4",
"meta5" => {"key1" => "value1", "key2" => {"key2_1" => "value2_1"}},
"service" => "lm-service" }
"resource.service.name" => "lm-service" }

result = plugin.process_record(tag, time, record)

Expand All @@ -74,7 +75,7 @@ def create_valid_subject
"meta3" => "testMeta3",
"meta4" => "testMeta4",
"meta5" => {"key1" => "value1", "key2" => {"key2_1" => "value2_1"}},
"service" => "lm-service"
"resource.service.name" => "lm-service"
}
assert_equal expected, result
end
Expand All @@ -95,5 +96,36 @@ def create_valid_subject
expected = nil
assert_equal expected, result
end


test "when device_less_logs is true and include_metadata is false record must have \'service\' and pick up namespace " do
plugin = create_driver(%[
resource_mapping {"a.b": "lm_property"}
device_less_logs true
include_metadata false
]).instance
tag = "lm.test"
time = Time.parse("2020-08-23T00:53:15+00:00").to_i
record = {
"message" => "Hello from test",
"timestamp" => "2020-10-30T00:29:08.629701504Z" ,
"meta1" => "testMeta1" ,
"meta2" => "testMeta2",
"meta3" => "testMeta3",
"meta4" => "testMeta4",
"meta5" => {"key1" => "value1", "key2" => {"key2_1" => "value2_1"}},
"resource.service.name" => "lm-service",
"resource.service.namespace" => "lm-namespace" }

result = plugin.process_record(tag, time, record)

expected = {
"message" => "Hello from test",
"timestamp" => "2020-10-30T00:29:08.629701504Z",
"resource.service.name" => "lm-service",
"resource.service.namespace" => "lm-namespace"
}
assert_equal expected, result
end
end
end

0 comments on commit 6582c87

Please sign in to comment.