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

[mdatagen] Support logs #12659

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

Conversation

sincejune
Copy link
Contributor

Description

This PR introduces the foundational changes necessary for supporting logs data in the mdatagen tool.

mdatagen files for logs

This update includes the generation of generated_logs.go and generated_logs_test.go. These files are specifically for receiver and scraper components that support logs data, enabling initial log handling capabilities.

Introducing LogsBuilder

This PR introduced a LogsBuilder similar to the existing MetricsBuilder. It provides a structured way to manage log data with the following functions:

  1. Emit(...ResourceLogsOption)
    Similar to Emit function in MetricsBuilder
  2. EmitForResource(...ResourceLogsOption)
    Similar to EmitForResource function in MetricsBuilder
  3. AppendLogRecord(plog.LogRecord)
    This function appends a log record to an internal buffer. The buffered log records are used to construct a ScopeLog when the Emit() or EmitForResource() functions are called. Scope name and version are automatically generated.

Next steps

  • Add more test cases to LogsBuilder (e.g. reading test configs)
  • Add LogsBuilderConfig to read logs property in metadata.yml to send structured logs
  • Update receivers in contrib to use LogsBuilder.

Example usage:

lb := NewLogsBuilder(settings)

res := pcommon.NewResource()
res.Attributes().PutStr("region", "us-west-1")

// append the first log record
lr := plog.NewLogRecord()
lr.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
lr.Attributes().PutStr("type", "log")
lr.Body().SetStr("the first log record")

// append the second log record
lr2 := plog.NewLogRecord()
lr2.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
lr2.Attributes().PutStr("type", "event")
lr2.Body().SetStr("the second log record")

lb.AppendLogRecord(lr)
lb.AppendLogRecord(lr2)

logs := lb.Emit(WithLogsResource(res))

Example output:

resourceLogs:
  - resource:
      attributes:
        - key: region
          value:
            stringValue: us-west-1
    scopeLogs:
      - logRecords:
          - attributes:
              - key: type
                value:
                  stringValue: log
            body:
              stringValue: the first log record
            spanId: ""
            timeUnixNano: "1742291226022739000"
            traceId: ""
          - attributes:
              - key: type
                value:
                  stringValue: event
            body:
              stringValue: the second log record
            spanId: ""
            timeUnixNano: "1742291226022739000"
            traceId: ""
        scope:
          name: github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscloudwatchreceiver
          version: latest

Link to tracking issue

Part of #12571

Testing

Added

Documentation

Added

@sincejune sincejune requested review from a team, evan-bradley and dmitryax as code owners March 18, 2025 12:27
Copy link

codecov bot commented Mar 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.59%. Comparing base (12dd09f) to head (ab14c7a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12659      +/-   ##
==========================================
+ Coverage   91.55%   91.59%   +0.04%     
==========================================
  Files         480      483       +3     
  Lines       26391    26504     +113     
==========================================
+ Hits        24162    24277     +115     
+ Misses       1766     1763       -3     
- Partials      463      464       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant