Logstash--Ruby exception occurred: no implicit conversion of nil into String

Hello,

Below is my json log string:

{
	"traceId": "o0uyveRU/8BkjL+lbpDlnQ==",
	"spanId": "73rmqIRmo34=",
	"operationName": "ProcessWorkItem",
	"startTime": "2024-07-22T06:07:12.650881409Z",
	"duration": "0.256620153s",
	"tags": [
		{
			"key": "otel.library.name",
			"vStr": "com.mdi.core.workmgmt.TypedConsumer"
		},
		{
			"key": "ta.work.type",
			"vStr": "INBOUND_ENTITY_INTEGRATION"
		},
		{
			"key": "ta.companyCode",
			"vStr": "AMBER_ROAD"
		},
		{
			"key": "ta.work.status",
			"vStr": "COMPLETED"
		},
		{
			"key": "ta.work.loadDuration",
			"vType": "INT64"
		},
		{
			"key": "service.version",
			"vStr": "24.2"
		},
		{
			"key": "work.processorclass",
			"vStr": "com.mdi.core.entity.integration.EntityStdIntegrationWorkProcessor"
		},
		{
			"key": "ta.work.receivedTime",
			"vType": "INT64",
			"vInt64": "1721628429423"
		},
		{
			"key": "ta.work.delegateDuration",
			"vType": "INT64",
			"vInt64": "257"
		},
		{
			"key": "ta.work.busKeySearch",
			"vStr": "AMBER_ROAD;SO_TEST_93;SALES_ORDER"
		},
		{
			"key": "ta.orgCode",
			"vStr": "AMBER_ROAD"
		},
		{
			"key": "ta.work.id",
			"vStr": "33267221"
		},
		{
			"key": "length",
			"vType": "INT64",
			"vInt64": "3680"
		},
		{
			"key": "ta.work.reportStatusDuration",
			"vType": "INT64",
			"vInt64": "4"
		},
		{
			"key": "ta.work.errorCount",
			"vType": "INT64"
		},
		{
			"key": "ta.work.delegateConsumerTime",
			"vType": "INT64",
			"vInt64": "253"
		},
		{
			"key": "span.kind",
			"vStr": "internal"
		},
		{
			"key": "otel.status_code",
			"vStr": "OK"
		},
		{
			"key": "internal.span.format",
			"vStr": "otlp"
		}
	],
	"process": {
		"serviceName": "TRADE_SINGLETON",
		"tags": [
			{
				"key": "deployment.environment",
				"vStr": "PSR"
			},
			{
				"key": "e2.environment.name",
				"vStr": "OTEL_PST"
			},
			{
				"key": "e2.instance.name",
				"vStr": "TRADE_SINGLETON_1"
			},
			{
				"key": "e2.product.family",
				"vStr": "GTM"
			},
			{
				"key": "e2.product.tenant.id",
				"vStr": "WMMERCURYTA"
			},
			{
				"key": "service.instance.id",
				"vStr": "URN:PSR:GTM:TA:OTEL_PST:TRADE_SINGLETON:TRADE_SINGLETON_1"
			},
			{
				"key": "service.namespace",
				"vStr": "TA"
			},
			{
				"key": "telemetry.sdk.language",
				"vStr": "java"
			},
			{
				"key": "telemetry.sdk.name",
				"vStr": "opentelemetry"
			},
			{
				"key": "telemetry.sdk.version",
				"vStr": "1.34.0"
			}
		]
	},
	"tag": "jaeger_spans"
}

I am using filters as below:

filter 
{ 
	json 
	{
		source => "message"
	}
		
	json 
	{
		source => "message"
		target => "tmessage"
	}

	mutate
	{
		add_field => {"process.serviceName" => "%{[tmessage][process][serviceName]}"}
	}
	
	ruby {
		code => '
			event.get("[tmessage][tags]").each { |a|
				name = a["key"]
				value = a["vStr"]
				name=name.gsub!(/\./,"@")
				if a["vStr"]
					value = a["vStr"]
				elsif a["vInt64"] 
					value = a["vInt64"]
				else
					value = ""
				end
				event.set("tag." + name, value)
				}
			'
		}
				
	ruby {
		code => '
			event.get("[tmessage][process][tags]").each { |a|
				name = a["key"]
				value = a["vStr"]
				name=name.gsub!(/\./,"@")
				if a["vStr"]
					value = a["vStr"]
				elsif a["vInt64"]
					value = a["vInt64"]
				else
					value = ""
				end
				event.set("process.tag." + name, value)
			}
		'
		}
		
	mutate {
		remove_field => [ "tmessage"]
	}
}

