Hash#dig 未按预期进行评估
Hash#dig not evaluating as expected
我在 JSON
的 Kubernetes pod 中收到以下日志消息
{
"kubernetes": {
"annotations": {
"kubectl.kubernetes.io/default-container": "testapp",
"kubectl.kubernetes.io/default-logs-container": "testapp",
"kubernetes.io/psp": "eks.privileged",
"sidecar.istio.io/status": "{\"initContainers\":[\"istio-init\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-data\",\"istio-podinfo\",\"istio-token\",\"istiod-ca-cert\"],\"imagePullSecrets\":null,\"revision\":\"default\"}"
},
"container_hash": "678267890xxx.dkr.ecr.us-east-2.amazonaws.com/testapp-ui@sha256:cfc2c911a3530f5ccd84db2c3a4d81fc4b218804af199c60fd42c17633",
"container_image": "678267890xxx.dkr.ecr.us-east-2.amazonaws.com/testapp-ui:378",
"container_name": "testapp",
"docker_id": "9f6a89247b28392ac52daa9ba7a19416f04100f6a6ef3b5c90be4f7c",
"host": "ip-10-64-97-41.us-west-2.compute.internal",
"labels": {
"app.kubernetes.io/app-name": "testapp",
"region": "us-east-2",
"pod-template-hash": "567bd7f6d4",
"service": "testapp",
"service.istio.io/canonical-name": "testapp-istio",
"service.istio.io/canonical-revision": "latest"
},
"namespace_name": "testingQA",
"pod_id": "a4560ae2-a845-4ff9-91c6-9114a5f362bc",
"pod_name": "testapp-deployment-543bd7f6d4-mpdft"
},
"message": "127.0.0.6 - - [09/Mar/2022:19:24:32 +0000] \"GET / HTTP/1.1\" 200 41559 \"-\" \"kube-probe/1.20+\" \"-\"\n",
"source": "stdout",
"time": "2021-11-04T19:24:32.460076299Z"
}
我正在尝试根据以下条件添加一个新字段 app_name
。我在 fluentd
中使用 record_modifier
过滤器来执行此操作
<filter**>
@type record_modifier
remove_keys _dummy_
<record>
_dummy_ ${record.key?("app") ? record["app_name"] = record["app"] : record.key?("kubernetes") && record.dig(:kubernetes, :labels, :service_name).nil? ? record["app_name"] = record["kubernetes"]["labels"]["service_name"] : record.key?("kubernetes") && record.dig(:kubernetes, :labels, :"app.kubernetes.io/app-name").nil? ? record["app_name"] = record.dig(:kubernetes, :labels, :"app.kubernetes.io/app-name") : record["app_name"] = "empty"}
</record>
</filter>
根据日志消息,字段 record["kubernetes"]["labels"]["app.kubernetes.io/app-name"]
的值为 "testapp"
。然而它 returns null
。不知道为什么会这样。
还观察到只有第一个 dig 命令评估正确。任何见解将不胜感激。
正如@muistooshort 所指出的,使用键作为符号而不是字符串是一个问题。以下作品
<filter**>
@type record_modifier
remove_keys _dummy_
<record>
_dummy_ ${record.key?("app") ? record["app_name"] = record["app"] : record.key?("kubernetes") && record.dig("kubernetes", "labels", "service_name").nil? ? record["app_name"] = record["kubernetes"]["labels"]["service_name"] : record.key?("kubernetes") && record.dig("kubernetes", "labels", "app.kubernetes.io/app-name").nil? ? record["app_name"] = record.dig("kubernetes", "labels", "app.kubernetes.io/app-name") : record["app_name"] = "empty"}
</record>
</filter>
我在 JSON
的 Kubernetes pod 中收到以下日志消息{
"kubernetes": {
"annotations": {
"kubectl.kubernetes.io/default-container": "testapp",
"kubectl.kubernetes.io/default-logs-container": "testapp",
"kubernetes.io/psp": "eks.privileged",
"sidecar.istio.io/status": "{\"initContainers\":[\"istio-init\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-data\",\"istio-podinfo\",\"istio-token\",\"istiod-ca-cert\"],\"imagePullSecrets\":null,\"revision\":\"default\"}"
},
"container_hash": "678267890xxx.dkr.ecr.us-east-2.amazonaws.com/testapp-ui@sha256:cfc2c911a3530f5ccd84db2c3a4d81fc4b218804af199c60fd42c17633",
"container_image": "678267890xxx.dkr.ecr.us-east-2.amazonaws.com/testapp-ui:378",
"container_name": "testapp",
"docker_id": "9f6a89247b28392ac52daa9ba7a19416f04100f6a6ef3b5c90be4f7c",
"host": "ip-10-64-97-41.us-west-2.compute.internal",
"labels": {
"app.kubernetes.io/app-name": "testapp",
"region": "us-east-2",
"pod-template-hash": "567bd7f6d4",
"service": "testapp",
"service.istio.io/canonical-name": "testapp-istio",
"service.istio.io/canonical-revision": "latest"
},
"namespace_name": "testingQA",
"pod_id": "a4560ae2-a845-4ff9-91c6-9114a5f362bc",
"pod_name": "testapp-deployment-543bd7f6d4-mpdft"
},
"message": "127.0.0.6 - - [09/Mar/2022:19:24:32 +0000] \"GET / HTTP/1.1\" 200 41559 \"-\" \"kube-probe/1.20+\" \"-\"\n",
"source": "stdout",
"time": "2021-11-04T19:24:32.460076299Z"
}
我正在尝试根据以下条件添加一个新字段 app_name
。我在 fluentd
中使用 record_modifier
过滤器来执行此操作
<filter**>
@type record_modifier
remove_keys _dummy_
<record>
_dummy_ ${record.key?("app") ? record["app_name"] = record["app"] : record.key?("kubernetes") && record.dig(:kubernetes, :labels, :service_name).nil? ? record["app_name"] = record["kubernetes"]["labels"]["service_name"] : record.key?("kubernetes") && record.dig(:kubernetes, :labels, :"app.kubernetes.io/app-name").nil? ? record["app_name"] = record.dig(:kubernetes, :labels, :"app.kubernetes.io/app-name") : record["app_name"] = "empty"}
</record>
</filter>
根据日志消息,字段 record["kubernetes"]["labels"]["app.kubernetes.io/app-name"]
的值为 "testapp"
。然而它 returns null
。不知道为什么会这样。
还观察到只有第一个 dig 命令评估正确。任何见解将不胜感激。
正如@muistooshort 所指出的,使用键作为符号而不是字符串是一个问题。以下作品
<filter**>
@type record_modifier
remove_keys _dummy_
<record>
_dummy_ ${record.key?("app") ? record["app_name"] = record["app"] : record.key?("kubernetes") && record.dig("kubernetes", "labels", "service_name").nil? ? record["app_name"] = record["kubernetes"]["labels"]["service_name"] : record.key?("kubernetes") && record.dig("kubernetes", "labels", "app.kubernetes.io/app-name").nil? ? record["app_name"] = record.dig("kubernetes", "labels", "app.kubernetes.io/app-name") : record["app_name"] = "empty"}
</record>
</filter>