---
setup:
  - do:
      cluster.health:
        wait_for_events: languid
  - do:
      cluster.put_component_template:
        name: metrics-otel@custom
        body:
          template:
            settings:
              index:
                time_series:
                  start_time: 2024-07-01T13:03:08.138Z
---
"Test push service overview metric":
  - do:
      indices.get_index_template:
        name: metrics-otel@template
  - length: {index_templates: 1}
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default","type":"metrics"},"resource":{"attributes":{"service.name":"OtelSample","telemetry.sdk.language":"dotnet","telemetry.sdk.name":"opentelemetry"},"dropped_attributes_count":0},"attributes":{"processor.event":"metric"}}'
  - is_false: errors
  - do:
      search:
        index: metrics-generic.otel-default
        body:
          fields: ["service.name", "telemetry.sdk.language", "telemetry.sdk.name" ]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.service\.name: [ "OtelSample" ] }
  - match: { hits.hits.0.fields.telemetry\.sdk\.language: [ "dotnet" ] }
  - match: { hits.hits.0.fields.telemetry\.sdk\.name: [ "opentelemetry" ] }
---
"Query resource attributes as top level":
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default","type":"metrics"},"processor":{"event":"metric"},"resource":{"attributes":{"service.name":"OtelSample","telemetry.sdk.language":"dotnet","telemetry.sdk.name":"opentelemetry"},"dropped_attributes_count":0}}'
  - is_false: errors
  - do:
      search:
        index: metrics-generic.otel-default
        body:
          fields: ["service.name", "telemetry.sdk.language", "telemetry.sdk.name"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.service\.name: [ "OtelSample" ] }
  - match: { hits.hits.0.fields.telemetry\.sdk\.language: [ "dotnet" ] }
  - match: { hits.hits.0.fields.telemetry\.sdk\.name: [ "opentelemetry" ] }
---
"Query attributes as top level":
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default","type":"metrics"},"attributes":{"processor.event":"metric", "foo": "bar"},"resource":{"attributes":{"service.name":"OtelSample","telemetry.sdk.language":"dotnet","telemetry.sdk.name":"opentelemetry"},"dropped_attributes_count":0}}'
  - is_false: errors
  - do:
      search:
        index: metrics-generic.otel-default
        body:
          fields: ["foo"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.foo\: ["bar"] }
---
"Boolean as dimension":
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default","type":"metrics"},"attributes":{"processor.event":"metric","transaction.root":false},"resource":{"attributes":{"service.name":"OtelSample","telemetry.sdk.language":"dotnet","telemetry.sdk.name":"opentelemetry"}}}'
  - is_false: errors
  - do:
      search:
        index: metrics-generic.otel-default
        body:
          fields: ["transaction.root"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.transaction\.root: [false] }
---
"Default data_stream.type must be metrics":
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default"},"attributes":{"processor.event":"metric","transaction.root":false},"resource":{"attributes":{"service.name":"OtelSample","telemetry.sdk.language":"dotnet","telemetry.sdk.name":"opentelemetry"}}}'
  - is_false: errors
  - do:
      search:
        index: metrics-generic.otel-default
        body:
          fields: ["data_stream.type"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0.fields.data_stream\.type: ["metrics"] }

---
"Custom dynamic template":
  - do:
      cluster.put_component_template:
        name: metrics-otel@custom
        body:
          template:
            settings:
              index:
                routing_path: [unit, attributes.*, resource.attributes.*]
                mode: time_series
                time_series:
                  start_time: 2024-07-01T13:03:08.138Z
            mappings:
              dynamic_templates:
                - no_ip_fields:
                    mapping:
                      type: keyword
                    match_mapping_type: string
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","data_stream":{"dataset":"generic.otel","namespace":"default"},"attributes":{"host.ip":"127.0.0.1","foo":"bar"}}'
  - is_false: errors
  - do:
      indices.get_data_stream:
        name: metrics-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get_mapping:
        index: $idx0name
        expand_wildcards: hidden
  - match: { .$idx0name.mappings.properties.attributes.properties.host\.ip.type: 'keyword' }
  - match: { .$idx0name.mappings.properties.attributes.properties.foo.type: "keyword" }
---
IP dimensions:
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {"dynamic_templates":{"metrics.foo.bar":"counter_long"}}
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            resource:
              attributes:
                host.ip: [ "127.0.0.1", "0.0.0.0" ]
            attributes:
              philip: [ a, b, c ]
            metrics:
              foo.bar: 42
  - is_false: errors

  - do:
      indices.get_data_stream:
        name: metrics-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get_mapping:
        index: $idx0name
        expand_wildcards: hidden
  - match: { .$idx0name.mappings.properties.resource.properties.attributes.properties.host\.ip.type: 'ip' }
  - match: { .$idx0name.mappings.properties.attributes.properties.philip.type: "keyword" }
---
"Long data type in attributes must be accepted":
  - do:
      indices.get_index_template:
        name: metrics-otel@template
  - length: {index_templates: 1}
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {}
          - '{"@timestamp":"2024-07-18T14:48:33.467654000Z","attributes":{"processor.pid": 17}}'
  - is_false: errors
  - do:
      search:
        index: metrics-generic.otel-default
  - length: { hits.hits: 1 }
