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

feat: add headers to OTEL config #333

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

Conversation

philtremblay
Copy link

Adds OtelMetricsExporterOTLPHeadersFlag to be able to add headers to the OTEL Endpoint

@philtremblay philtremblay requested a review from a team as a code owner March 6, 2025 15:33
Copy link

coderabbitai bot commented Mar 6, 2025

Walkthrough

The changes add support for specifying custom headers in the OpenTelemetry metrics and traces exporters. New flags for headers have been introduced in both CLI files, and the header values are parsed into maps. The configuration structures (OTLPConfig) in both modules now include a Headers field. In the module functions, conditional logic appends exporter options for gRPC and HTTP modes when headers are provided, thereby enabling enhanced configuration of exporter communications.

Changes

Files Change Summary
otlp/otlpmetrics/cli.go
otlp/otlptraces/cli.go
Added new flags (OtelMetricsExporterOTLPHeadersFlag and OtelTracesExporterOTLPHeadersFlag) to register string slice inputs. Updated AddFlags and FXModuleFromFlags to parse header key-value pairs into a headers map for inclusion in the OTLPConfig structure.
otlp/otlpmetrics/module.go
otlp/otlptraces/traces.go
Introduced a new field Headers map[string]string in OTLPConfig. In the MetricsModule and TracesModule functions, added conditional logic to append export options (for both gRPC and HTTP exporters) if headers are provided in the configuration.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant FXModule
    participant Config
    participant ExporterModule

    User->>CLI: Provide header flags (key=value pairs)
    CLI->>FXModule: Parse custom header flags
    FXModule->>Config: Build OTLPConfig with headers map
    Config->>ExporterModule: Pass configuration data
    ExporterModule->>ExporterModule: Conditionally add gRPC/HTTP header options
    ExporterModule-->>User: Exporter setup complete with custom headers
Loading

Poem

Hop along, dear friend, with glee,
A header flag now sets us free,
Custom keys in a rhythmic map,
Across gRPC and HTTP, a seamless lap,
My whiskers twitch with code so sweet,
Exporters sing a header beat!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fbcb459 and 3bb497a.

📒 Files selected for processing (4)
  • otlp/otlpmetrics/cli.go (4 hunks)
  • otlp/otlpmetrics/module.go (3 hunks)
  • otlp/otlptraces/cli.go (4 hunks)
  • otlp/otlptraces/traces.go (3 hunks)
🔇 Additional comments (15)
otlp/otlpmetrics/module.go (3)

45-45: LGTM - Adding Headers field to OTLPConfig struct

The addition of the Headers field to the OTLPConfig struct is a clean way to support custom headers in the OTLP configuration.


136-140: LGTM - Header support for gRPC mode

The implementation correctly adds the headers option to the gRPC exporter when headers are provided.


156-160: LGTM - Header support for HTTP mode

The implementation correctly adds the headers option to the HTTP exporter when headers are provided.

otlp/otlptraces/traces.go (3)

36-36: LGTM - Adding Headers field to OTLPConfig struct

The addition of the Headers field to the OTLPConfig struct is consistent with the metrics module implementation.


113-117: LGTM - Header support for gRPC mode

The implementation correctly adds the headers option to the gRPC exporter when headers are provided.


129-133: LGTM - Header support for HTTP mode

The implementation correctly adds the headers option to the HTTP exporter when headers are provided.

otlp/otlptraces/cli.go (5)

4-4: LGTM - Added strings import

The strings package is correctly imported to support the header parsing functionality.


21-21: LGTM - New constant for OTLP headers flag

The constant is well-named and maintains consistency with the existing constants.


35-35: LGTM - Added new flag for OTLP headers

The StringSlice flag type is appropriate for handling multiple headers.


54-62: LGTM - Header parsing implementation

The header parsing logic correctly splits each header by the first '=' character and only adds entries with both key and value to the map.


68-68: LGTM - Added Headers field to OTLPConfig instantiation

The headers map is correctly passed to the OTLPConfig struct.

otlp/otlpmetrics/cli.go (4)

4-4: LGTM - Added strings import

The strings package is correctly imported to support the header parsing functionality.


23-23: LGTM - New constant for OTLP headers flag

The constant is well-named and maintains consistency with the existing constants.


36-36: LGTM - Added new flag for OTLP headers

The StringSlice flag type is appropriate for handling multiple headers.


63-63: LGTM - Added Headers field to OTLPConfig instantiation

The headers map is correctly passed to the OTLPConfig struct.

Comment on lines +51 to +56
otelMetricsExporterOTLPHeaders, _ := cmd.Flags().GetStringSlice(OtelMetricsExporterOTLPHeadersFlag)
headersMap := make(map[string]string)
for _, header := range otelMetricsExporterOTLPHeaders {
parts := strings.SplitN(header, "=", 2)
headersMap[parts[0]] = parts[1]
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add validation to prevent panic with malformed headers

The current implementation assumes that splitting the header always produces at least two parts, which could cause a panic if a malformed header is provided (e.g., a header without an '=' character).

Apply this diff to fix the issue:

 headersMap := make(map[string]string)
 for _, header := range otelMetricsExporterOTLPHeaders {
 	parts := strings.SplitN(header, "=", 2)
-	headersMap[parts[0]] = parts[1]
+	if len(parts) == 2 {
+		headersMap[parts[0]] = parts[1]
+	}
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
otelMetricsExporterOTLPHeaders, _ := cmd.Flags().GetStringSlice(OtelMetricsExporterOTLPHeadersFlag)
headersMap := make(map[string]string)
for _, header := range otelMetricsExporterOTLPHeaders {
parts := strings.SplitN(header, "=", 2)
headersMap[parts[0]] = parts[1]
}
otelMetricsExporterOTLPHeaders, _ := cmd.Flags().GetStringSlice(OtelMetricsExporterOTLPHeadersFlag)
headersMap := make(map[string]string)
for _, header := range otelMetricsExporterOTLPHeaders {
parts := strings.SplitN(header, "=", 2)
if len(parts) == 2 {
headersMap[parts[0]] = parts[1]
}
}

Copy link

codecov bot commented Mar 6, 2025

Codecov Report

Attention: Patch coverage is 26.47059% with 25 lines in your changes missing coverage. Please review.

Project coverage is 25.42%. Comparing base (fbcb459) to head (3bb497a).

Files with missing lines Patch % Lines
otlp/otlpmetrics/cli.go 0.00% 8 Missing ⚠️
otlp/otlpmetrics/module.go 0.00% 8 Missing ⚠️
otlp/otlptraces/cli.go 50.00% 4 Missing and 1 partial ⚠️
otlp/otlptraces/traces.go 50.00% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #333      +/-   ##
==========================================
- Coverage   25.62%   25.42%   -0.21%     
==========================================
  Files         117      117              
  Lines        5062     5094      +32     
==========================================
- Hits         1297     1295       -2     
- Misses       3665     3699      +34     
  Partials      100      100              

☔ 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