Skip to content

OTTL Index function implementation. #40393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mashhurs
Copy link
Contributor

@mashhurs mashhurs commented May 30, 2025

Description

Index(source, value) returns the 1st occurence of value in the source. The source can be Slice or String type.

Link to tracking issue

Fixes #40351

Testing

  • Unit testing
  • E2E testing
  • Local test
// config
receivers:
  otlp:
    protocols:
      http:
        endpoint: 127.0.0.1:8080

processors:
  transform:
    log_statements:
      - context: log
        statements:
          - set(attributes["indexof"], Index(attributes["array.attribute"], "values"))

exporters:
  debug:
    verbosity: detailed

service:
  telemetry:
    logs:
      level: debug
  pipelines:
    logs:
      receivers: [otlp]
      processors: [transform]
      exporters: [debug]

// payload
curl --location '127.0.0.1:8080/v1/logs' \
--header 'Content-Type: application/json' \
--data '{
 "resourceLogs": [
   {
     "resource": {
       "attributes": [
         {
           "key": "service.name",
           "value": {
             "stringValue": "my.service"
           }
         }
       ]
     },
     "scopeLogs": [
       {
         "scope": {
           "name": "my.library",
           "version": "1.0.0",
           "attributes": [
             {
               "key": "my.scope.attribute",
               "value": {
                 "stringValue": "some scope attribute"
               }
             }
           ]
         },
         "logRecords": [
           {
             "timeUnixNano": "1544712660300000000",
             "observedTimeUnixNano": "1544712660300000000",
             "severityNumber": 10,
             "severityText": "Information",
             "traceId": "5B8EFFF798038103D269B633813FC60C",
             "spanId": "EEE19B7EC3C1B174",
             "body": {
               "stringValue": "Example log record"
             },
             "attributes": [
               {
                  "key": "source.ip",
                  "value": {
                    "stringValue": "123.124.125.1"
                  }
               },
               {
                 "key": "string.attribute",
                 "value": {
                   "stringValue": "sending some data for Index function"
                 }
               },
               {
                 "key": "boolean.attribute",
                 "value": {
                   "boolValue": true
                 }
               },
               {
                 "key": "int.attribute",
                 "value": {
                   "intValue": "10"
                 }
               },
               {
                 "key": "double.attribute",
                 "value": {
                   "doubleValue": 637.704
                 }
               },
               {
                 "key": "array.attribute",
                 "value": {
                   "arrayValue": {
                     "values": [
                       {
                         "stringValue": "many"
                       },
                       {
                         "stringValue": "values"
                       }
                     ]
                   }
                 }
               },
               {
                 "key": "map.attribute",
                 "value": {
                   "kvlistValue": {
                     "values": [
                       {
                         "key": "some.map.key",
                         "value": {
                           "stringValue": "some value"
                         }
                       }
                     ]
                   }
                 }
               }
             ]
           }
         ]
       }
     ]
   }
 ]
}'

// output 
LogRecord #0
ObservedTimestamp: 2018-12-13 14:51:00.3 +0000 UTC
Timestamp: 2018-12-13 14:51:00.3 +0000 UTC
SeverityText: Information
SeverityNumber: Info2(10)
Body: Str(Example log record)
Attributes:
     -> source.ip: Str(123.124.125.1)
     -> string.attribute: Str(sending some data for Index function)
     -> boolean.attribute: Bool(true)
     -> int.attribute: Int(10)
     -> double.attribute: Double(637.704)
     -> array.attribute: Slice(["many","values"])
     -> map.attribute: Map({"some.map.key":"some value"})
     -> indexof: Int(1)
Trace ID: 5b8efff798038103d269b633813fc60c
Span ID: eee19b7ec3c1b174
Flags: 0
        {"resource": {}, "otelcol.component.id": "debug", "otelcol.component.kind": "exporter", "otelcol.signal": "logs"}

Documentation

  • changelog

@mashhurs mashhurs marked this pull request as ready for review June 5, 2025 16:25
@mashhurs mashhurs requested a review from a team as a code owner June 5, 2025 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pkg/ottl] New function: Index
2 participants