Skip to content
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

The ExtractPatterns function doesn't seem to be working #29435

Closed
stephenhong opened this issue Nov 21, 2023 · 3 comments
Closed

The ExtractPatterns function doesn't seem to be working #29435

stephenhong opened this issue Nov 21, 2023 · 3 comments
Labels
bug Something isn't working needs triage New item requiring triage pkg/ottl

Comments

@stephenhong
Copy link

Component(s)

pkg/ottl

What happened?

Description

Hello team, I'm testing the transform processor to format my logs and facing some trouble. I have the following log record in a file
16:45:57.603 INFO trace_id=4072391c964eb98bdc913a4f901907ba span_id=7c7a53100483dfa1 trace_flags=01 - Hello World
I use the file log receiver to read this log from the file and export it to the console via debug exporter. Here is the resulting log record

LogRecord #0
ObservedTimestamp: 2023-11-21 15:29:24.861318 +0000 UTC
Timestamp: 1970-01-01 00:00:00 +0000 UTC
SeverityText: INFO
SeverityNumber: Info(9)
Body: Str(16:45:57.603 INFO trace_id=4072391c964eb98bdc913a4f901907ba span_id=7c7a53100483dfa1 trace_flags=01 - Hello World)
Attributes:
     -> log.file.name: Str(app.log)
Trace ID: 
Span ID: 
Flags: 0

Then I want to use the transform processor to extract the span id from the log record and fill in the empty field like this

processors:
  transform:
    error_mode: ignore
    log_statements:
      - context: log
        statements:
          - set(severity_number, SEVERITY_NUMBER_INFO) where IsString(body) and IsMatch(body, "\\sINFO\\s")
          - set(severity_text, "INFO") where IsString(body) and IsMatch(body, "\\sINFO\\s")
          - set(cache, ExtractPatterns(body, "span_id\=(?P<SpanId>[\w]*)")
          - set(span_id.string, cache["SpanId"])

but I get the following error

Error: invalid configuration: processors::transform: unable to parse OTTL statement "set(span_id.string, ExtractPatterns(body, \"span_id\\=(?P<SpanId>[\\w]*)\")": statement has invalid syntax: 1:43: invalid quoted string "\"span_id\\=(?P<SpanId>[\\w]*)\"": invalid syntax
2023/11/21 10:38:16 collector server run finished with error: invalid configuration: processors::transform: unable to parse OTTL statement "set(span_id.string, ExtractPatterns(body, \"span_id\\=(?P<SpanId>[\\w]*)\")": statement has invalid syntax: 1:43: invalid quoted string "\"span_id\\=(?P<SpanId>[\\w]*)\"": invalid syntax

I tried to use different variations of the regex pattern but they all returned invalid syntax error, so I started to wonder if the ExtractPatterns function is working at all...? I also tried
- merge_maps(cache, ExtractPatterns(body, "span_id\=(?P<SpanId>[\w]*)"), "upsert")
but still got the invalid syntax error.

Steps to Reproduce

Set up the transform processor like this

processors:
  transform:
    error_mode: ignore
    log_statements:
      - context: log
        statements:
          - set(severity_number, SEVERITY_NUMBER_INFO) where IsString(body) and IsMatch(body, "\\sINFO\\s")
          - set(severity_text, "INFO") where IsString(body) and IsMatch(body, "\\sINFO\\s")
          - set(cache, ExtractPatterns(body, "span_id\=(?P<SpanId>[\w]*)")
          - set(span_id.string, cache["SpanId"])

then run the collector

Expected Result

span_id field is populated

Actual Result

invalid syntax error

Collector version

0.89.0

Environment information

Environment

OS: Mac OS X 13.6.1
Compiler(if manually compiled): (e.g., "go 14.2")

OpenTelemetry Collector configuration

processors:
  transform:
    error_mode: ignore
    log_statements:
      - context: log
        statements:
          - set(severity_number, SEVERITY_NUMBER_INFO) where IsString(body) and IsMatch(body, "\\sINFO\\s")
          - set(severity_text, "INFO") where IsString(body) and IsMatch(body, "\\sINFO\\s")
          - set(cache, ExtractPatterns(body, "span_id\=(?P<SpanId>[\w]*)")
          - set(span_id.string, cache["SpanId"])

Log output

Error: invalid configuration: processors::transform: unable to parse OTTL statement "set(span_id.string, ExtractPatterns(body, \"span_id\\=(?P<SpanId>[\\w]*)\")": statement has invalid syntax: 1:43: invalid quoted string "\"span_id\\=(?P<SpanId>[\\w]*)\"": invalid syntax
2023/11/21 10:38:16 collector server run finished with error: invalid configuration: processors::transform: unable to parse OTTL statement "set(span_id.string, ExtractPatterns(body, \"span_id\\=(?P<SpanId>[\\w]*)\")": statement has invalid syntax: 1:43: invalid quoted string "\"span_id\\=(?P<SpanId>[\\w]*)\"": invalid syntax

Additional context

No response

@stephenhong stephenhong added bug Something isn't working needs triage New item requiring triage labels Nov 21, 2023
Copy link
Contributor

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@stephenhong
Copy link
Author

I was able to resolve this. I was missing a ) and the \ must be escaped because of how the collector reads the config.
Using - set(cache, ExtractPatterns(body, "span_id\\=(?P<SpanId>[\\w]*)")) works, instead of - set(cache, ExtractPatterns(body, "span_id\=(?P<SpanId>[\w]*)")

@crobert-1
Copy link
Member

Thanks for following up and providing the solution for your issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage New item requiring triage pkg/ottl
Projects
None yet
Development

No branches or pull requests

2 participants