すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:ログの特定のテキスト形式への変換

最終更新日:Aug 28, 2024

このトピックでの実践事例は、日常業務中にチケットで提出された実際のデータ変換要件に基づいています。 このトピックでは、LOG ドメイン固有言語 (DSL) オーケストレーションを使用して、要件を満たすようにログを変換する方法について説明します。

シナリオ: 非標準の JSON オブジェクトを JSON データに変換し、オブジェクトを展開する

収集された辞書データに対して 2 次ネストを実行し、データを展開すると仮定します。 辞書データを JSON データに変換してから、e_json 関数でデータを展開します。

  • 生ログ

    content: {
      'referer': '-',
      'request': 'GET /phpMyAdmin',
      'status': 404,
      'data-1': {
        'aaa': 'Mozilla',
        'bbb': 'asde'
      },
      'data-2': {
        'up_adde': '-',
        'up_host': '-'
      }
    }
  • LOG DSL オーケストレーション

    1. content フィールドのデータを JSON 形式に変換します。

      e_set("content_json",str_replace(ct_str(v("content")),"'",'"'))

      処理後のログは以下のとおりです。

      content: {
        'referer': '-',
        'request': 'GET /phpMyAdmin',
        'status': 404,
        'data-1': {
          'aaa': 'Mozilla',
          'bbb': 'asde'
        },
        'data-2': {
          'up_adde': '-',
          'up_host': '-'
        }
      }
      content_json:  {
        "referer": "-",
        "request": "GET /phpMyAdmin",
        "status": 404,
        "data-1": {
          "aaa": "Mozilla",
          "bbb": "asde"
        },
        "data-2": {
          "up_adde": "-",
          "up_host": "-"
        }
      }
    2. 事前処理の後に生成された標準化された content_json データを展開します。 たとえば、JSON データで depth1 に設定すると、最初のレイヤーにあるデータが展開されます。

      e_json("content_json",depth=1,fmt='full')

      展開後のログは次のとおりです。

      content_json.data-1.data-1:  {"aaa": "Mozilla", "bbb": "asde"}
      content_json.data-2.data-2:  {"up_adde": "-", "up_host": "-"}
      content_json.referer:  -
      content_json.request:  GET /phpMyAdmin
      content_json.status:  404

      depth2 に設定した場合、展開後のログは次のとおりです。

      content_json.data-1.aaa:  Mozilla
      content_json.data-1.bbb:  asde
      content_json.data-2.up_adde:  -
      content_json.data-2.up_host:  -
      content_json.referer:  -
      content_json.request:  GET /phpMyAdmin
      content_json.status:  404
    3. まとめると、次のように LOG DSL ルールを使用します。

      e_set("content_json",str_replace(ct_str(v("content")),"'",'"'))
      e_json("content_json",depth=2,fmt='full')
  • 変換後のログ

    depth2 の設定することで、ログ変換後に次のログが生成されます。

    content:  {
      'referer': '-',
      'request': 'GET /phpMyAdmin',
      'status': 404,
      'data-1': {
        'aaa': 'Mozilla',
        'bbb': 'asde'
      },
      'data-2': {
        'up_adde': '-',
        'up_host': '-'
      }
    }
    content_json:  {
      "referer": "-",
      "request": "GET /phpMyAdmin",
      "status": 404,
      "data-1": {
        "aaa": "Mozilla",
        "bbb": "asde"
      },
      "data-2": {
        "up_adde": "-",
        "up_host": "-"
      }
    }
    content_json.data-1.aaa:  Mozilla
    content_json.data-1.bbb:  asde
    content_json.data-2.up_adde:  -
    content_json.data-2.up_host:  -
    content_json.referer:  -
    content_json.request:  GET /phpMyAdmin
    content_json.status:  404

他テキスト形式のログの JSON への変換とデータの展開

非標準の JSON データを展開するために、ルールを柔軟に組み合わせることができます。

  • 生ログ

    content : {
      "pod" => {
        "name" => "crm-learning-follow-7bc48f8b6b-m6kgb"
      }, "node" => {
        "name" => "tw5"
      }, "labels" => {
        "pod-template-hash" => "7bc48f8b6b", "app" => "crm-learning-follow"
      }, "container" => {
        "name" => "crm-learning-follow"
      }, "namespace" => "testing1"
    }
  • LOG DSL オーケストレーション

    1. str_logtash_config_normalize 関数を使用してログを JSON 形式に変換します。

      e_set("normalize_data",str_logtash_config_normalize(v("content")))
    2. JSON 関数を使用してデータを展開します。

      e_json("normalize_data",depth=1,fmt='full')
    3. まとめると、次のように LOG DSL ルールを使用します。

      e_set("normalize_data",str_logtash_config_normalize(v("content")))
      e_json("normalize_data",depth=1,fmt='full')
  • 変換後のログ

    content : {
      "pod" => {
        "name" => "crm-learning-follow-7bc48f8b6b-m6kgb"
      }, "node" => {
        "name" => "tw5"
      }, "labels" => {
        "pod-template-hash" => "7bc48f8b6b", "app" => "crm-learning-follow"
      }, "container" => {
        "name" => "crm-learning-follow"
      }, "namespace" => "testing1"
    }
    normalize_data:  {
      "pod": {
        "name": "crm-learning-follow-7bc48f8b6b-m6kgb"
      },
      "node": {
        "name": "tw5"
      },
      "labels": {
        "pod-template-hash": "7bc48f8b6b",
        "app": "crm-learning-follow"
      },
      "container": {
        "name": "crm-learning-follow"
      },
      "namespace": "testing1"
    }
    normalize_data.container.container:  {"name": "crm-learning-follow"}
    normalize_data.labels.labels:  {"pod-template-hash": "7bc48f8b6b", "app": "crm-learning-follow"}
    normalize_data.namespace:  testing1
    normalize_data.node.node:  {"name": "tw5"}
    normalize_data.pod.pod:  {"name": "crm-learning-follow-7bc48f8b6b-m6kgb"}

特別なエンコード形式で書かれたテキストの変換

日常業務で記録される 16 進数文字は、読み取る前にデコードする必要があります。 str_hex_escape_encode 関数を使用して、16 進数文字に対してエスケープ操作を実行します。

  • 生ログ

    content : "\xe4\xbd\xa0\xe5\xa5\xbd"
  • LOG DSL オーケストレーション

    e_set("hex_encode",str_hex_escape_encode(v("content")))
  • 変換後のログ

    content : "\xe4\xbd\xa0\xe5\xa5\xbd"
    hex_encode : "Hello"

XML フィールドの展開

XML データなど、日常業務中にさまざまなタイプのデータに遭遇する場合があります。 XML データを JSON に変換するには、xml_to_json 関数を使用します。

  • テストログ

    str : <? xmlversion="1.0"? >
    <data>
        <countryname="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighborname="Austria"direction="E"/>
            <neighborname="Switzerland"direction="W"/>
        </country>
        <countryname="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighborname="Malaysia"direction="N"/>
        </country>
        <countryname="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighborname="Costa Rica"direction="W"/>
            <neighborname="Colombia"direction="E"/>
        </country>
    </data>
  • LOG DSL オーケストレーション

    e_set("str_json",xml_to_json(v("str")))
  • 変換後のログ

    str : <? xmlversion="1.0"? >
    <data>
        <countryname="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighborname="Austria"direction="E"/>
            <neighborname="Switzerland"direction="W"/>
        </country>
        <countryname="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighborname="Malaysia"direction="N"/>
        </country>
        <countryname="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighborname="Costa Rica"direction="W"/>
            <neighborname="Colombia"direction="E"/>
        </country>
    </data>
    str_dict :{
      "data": {
        "country": [{
          "@name": "Liechtenstein",
          "rank": "1",
          "year": "2008",
          "gdppc": "141100",
          "neighbor": [{
            "@name": "Austria",
            "@direction": "E"
          }, {
            "@name": "Switzerland",
            "@direction": "W"
          }]
        }, {
          "@name": "Singapore",
          "rank": "4",
          "year": "2011",
          "gdppc": "59900",
          "neighbor": {
            "@name": "Malaysia",
            "@direction": "N"
          }
        }, {
          "@name": "Panama",
          "rank": "68",
          "year": "2011",
          "gdppc": "13600",
          "neighbor": [{
            "@name": "Costa Rica",
            "@direction": "W"
          }, {
            "@name": "Colombia",
            "@direction": "E"
          }]
        }]
      }
    }