---
"Dynamic templates":
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {"dynamic_templates":{"metrics.counter_long":"counter_long","metrics.gauge_long":"gauge_long","metrics.counter_double":"counter_double","metrics.gauge_double":"gauge_double","metrics.summary":"summary","metrics.summary_minmax":"summary_minmax","metrics.histogram":"histogram","metrics.exponential_histogram":"exponential_histogram"}}
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            attributes:
              foo: bar
            metrics:
              counter_long: 42
              gauge_long: 42
              counter_double: 42.42
              gauge_double: 42.42
              summary:
                sum: 42.42
                value_count: 42
              summary_minmax:
                sum: 42.42
                value_count: 42
                min: 0.42
                max: 4.2
              histogram:
                counts: [1,2]
                values: [42,4242]
              exponential_histogram:
                scale: 11
                zero:
                  count: 1
                  threshold: 42
                positive:
                  indices: [0,1]
                  counts: [2,3]
                negative:
                  indices: [4,5]
                  counts: [6,7]
                sum: 12.3
                min: 0.1
                max: 99.9

  - is_false: errors
  - do:
      search:
        index: metrics-generic.otel-default
  - length: { hits.hits: 1 }

  - do:
      indices.get_data_stream:
        name: metrics-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get_mapping:
        index: $idx0name
        expand_wildcards: hidden
  - match: { .$idx0name.mappings.properties.metrics.properties.counter_long.type: 'long' }
  - match: { .$idx0name.mappings.properties.metrics.properties.gauge_long.type: 'long' }
  - match: { .$idx0name.mappings.properties.metrics.properties.counter_double.type: 'double' }
  - match: { .$idx0name.mappings.properties.metrics.properties.gauge_double.type: 'double' }
  - match: { .$idx0name.mappings.properties.metrics.properties.summary.type: 'aggregate_metric_double' }
  - match: { .$idx0name.mappings.properties.metrics.properties.summary_minmax.type: 'aggregate_metric_double' }
  - match: { .$idx0name.mappings.properties.metrics.properties.histogram.type: 'histogram' }
  - match: { .$idx0name.mappings.properties.metrics.properties.histogram.time_series_metric: 'histogram' }
  - match: { .$idx0name.mappings.properties.metrics.properties.exponential_histogram.type: 'exponential_histogram' }
  - match: { .$idx0name.mappings.properties.metrics.properties.exponential_histogram.time_series_metric: 'histogram' }
---
Empty IP field:
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {"dynamic_templates":{"metrics.foo.bar":"counter_long"}}
          - "@timestamp": 2024-07-18T14:48:33.467654000Z
            resource:
              attributes:
                host.name: localhost
                host.ip: ""
            metrics:
              foo.bar: 42
  - is_false: errors

  - do:
      indices.get_data_stream:
        name: metrics-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get_mapping:
        index: $idx0name
        expand_wildcards: hidden
  - match: { .$idx0name.mappings.properties.resource.properties.attributes.properties.host\.ip.type: 'ip' }
  - do:
      search:
        index: metrics-generic.otel-default
        body:
          fields: ["*"]
  - length: { hits.hits: 1 }
  - match: { hits.hits.0._ignored: ["resource.attributes.host.ip"] }
---
Metrics with different scope names are not duplicates:
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {"dynamic_templates":{"metrics.foo.bar":"counter_long"}}
          - "@timestamp": 2024-07-18T14:00:00Z
            scope:
              name: foo
            resource:
              attributes:
                service.name: foo
            metrics:
              foo.bar: 42
          - create: {"dynamic_templates":{"metrics.foo.bar":"counter_long"}}
          - "@timestamp": 2024-07-18T14:00:00Z
            scope:
              name: bar
            resource:
              attributes:
                service.name: foo
            metrics:
              foo.bar: 42
  - is_false: errors

---
Metrics with different metric name hashes are not duplicates:
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {"dynamic_templates":{"metrics.foo":"counter_long"}}
          - "@timestamp": 2024-07-18T14:00:00Z
            attributes:
              foo: bar
            metrics:
              foo: 42
            _metric_names_hash: a9f37ed7
          - create: {"dynamic_templates":{"metrics.bar":"counter_long"}}
          - "@timestamp": 2024-07-18T14:00:00Z
            attributes:
              foo: bar
            metrics:
              bar: 42
            _metric_names_hash: 76b77d1a
  - is_false: errors
---
host.name pass-through:
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {"dynamic_templates":{"metrics.foo.bar":"counter_long"}}
          - "@timestamp": 2024-07-18T14:00:00Z
            scope:
              name: foo
            resource:
              attributes:
                host.name: localhost
            metrics:
              foo.bar: 42
  - is_false: errors
  - do:
      search:
        index: metrics-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" ] }

---
data stream settings ilm:
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - create: {"dynamic_templates":{"metrics.foo.bar":"counter_long"}}
          - "@timestamp": 2024-07-18T14:00:00Z
            resource:
              attributes:
                host.name: localhost
            metrics:
              foo.bar: 42
  - is_false: errors
  - do:
      indices.put_data_stream_settings:
        name: metrics-generic.otel-default
        body:
          index:
            number_of_shards: 2
  - match: { data_streams.0.name: metrics-generic.otel-default }
  - match: { data_streams.0.applied_to_data_stream: true }

---
unit metadata:
  - do:
      bulk:
        index: metrics-generic.otel-default
        refresh: true
        body:
          - {"create": {"dynamic_templates":{"metrics.foo.bar":"gauge_double"},"dynamic_template_params":{"metrics.foo.bar":{"unit": "{percent}"}}}}
          - "@timestamp": 2024-07-18T14:00:00Z
            scope:
              name: foo
            resource:
              attributes:
                host.name: localhost
            metrics:
              foo.bar: 42
  - is_false: errors
  - do:
      indices.get_data_stream:
        name: metrics-generic.otel-default
  - set: { data_streams.0.indices.0.index_name: idx0name }

  - do:
      indices.get_mapping:
        index: $idx0name
        expand_wildcards: hidden
  - match: { .$idx0name.mappings.properties.metrics.properties.foo\.bar.type: 'double' }
  - match: { .$idx0name.mappings.properties.metrics.properties.foo\.bar.meta.unit: '{percent}' }