Able to get all the fields as expected shown below, But if you see there is a ruby exception error showing and "tags" field is showing _rubyexception. Pls let me know where would be the issue?

[2024-07-26T12:41:42,723][ERROR][logstash.filters.ruby    ][main][a0fdcdbb1a176b5c56feaa37ac17769719c5890f692c3165948e12f8300e5d15] Ruby 
exception occurred: no implicit conversion of nil into String {:class=>"TypeError", :backtrace=>["org/jruby/RubyString.java:1232:in `+'", 
"(ruby filter code):14:in `block in register'", "org/jruby/RubyArray.java:1989:in `each'", "(ruby filter code):3:in `block in register'", 
"/usr/share/logstash/vendor/bundle/jruby/3.1.0/gems/logstash-filter-ruby-3.1.8/lib/logstash/filters/ruby.rb:96:in `inline_script'", 
"/usr/share/logstash/vendor/bundle/jruby/3.1.0/gems/logstash-filter-ruby-3.1.8/lib/logstash/filters/ruby.rb:89:in `filter'", 
"/usr/share/logstash/logstash-core/lib/logstash/filters/base.rb:158:in `do_filter'", 
"/usr/share/logstash/logstash-core/lib/logstash/filters/base.rb:176:in `block in multi_filter'", "org/jruby/RubyArray.java:1989:in `each'", 
"/usr/share/logstash/logstash-core/lib/logstash/filters/base.rb:173:in `multi_filter'", 
"org/logstash/config/ir/compiler/AbstractFilterDelegatorExt.java:133:in `multi_filter'", 
"/usr/share/logstash/logstash-core/lib/logstash/java_pipeline.rb:304:in `block in start_workers'"]}
{
                      "tag.ta@work@type" => "INBOUND_ENTITY_INTEGRATION",
                              "duration" => "0.256620153s",
         "process.tag.service@namespace" => "TA",
                            "@timestamp" => 2024-07-26T12:41:42.437517265Z,
                                "spanId" => "73rmqIRmo34=",
                    "tag.ta@work@status" => "COMPLETED",
               "tag.work@processorclass" => "com.mdi.core.entity.integration.EntityStdIntegrationWorkProcessor",
          "tag.ta@work@delegateDuration" => "257",
                        "tag.ta@orgCode" => "AMBER_ROAD",
       "process.tag.e2@environment@name" => "OTEL_PST",
                 "tag.otel@library@name" => "com.mdi.core.workmgmt.TypedConsumer",
         "process.tag.e2@product@family" => "GTM",
        "process.tag.telemetry@sdk@name" => "opentelemetry",
              "tag.ta@work@loadDuration" => "",
                               "traceId" => "o0uyveRU/8BkjL+lbpDlnQ==",
          "process.tag.e2@instance@name" => "TRADE_SINGLETON_1",
                             "startTime" => "2024-07-22T06:07:12.650881409Z",
                         "operationName" => "ProcessWorkItem",
                                 "event" => {
        "original" => "{\"traceId\":\"o0uyveRU/8BkjL+lbpDlnQ==\",\"spanId\":\"73rmqIRmo34=\",\"operationName\":\"ProcessWorkItem\",\"startTime\":\"2024-07-22T06:07:12.650881409Z\",\"duration\":\"0.256620153s\",\"tags\":[{\"key\":\"otel.library.name\",\"vStr\":\"com.mdi.core.workmgmt.TypedConsumer\"},{\"key\":\"ta.work.type\",\"vStr\":\"INBOUND_ENTITY_INTEGRATION\"},{\"key\":\"ta.companyCode\",\"vStr\":\"AMBER_ROAD\"},{\"key\":\"ta.work.status\",\"vStr\":\"COMPLETED\"},{\"key\":\"ta.work.loadDuration\",\"vType\":\"INT64\"},{\"key\":\"service.version\",\"vStr\":\"24.2\"},{\"key\":\"work.processorclass\",\"vStr\":\"com.mdi.core.entity.integration.EntityStdIntegrationWorkProcessor\"},{\"key\":\"ta.work.receivedTime\",\"vType\":\"INT64\",\"vInt64\":\"1721628429423\"},{\"key\":\"ta.work.delegateDuration\",\"vType\":\"INT64\",\"vInt64\":\"257\"},{\"key\":\"ta.work.busKeySearch\",\"vStr\":\"AMBER_ROAD;SO_TEST_93;SALES_ORDER\"},{\"key\":\"ta.orgCode\",\"vStr\":\"AMBER_ROAD\"},{\"key\":\"ta.work.id\",\"vStr\":\"33267221\"},{\"key\":\"length\",\"vType\":\"INT64\",\"vInt64\":\"3680\"},{\"key\":\"ta.work.reportStatusDuration\",\"vType\":\"INT64\",\"vInt64\":\"4\"},{\"key\":\"ta.work.errorCount\",\"vType\":\"INT64\"},{\"key\":\"ta.work.delegateConsumerTime\",\"vType\":\"INT64\",\"vInt64\":\"253\"},{\"key\":\"span.kind\",\"vStr\":\"internal\"},{\"key\":\"otel.status_code\",\"vStr\":\"OK\"},{\"key\":\"internal.span.format\",\"vStr\":\"otlp\"}],\"process\":{\"serviceName\":\"TRADE_SINGLETON\",\"tags\":[{\"key\":\"deployment.environment\",\"vStr\":\"PSR\"},{\"key\":\"e2.environment.name\",\"vStr\":\"OTEL_PST\"},{\"key\":\"e2.instance.name\",\"vStr\":\"TRADE_SINGLETON_1\"},{\"key\":\"e2.product.family\",\"vStr\":\"GTM\"},{\"key\":\"e2.product.tenant.id\",\"vStr\":\"WMMERCURYTA\"},{\"key\":\"service.instance.id\",\"vStr\":\"URN:PSR:GTM:TA:OTEL_PST:TRADE_SINGLETON:TRADE_SINGLETON_1\"},{\"key\":\"service.namespace\",\"vStr\":\"TA\"},{\"key\":\"telemetry.sdk.language\",\"vStr\":\"java\"},{\"key\":\"telemetry.sdk.name\",\"vStr\":\"opentelemetry\"},{\"key\":\"telemetry.sdk.version\",\"vStr\":\"1.34.0\"}]},\"tag\":\"jaeger_spans\"}",
        "sequence" => 0
    },
                                  "tags" => [
        [0] "_jsonparsefailure",
        [1] "_rubyexception"
    ],
       "process.tag.service@instance@id" => "URN:PSR:GTM:TA:OTEL_PST:TRADE_SINGLETON:TRADE_SINGLETON_1",
                   "process.serviceName" => "TRADE_SINGLETON",
                               "message" => "{\"traceId\":\"o0uyveRU/8BkjL+lbpDlnQ==\",\"spanId\":\"73rmqIRmo34=\",\"operationName\":\"ProcessWorkItem\",\"startTime\":\"2024-07-22T06:07:12.650881409Z\",\"duration\":\"0.256620153s\",\"tags\":[{\"key\":\"otel.library.name\",\"vStr\":\"com.mdi.core.workmgmt.TypedConsumer\"},{\"key\":\"ta.work.type\",\"vStr\":\"INBOUND_ENTITY_INTEGRATION\"},{\"key\":\"ta.companyCode\",\"vStr\":\"AMBER_ROAD\"},{\"key\":\"ta.work.status\",\"vStr\":\"COMPLETED\"},{\"key\":\"ta.work.loadDuration\",\"vType\":\"INT64\"},{\"key\":\"service.version\",\"vStr\":\"24.2\"},{\"key\":\"work.processorclass\",\"vStr\":\"com.mdi.core.entity.integration.EntityStdIntegrationWorkProcessor\"},{\"key\":\"ta.work.receivedTime\",\"vType\":\"INT64\",\"vInt64\":\"1721628429423\"},{\"key\":\"ta.work.delegateDuration\",\"vType\":\"INT64\",\"vInt64\":\"257\"},{\"key\":\"ta.work.busKeySearch\",\"vStr\":\"AMBER_ROAD;SO_TEST_93;SALES_ORDER\"},{\"key\":\"ta.orgCode\",\"vStr\":\"AMBER_ROAD\"},{\"key\":\"ta.work.id\",\"vStr\":\"33267221\"},{\"key\":\"length\",\"vType\":\"INT64\",\"vInt64\":\"3680\"},{\"key\":\"ta.work.reportStatusDuration\",\"vType\":\"INT64\",\"vInt64\":\"4\"},{\"key\":\"ta.work.errorCount\",\"vType\":\"INT64\"},{\"key\":\"ta.work.delegateConsumerTime\",\"vType\":\"INT64\",\"vInt64\":\"253\"},{\"key\":\"span.kind\",\"vStr\":\"internal\"},{\"key\":\"otel.status_code\",\"vStr\":\"OK\"},{\"key\":\"internal.span.format\",\"vStr\":\"otlp\"}],\"process\":{\"serviceName\":\"TRADE_SINGLETON\",\"tags\":[{\"key\":\"deployment.environment\",\"vStr\":\"PSR\"},{\"key\":\"e2.environment.name\",\"vStr\":\"OTEL_PST\"},{\"key\":\"e2.instance.name\",\"vStr\":\"TRADE_SINGLETON_1\"},{\"key\":\"e2.product.family\",\"vStr\":\"GTM\"},{\"key\":\"e2.product.tenant.id\",\"vStr\":\"WMMERCURYTA\"},{\"key\":\"service.instance.id\",\"vStr\":\"URN:PSR:GTM:TA:OTEL_PST:TRADE_SINGLETON:TRADE_SINGLETON_1\"},{\"key\":\"service.namespace\",\"vStr\":\"TA\"},{\"key\":\"telemetry.sdk.language\",\"vStr\":\"java\"},{\"key\":\"telemetry.sdk.name\",\"vStr\":\"opentelemetry\"},{\"key\":\"telemetry.sdk.version\",\"vStr\":\"1.34.0\"}]},\"tag\":\"jaeger_spans\"}",
              "tag.ta@work@busKeySearch" => "AMBER_ROAD;SO_TEST_93;SALES_ORDER",
    "process.tag.telemetry@sdk@language" => "java",
                                  "host" => {
        "name" => "70efee3ab062"
    },
      "process.tag.e2@product@tenant@id" => "WMMERCURYTA",
                    "tag.ta@companyCode" => "AMBER_ROAD",
    "process.tag.deployment@environment" => "PSR",
              "tag.ta@work@receivedTime" => "1721628429423",
                   "tag.service@version" => "24.2",
                        "tag.ta@work@id" => "33267221",
     "process.tag.telemetry@sdk@version" => "1.34.0",
                              "@version" => "1"
}

The exception occurred at line 14 of the code block in one of your ruby filters. The line numbers start at 2 (don't ask) ...

ruby {
 2		code => '
 3			event.get("[tmessage][process][tags]").each { |a|
 4				name = a["key"]
 5				value = a["vStr"]
 6				name=name.gsub!(/\./,"@")
 7				if a["vStr"]
 8					value = a["vStr"]
 9				elsif a["vInt64"]
10				value = a["vInt64"]
11				else
12					value = ""
13				end
14				event.set("process.tag." + name, value)
		}

So the exception is happening at the event.set call. In that context "no implicit conversion of nil into String" is telling you that "name" is nil.

Don't use gsub! at line 6. That's what is returning nil.

1 Like

Thanks badger, I want to replace "." with @, for name field,so using gsub. Pls let me know any other I can use?

Use gsub instead of gsub! gsub! modifies the string itself, but returns nil if no substitutions are made, which is not what you want. Use gsub, which returns a copy of the string with the substitutions made (if any).

1 Like

Thanks Badger