0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CloudWatch Logs から Lambda を使って NewRelic にログを転送する際にカスタムタグをつける

Posted at

概要

  • 掲題の通り
  • 公式のコードに変更を加えているので、自己責任で利用してください
  • Lambda から NewRelic にログを転送するときに任意のタグをつけて送りたかった
    • NewRelic の Logs の画面で任意のタグでフィルタリングしたかったため
  • fluentd ではできそうだった

方法

diff --git a/src/function.py b/src/function.py
index 937c1b6..b242aba 100644
--- a/src/function.py
+++ b/src/function.py
@@ -371,6 +371,12 @@ def _reconstruct_infra_data(context, entry, logEvents):
 #  NR Logging  #
 ################
 
+def _get_custom_atrributes():
+    """
+    This functions get attribute key from env vars.
+    """
+    return os.getenv("CUSTOM_ATTRIBUTES", "{}")
+
 
 def _logging_enabled():
     """
@@ -447,6 +453,7 @@ def _package_log_payload(data):
                         "logStream": entry["logStream"],
                         "logGroup": entry["logGroup"],
                     },
+                    "custom": _get_custom_atrributes(),
                 }
             },
             "logs": log_messages,

結果

  • CUSTOM_ATTRIBUTES = "{\"group\":\"group-x\"}" とした場合
  • Logs 画面の UI にも反映されている
    • image (10).png
  • ログデータの詳細は以下
    • image (9).png
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?