Skip to content

feat(otelfiber): add fiber v3 support #1277

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 1 commit into
base: main
Choose a base branch
from

Conversation

drev74
Copy link

@drev74 drev74 commented Mar 29, 2025

What was done

  1. Added support for fiber/v3 for otelfiber
  2. Decoupled otelfiber from fiber/v2

Issues:

  1. Where to merge otelfiber/v3 to keep otelfiber/v2 ?
  2. otelfiber tests fail

Summary by CodeRabbit

  • New Features
    • Introduced utility functions for efficient string and byte conversions.
  • Refactor
    • Updated the underlying framework version, enhancing context handling and request processing.
  • Tests
    • Refined test configurations with clearer timeout settings for improved reliability.

@drev74 drev74 requested a review from a team as a code owner March 29, 2025 19:44
@drev74 drev74 requested review from gaby, sixcolors, ReneWerner87 and efectn and removed request for a team March 29, 2025 19:44
Copy link
Contributor

coderabbitai bot commented Mar 29, 2025

Walkthrough

The pull request updates several files to support the upgrade from Fiber v2 to Fiber v3. Function signatures and field types have been changed from using pointer receivers (e.g., *fiber.Ctx) to value receivers (fiber.Ctx). In addition, calls to context methods (e.g., switching from UserContext() to Context()) have been adjusted. New utility functions have been introduced to facilitate zero-allocation string and byte conversions, and test configurations were refined with a more explicit timeout setup.

Changes

Files Change Summary
otelfiber/config.go, otelfiber/fiber.go, otelfiber/semconv.go Upgraded function signatures and field types from using *fiber.Ctx to fiber.Ctx; updated Fiber imports from v2 to v3; revised calls to utility functions by removing the package prefix (e.g., utils.CopyStringCopyString)
otelfiber/example/server.go, otelfiber/otelfiber_test/fiber_test.go Updated Fiber import paths from v2 to v3; modified route handler signatures and context method calls (from UserContext() to Context()); adjusted test configurations by replacing hardcoded timeouts with a structured timeout setting
otelfiber/utils.go Added a new file providing utility functions (UnsafeString, UnsafeBytes, CopyString, CopyBytes) for efficient string and byte conversions without additional memory allocations

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant M as Middleware
    participant H as Handler
    participant U as Utils

    C->>M: Send HTTP Request
    M->>M: Create fiber.Ctx (value type) from request
    M->>U: Optionally invoke CopyString / UnsafeBytes if needed
    M->>H: Pass updated context to handler
    H-->>M: Return handler response
    M-->>C: Deliver HTTP Response
Loading

Possibly related PRs

Suggested labels

✏️ Feature

Suggested reviewers

  • sixcolors
  • gaby
  • ReneWerner87
  • efectn

Poem

I'm a bunny in the code field, hopping with delight,
Fiber v3 now shines so bright.
No pointers to chase, just values to share,
New utility carrots make life fair.
With every hop, I cheer this change—so light!
Carrots and code, a whimsical bite.
Happy to nibble on updates day and night!

✨ 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 plan to trigger planning for file edits and PR creation.
  • @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 or @coderabbitai title 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.

@github-actions github-actions bot added the ✏️ Feature New feature or request label Mar 29, 2025
Copy link
Contributor

@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: 0

🧹 Nitpick comments (2)
otelfiber/example/server.go (1)

37-39: Fix commented-out code to match new API.

The commented-out code for WithSpanNameFormatter still uses a pointer receiver (*fiber.Ctx) which doesn't match the updated API.

-//app.Use(otelfiber.Middleware(otelfiber.WithSpanNameFormatter(func(ctx *fiber.Ctx) string {
+//app.Use(otelfiber.Middleware(otelfiber.WithSpanNameFormatter(func(ctx fiber.Ctx) string {
otelfiber/semconv.go (1)

84-112: Consider adding validation for empty credentials.

The HasBasicAuth function correctly identifies and decodes basic authentication headers. However, it might be worth adding validation for empty usernames or passwords to prevent potential security issues.

func HasBasicAuth(auth string) (string, bool) {
    if auth == "" {
        return "", false
    }

    // Check if the Authorization header is Basic
    if !strings.HasPrefix(auth, "Basic ") {
        return "", false
    }

    // Decode the header contents
    raw, err := base64.StdEncoding.DecodeString(auth[6:])
    if err != nil {
        return "", false
    }

    // Get the credentials
    creds := UnsafeString(raw)

    // Check if the credentials are in the correct form
    // which is "username:password".
    index := strings.Index(creds, ":")
    if index == -1 {
        return "", false
    }

    // Get the username
+   username := creds[:index]
+   if username == "" {
+       return "", false
+   }
+
-   return creds[:index], true
+   return username, true
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ea8fb10 and 4e4573c.

⛔ Files ignored due to path filters (4)
  • otelfiber/example/go.mod is excluded by !**/*.mod
  • otelfiber/example/go.sum is excluded by !**/*.sum, !**/*.sum
  • otelfiber/go.mod is excluded by !**/*.mod
  • otelfiber/go.sum is excluded by !**/*.sum, !**/*.sum
📒 Files selected for processing (6)
  • otelfiber/config.go (5 hunks)
  • otelfiber/example/server.go (2 hunks)
  • otelfiber/fiber.go (6 hunks)
  • otelfiber/otelfiber_test/fiber_test.go (17 hunks)
  • otelfiber/semconv.go (5 hunks)
  • otelfiber/utils.go (1 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
otelfiber/fiber.go (1)
otelfiber/utils.go (1)
  • CopyString (18-20)
otelfiber/semconv.go (1)
otelfiber/utils.go (3)
  • CopyString (18-20)
  • CopyBytes (23-27)
  • UnsafeString (8-10)
🔇 Additional comments (22)
otelfiber/utils.go (1)

1-28: Good implementation of utility functions for string and byte conversions.

These utility functions are well-documented and provide a clean way to unbundle from Fiber v2, supporting the PR's goal of decoupling. Using unsafe for zero-allocation conversions is a common pattern for performance-critical code.

A few observations:

  • UnsafeString and UnsafeBytes properly leverage Go's unsafe package for zero-allocation conversions
  • CopyString and CopyBytes provide safer alternatives when immutability is needed
  • All functions are properly documented with clear comments
otelfiber/fiber.go (6)

8-8: Updated import to Fiber v3.

The import has been correctly updated to use v3 of the Fiber framework.


82-82: Updated middleware function signature to use value receiver instead of pointer.

The function signature has been correctly updated to match Fiber v3's API change from pointer receivers to value receivers for context objects.


89-89: Updated context access method.

Changed from c.UserContext() to c.Context() to match Fiber v3's API changes.


114-114: Using newly created utility function.

Switched from utils.CopyString() to the local CopyString() implementation from the new utils.go file, helping to decouple from Fiber v2's utilities.


119-119: Updated context manipulation methods.

Changed from SetUserContext() to SetContext() and from UserContext() to Context() to align with Fiber v3's API changes.

Also applies to: 151-151, 167-167


178-178: Updated span name formatter function signature.

The function signature of defaultSpanNameFormatter has been correctly updated to use a value receiver instead of a pointer receiver for the context object.

otelfiber/config.go (3)

4-4: Updated import to Fiber v3.

The import has been correctly updated to use v3 of the Fiber framework.


13-13: Updated config struct field types.

All function type signatures in the config struct have been correctly updated to use value receivers instead of pointer receivers for the context object, aligning with Fiber v3's API changes.

Also applies to: 19-21


38-38: Updated option function signatures.

All option function signatures have been correctly updated to use value receivers instead of pointer receivers for the context object, maintaining consistency with the config struct changes.

Also applies to: 71-71, 96-96, 104-104

otelfiber/example/server.go (3)

10-10: Updated import to Fiber v3.

The import has been correctly updated to use v3 of the Fiber framework.


43-45: Updated handler function signatures.

Route handler function signatures have been correctly updated to use value receivers instead of pointer receivers for the context object, aligning with Fiber v3's API changes.

Also applies to: 47-51


49-49: Updated context access method.

Changed from c.UserContext() to c.Context() to match Fiber v3's API changes.

otelfiber/otelfiber_test/fiber_test.go (4)

10-13: Successfully updated import paths for fiber v3.

The imports have been appropriately updated to reflect the new versions:

  • Added time package for the explicit timeout configurations
  • Changed github.com/gofiber/contrib/otelfiber/v2 to /v3
  • Changed github.com/gofiber/fiber/v2 to /v3

This aligns with the PR objective to add fiber v3 support.


41-43: Correctly updated handler signature from pointer to value receiver.

The function signature has been updated from func(ctx *fiber.Ctx) error to func(ctx fiber.Ctx) error, changing from a pointer receiver to a value receiver, which is consistent with Fiber v3's API changes.


176-179: Context access method correctly updated.

The change from the implicit context access to the explicit ctx.Context() method reflects Fiber v3's API for accessing the underlying context.


181-181: Enhanced test configuration with explicit timeout.

The test configuration has been improved by using fiber.TestConfig{Timeout: 3 * time.Second} instead of a hardcoded millisecond value, which is more readable and maintainable.

otelfiber/semconv.go (5)

7-7: Successfully updated import for fiber v3.

The import has been updated from github.com/gofiber/fiber/v2 to github.com/gofiber/fiber/v3.


12-33: Function signature updated to use value receiver.

The function signature has been changed from pointer to value receiver (*fiber.Ctx to fiber.Ctx), which matches Fiber v3's API changes. The function body has also been updated to use direct utility functions without the utils. prefix.


35-74: Comprehensive update to value receivers and utility functions.

This function has been correctly updated to:

  1. Use value receivers (fiber.Ctx instead of *fiber.Ctx)
  2. Remove utils. prefix from utility functions (CopyString, CopyBytes)
  3. Maintain comments explaining the need for string copying (lines 38-40)

The changes maintain the original functionality while adapting to the new API.


76-82: Updated helper function to use value receiver.

The httpFlavorAttribute function has been properly updated to use a value receiver for consistency with the rest of the changes.


101-101: Removed utils prefix from UnsafeString.

The function call to utils.UnsafeString has been updated to UnsafeString, consistent with the approach in the rest of the file where utility functions are now directly called without the utils. prefix.

@gaby
Copy link
Member

gaby commented Mar 29, 2025

@drev74 The team is currently managing all these migrations. Fiber v3 is still under development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Feature New feature or request v3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants