---
setup:
  - do:
      cluster.health:
        wait_for_events: languid
---
"Default data_stream.type must be logs":
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default"}, "attributes": { "foo": "bar"}, "body":{"text":"Error: Unable to connect to the database."},"severity_text":"ERROR","severity_number":3,"trace_id":"abc123xyz456def789ghi012jkl345"}'
  - is_false: errors
  - do:
      search:
        index: logs-generic.otel-default
        body:
          fields: ["data_stream.type"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.data_stream\.type: ["logs"] }
---
"Multi value fields":
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            data_stream:
              type: logs
              dataset: generic.otel
              namespace: default
            resource:
              attributes:
                host.ip: ["127.0.0.1", "0.0.0.0"]
            attributes:
              foo: [3, 2, 1]
              bar: [b, c, a]
            body:
              text: "Error: Unable to connect to the database."
            severity_text: ERROR
  - is_false: errors
  - do:
      search:
        index: logs-generic.otel-default
        body:
          fields: ["*"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.resource\.attributes\.host\.ip: ["127.0.0.1", "0.0.0.0"] }
  - match: { hits.hits.0.fields.attributes\.foo: [3, 2, 1] }
  - match: { hits.hits.0.fields.attributes\.bar: [b, c, a] }
---
"Exception aliases":
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default"}, "attributes": { "exception.type": "MyException", "exception.message": "foo", "exception.stacktrace": "Exception in thread \"main\" java.lang.RuntimeException: Test exception\n at com.example.GenerateTrace.methodB(GenerateTrace.java:13)\n at com.example.GenerateTrace.methodA(GenerateTrace.java:9)\n at com.example.GenerateTrace.main(GenerateTrace.java:5)" },"trace_id":"abc123xyz456def789ghi012jkl345"}'
  - is_false: errors
  - do:
      search:
        index: logs-generic.otel-default
        body:
          fields: ["error.exception.type", "error.exception.message", "error.stack_trace"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.error\.exception\.type: ["MyException"] }
  - match: { hits.hits.0.fields.error\.exception\.message: ["foo"] }
  - match: { hits.hits.0.fields.error\.stack_trace: ["Exception in thread \"main\" java.lang.RuntimeException: Test exception\n at com.example.GenerateTrace.methodB(GenerateTrace.java:13)\n at com.example.GenerateTrace.methodA(GenerateTrace.java:9)\n at com.example.GenerateTrace.main(GenerateTrace.java:5)"] }
---
"Long stacktrace":
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: { }
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            resource:
              attributes:
                service.name: my-service
            attributes:
              exception.stacktrace: |-
                Exception in thread "main" java.lang.RuntimeException: Simulated deep stacktrace for testing
                  at com.example.project.moduleA.ClassA.methodA(ClassA.java:10)
                  at com.example.project.moduleA.ClassA.methodB(ClassA.java:15)
                  at com.example.project.moduleB.ClassB.methodC(ClassB.java:22)
                  at com.example.project.moduleB.ClassB.methodD(ClassB.java:27)
                  at com.example.project.moduleC.ClassC.methodE(ClassC.java:34)
                  at com.example.project.moduleC.ClassC.methodF(ClassC.java:39)
                  at com.example.project.moduleD.ClassD.methodG(ClassD.java:46)
                  at com.example.project.moduleD.ClassD.methodH(ClassD.java:51)
                  at com.example.project.moduleE.ClassE.methodI(ClassE.java:58)
                  at com.example.project.moduleE.ClassE.methodJ(ClassE.java:63)
                  at com.example.project.moduleF.ClassF.methodK(ClassF.java:70)
                  at com.example.project.moduleF.ClassF.methodL(ClassF.java:75)
                  at com.example.project.moduleG.ClassG.methodM(ClassG.java:82)
                  at com.example.project.moduleG.ClassG.methodN(ClassG.java:87)
                  at com.example.project.moduleH.ClassH.methodO(ClassH.java:94)
                  at com.example.project.moduleH.ClassH.methodP(ClassH.java:99)
                  at com.example.project.moduleI.ClassI.methodQ(ClassI.java:106)
                  at com.example.project.moduleI.ClassI.methodR(ClassI.java:111)
                  at com.example.project.moduleJ.ClassJ.methodS(ClassJ.java:118)
                  at com.example.project.moduleJ.ClassJ.methodT(ClassJ.java:123)
                  at com.example.project.moduleK.ClassK.methodU(ClassK.java:130)
                  at com.example.project.moduleK.ClassK.methodV(ClassK.java:135)
                  at com.example.project.moduleL.ClassL.methodW(ClassL.java:142)
                  at com.example.project.moduleL.ClassL.methodX(ClassL.java:147)
                  at com.example.project.moduleM.ClassM.methodY(ClassM.java:154)
                  at com.example.project.moduleM.ClassM.methodZ(ClassM.java:159)
                  at com.example.project.moduleN.ClassN.methodAA(ClassN.java:166)
                  at com.example.project.moduleN.ClassN.methodAB(ClassN.java:171)
                  at com.example.project.moduleO.ClassO.methodAC(ClassO.java:178)
                  at com.example.project.moduleO.ClassO.methodAD(ClassO.java:183)
                  at com.example.project.moduleP.ClassP.methodAE(ClassP.java:190)
                  at com.example.project.moduleP.ClassP.methodAF(ClassP.java:195)
                  at com.example.project.moduleQ.ClassQ.methodAG(ClassQ.java:202)
                  at com.example.project.moduleQ.ClassQ.methodAH(ClassQ.java:207)
                  at com.example.project.moduleR.ClassR.methodAI(ClassR.java:214)
                  at com.example.project.moduleR.ClassR.methodAJ(ClassR.java:219)
                  at com.example.project.moduleS.ClassS.methodAK(ClassS.java:226)
                  at com.example.project.moduleS.ClassS.methodAL(ClassS.java:231)
                  at com.example.project.moduleT.ClassT.methodAM(ClassT.java:238)
                  at com.example.project.moduleT.ClassT.methodAN(ClassT.java:243)
                  at com.example.project.moduleU.ClassU.methodAO(ClassU.java:250)
                  at com.example.project.moduleU.ClassU.methodAP(ClassU.java:255)
                  at com.example.project.moduleV.ClassV.methodAQ(ClassV.java:262)
                  at com.example.project.moduleV.ClassV.methodAR(ClassV.java:267)
                  at com.example.project.moduleW.ClassW.methodAS(ClassW.java:274)
                  at com.example.project.moduleW.ClassW.methodAT(ClassW.java:279)
                  at com.example.project.moduleX.ClassX.methodAU(ClassX.java:286)
                  at com.example.project.moduleX.ClassX.methodAV(ClassX.java:291)
                  at com.example.project.moduleY.ClassY.methodAW(ClassY.java:298)
                  at com.example.project.moduleY.ClassY.methodAX(ClassY.java:303)
                  at com.example.project.moduleZ.ClassZ.methodAY(ClassZ.java:310)
                  at com.example.project.moduleZ.ClassZ.methodAZ(ClassZ.java:315)
                  at com.example.project.moduleAA.ClassAA.methodBA(ClassAA.java:322)
                  at com.example.project.moduleAA.ClassAA.methodBB(ClassAA.java:327)
                  at com.example.project.moduleAB.ClassAB.methodBC(ClassAB.java:334)
                  at com.example.project.moduleAB.ClassAB.methodBD(ClassAB.java:339)
                  at com.example.project.moduleAC.ClassAC.methodBE(ClassAC.java:346)
                  at com.example.project.moduleAC.ClassAC.methodBF(ClassAC.java:351)
                  at com.example.project.moduleAD.ClassAD.methodBG(ClassAD.java:358)
                  at com.example.project.moduleAD.ClassAD.methodBH(ClassAD.java:363)
                  at com.example.project.moduleAE.ClassAE.methodBI(ClassAE.java:370)
                  at com.example.project.moduleAE.ClassAE.methodBJ(ClassAE.java:375)
                  at com.example.project.moduleAF.ClassAF.methodBK(ClassAF.java:382)
                  at com.example.project.moduleAF.ClassAF.methodBL(ClassAF.java:387)
                  at com.example.project.moduleAG.ClassAG.methodBM(ClassAG.java:394)
                  at com.example.project.moduleAG.ClassAG.methodBN(ClassAG.java:399)
                  at com.example.project.moduleAH.ClassAH.methodBO(ClassAH.java:406)
                  at com.example.project.moduleAH.ClassAH.methodBP(ClassAH.java:411)
                  at com.example.project.moduleAI.ClassAI.methodBQ(ClassAI.java:418)
                  at com.example.project.moduleAI.ClassAI.methodBR(ClassAI.java:423)
                  at com.example.project.moduleAJ.ClassAJ.methodBS(ClassAJ.java:430)
                  at com.example.project.moduleAJ.ClassAJ.methodBT(ClassAJ.java:435)
                  at com.example.project.moduleAK.ClassAK.methodBU(ClassAK.java:442)
                  at com.example.project.moduleAK.ClassAK.methodBV(ClassAK.java:447)
                  at com.example.project.moduleAL.ClassAL.methodBW(ClassAL.java:454)
                  at com.example.project.moduleAL.ClassAL.methodBX(ClassAL.java:459)
                  at com.example.project.moduleAM.ClassAM.methodBY(ClassAM.java:466)
                  at com.example.project.moduleAM.ClassAM.methodBZ(ClassAM.java:471)
                  at com.example.project.Main.main(Main.java:478)
                Caused by: java.lang.IllegalArgumentException: Invalid argument encountered
                  at com.example.project.util.Validator.check(Validator.java:55)
                  at com.example.project.moduleA.ClassA.methodA(ClassA.java:8)
                  at com.example.project.moduleA.ClassA.methodB(ClassA.java:15)
                  at com.example.project.moduleB.ClassB.methodC(ClassB.java:22)
                  at com.example.project.moduleB.ClassB.methodD(ClassB.java:27)
                  at com.example.project.moduleC.ClassC.methodE(ClassC.java:34)
                  at com.example.project.moduleC.ClassC.methodF(ClassC.java:39)
                  at com.example.project.moduleD.ClassD.methodG(ClassD.java:46)
                  at com.example.project.moduleD.ClassD.methodH(ClassD.java:51)
                  at com.example.project.moduleE.ClassE.methodI(ClassE.java:58)
                  at com.example.project.moduleE.ClassE.methodJ(ClassE.java:63)
                  at com.example.project.moduleF.ClassF.methodK(ClassF.java:70)
                  at com.example.project.moduleF.ClassF.methodL(ClassF.java:75)
                  at com.example.project.moduleG.ClassG.methodM(ClassG.java:82)
                  at com.example.project.moduleG.ClassG.methodN(ClassG.java:87)
                  at com.example.project.moduleH.ClassH.methodO(ClassH.java:94)
                  at com.example.project.moduleH.ClassH.methodP(ClassH.java:99)
                  at com.example.project.moduleI.ClassI.methodQ(ClassI.java:106)
                  at com.example.project.moduleI.ClassI.methodR(ClassI.java:111)
                  at com.example.project.moduleJ.ClassJ.methodS(ClassJ.java:118)
                  at com.example.project.moduleJ.ClassJ.methodT(ClassJ.java:123)
                  at com.example.project.moduleK.ClassK.methodU(ClassK.java:130)
                  at com.example.project.moduleK.ClassK.methodV(ClassK.java:135)
                  at com.example.project.moduleL.ClassL.methodW(ClassL.java:142)
                  at com.example.project.moduleL.ClassL.methodX(ClassL.java:147)
                  at com.example.project.moduleM.ClassM.methodY(ClassM.java:154)
                  at com.example.project.moduleM.ClassM.methodZ(ClassM.java:159)
                  at com.example.project.moduleN.ClassN.methodAA(ClassN.java:166)
                  at com.example.project.moduleN.ClassN.methodAB(ClassN.java:171)
                  at com.example.project.moduleO.ClassO.methodAC(ClassO.java:178)
                  at com.example.project.moduleO.ClassO.methodAD(ClassO.java:183)
                  at com.example.project.moduleP.ClassP.methodAE(ClassP.java:190)
                  at com.example.project.moduleP.ClassP.methodAF(ClassP.java:195)
                  at com.example.project.moduleQ.ClassQ.methodAG(ClassQ.java:202)
                  at com.example.project.moduleQ.ClassQ.methodAH(ClassQ.java:207)
                  at com.example.project.moduleR.ClassR.methodAI(ClassR.java:214)
                  at com.example.project.moduleR.ClassR.methodAJ(ClassR.java:219)
                  at com.example.project.moduleS.ClassS.methodAK(ClassS.java:226)
                  at com.example.project.moduleS.ClassS.methodAL(ClassS.java:231)
                  at com.example.project.moduleT.ClassT.methodAM(ClassT.java:238)
                  at com.example.project.moduleT.ClassT.methodAN(ClassT.java:243)
                  at com.example.project.moduleU.ClassU.methodAO(ClassU.java:250)
                  at com.example.project.moduleU.ClassU.methodAP(ClassU.java:255)
                  at com.example.project.moduleV.ClassV.methodAQ(ClassV.java:262)
                  at com.example.project.moduleV.ClassV.methodAR(ClassV.java:267)
                  at com.example.project.moduleW.ClassW.methodAS(ClassW.java:274)
                  at com.example.project.moduleW.ClassW.methodAT(ClassW.java:279)
                  at com.example.project.moduleX.ClassX.methodAU(ClassX.java:286)
                  at com.example.project.moduleX.ClassX.methodAV(ClassX.java:291)
                  at com.example.project.moduleY.ClassY.methodAW(ClassY.java:298)
                  at com.example.project.moduleY.ClassY.methodAX(ClassY.java:303)
                  at com.example.project.moduleZ.ClassZ.methodAY(ClassZ.java:310)
                  at com.example.project.moduleZ.ClassZ.methodAZ(ClassZ.java:315)
                  at com.example.project.moduleAA.ClassAA.methodBA(ClassAA.java:322)
                  at com.example.project.moduleAA.ClassAA.methodBB(ClassAA.java:327)
                  at com.example.project.moduleAB.ClassAB.methodBC(ClassAB.java:334)
                  at com.example.project.moduleAB.ClassAB.methodBD(ClassAB.java:339)
                  at com.example.project.moduleAC.ClassAC.methodBE(ClassAC.java:346)
                  at com.example.project.moduleAC.ClassAC.methodBF(ClassAC.java:351)
                  at com.example.project.moduleAD.ClassAD.methodBG(ClassAD.java:358)
                  at com.example.project.moduleAD.ClassAD.methodBH(ClassAD.java:363)
                  at com.example.project.moduleAE.ClassAE.methodBI(ClassAE.java:370)
                  at com.example.project.moduleAE.ClassAE.methodBJ(ClassAE.java:375)
                  at com.example.project.moduleAF.ClassAF.methodBK(ClassAF.java:382)
                  at com.example.project.moduleAF.ClassAF.methodBL(ClassAF.java:387)
                  at com.example.project.moduleAG.ClassAG.methodBM(ClassAG.java:394)
                  at com.example.project.moduleAG.ClassAG.methodBN(ClassAG.java:399)
                  at com.example.project.moduleAH.ClassAH.methodBO(ClassAH.java:406)
                  at com.example.project.moduleAH.ClassAH.methodBP(ClassAH.java:411)
                  at com.example.project.moduleAI.ClassAI.methodBQ(ClassAI.java:418)
                  at com.example.project.moduleAI.ClassAI.methodBR(ClassAI.java:423)
                  at com.example.project.moduleAJ.ClassAJ.methodBS(ClassAJ.java:430)
                  at com.example.project.moduleAJ.ClassAJ.methodBT(ClassAJ.java:435)
                  at com.example.project.moduleAK.ClassAK.methodBU(ClassAK.java:442)
                  at com.example.project.moduleAK.ClassAK.methodBV(ClassAK.java:447)
                  at com.example.project.moduleAL.ClassAL.methodBW(ClassAL.java:454)
                  at com.example.project.moduleAL.ClassAL.methodBX(ClassAL.java:459)
                  at com.example.project.moduleAM.ClassAM.methodBY(ClassAM.java:466)
                  at com.example.project.moduleAM.ClassAM.methodBZ(ClassAM.java:471)
                  ... 58 more
                Caused by: java.lang.NullPointerException: Null value found
                  at com.example.project.util.Validator.requireNonNull(Validator.java:60)
                  at com.example.project.util.Validator.check(Validator.java:53)
                  ... 77 more

  - is_false: errors
  - do:
      search:
        index: logs-generic.otel-default
  - length: { hits.hits: 1 }
  - exists: hits.hits.0._source.attributes.exception\.stacktrace
  - exists: hits.hits.0._ignored
---
"resource.attributes.host.name @timestamp should be used as sort fields":
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:49:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default"}, "body": {"text":"error1"}}'
  - is_false: errors
  - do:
      indices.get_data_stream:
        name: logs-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: datastream-backing-index }
  - do:
      indices.get_settings:
        index: $datastream-backing-index
  - is_true: $datastream-backing-index
  - match: { .$datastream-backing-index.settings.index.sort.field.0: "resource.attributes.host.name" }
  - match: { .$datastream-backing-index.settings.index.sort.field.1: "@timestamp" }
---
Event body:
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            resource:
              attributes:
                service.name: my-service
            attributes:
              event.name: foo
            event_name: foo
            body:
              structured:
                foo:
                  bar: baz
  - is_false: errors
  - do:
      indices.get_data_stream:
        name: logs-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: datastream-backing-index }
  - do:
      indices.get_mapping:
        index: $datastream-backing-index
  - is_true: $datastream-backing-index
  - match: { .$datastream-backing-index.mappings.properties.body.properties.structured.type: "flattened" }
  - match: { .$datastream-backing-index.mappings.properties.event_name.type: "keyword" }
---
Structured log body:
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            resource:
              attributes:
                service.name: my-service
            body:
              structured:
                foo:
                  bar: baz
  - is_false: errors
  - do:
      indices.get_data_stream:
        name: logs-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: datastream-backing-index }
  - do:
      indices.get_mapping:
        index: $datastream-backing-index
  - is_true: $datastream-backing-index
  - match: { .$datastream-backing-index.mappings.properties.body.properties.structured.type: "flattened" }
---
"event.dataset alias must point to data_stream.dataset":
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:49:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default"}, "body_text":"error1"}'
  - is_false: errors
  - is_false: errors
  - do:
      search:
        index: logs-generic.otel-default
        body:
          fields: ["event.dataset"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.event\.dataset: ["generic.otel"] }
---
host.name pass-through:
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: {}
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            resource:
              attributes:
                host.name: localhost
  - is_false: errors
  - do:
      search:
        index: logs-generic.otel-default
        body:
          query:
            term:
              host.name: localhost
          fields: [ "*" ]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.resource\.attributes\.host\.name: [ "localhost" ] }
  - match: { hits.hits.0.fields.host\.name: [ "localhost" ] }
---
"kubernetes.* -> resource.attributes.k8s.* aliases":
  - do:
      bulk:
        index: logs-generic.otel-default
        refresh: true
        body:
          - create: { }
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            data_stream:
              dataset: generic.otel
              namespace: default
            resource:
              attributes:
                k8s.container.name: myContainerName
                k8s.cronjob.name: myCronJobName
                k8s.job.name: myJobName
                k8s.statefulset.name: myStatefulsetName
                k8s.daemonset.name: myDaemonsetName
                k8s.replicaset.name: myReplicasetName
                k8s.node.uid: myNodeUid
                k8s.node.hostname: myNodeHostname
                k8s.cluster.name: myClusterName
  - is_false: errors
  - do:
      search:
        index: logs-generic.otel-default
        body:
          fields: ["kubernetes.container.name", "kubernetes.cronjob.name", "kubernetes.job.name", "kubernetes.statefulset.name", "kubernetes.daemonset.name", "kubernetes.replicaset.name", "kubernetes.node.uid", "kubernetes.node.hostname", "orchestrator.cluster.name" ]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.kubernetes\.container\.name : ["myContainerName"] }
  - match: { hits.hits.0.fields.kubernetes\.cronjob\.name : ["myCronJobName"] }
  - match: { hits.hits.0.fields.kubernetes\.job\.name : ["myJobName"] }
  - match: { hits.hits.0.fields.kubernetes\.statefulset\.name : ["myStatefulsetName"] }
  - match: { hits.hits.0.fields.kubernetes\.daemonset\.name : ["myDaemonsetName"] }
  - match: { hits.hits.0.fields.kubernetes\.replicaset\.name : ["myReplicasetName"] }
  - match: { hits.hits.0.fields.kubernetes\.node\.uid : ["myNodeUid"] }
  - match: { hits.hits.0.fields.kubernetes\.node\.hostname : ["myNodeHostname"] }
  - match: { hits.hits.0.fields.orchestrator\.cluster\.name : ["myClusterName"] }
