Skip to content

CORE/FFI/GO - SpanFromContext implementation#4507

Merged
affonsov merged 12 commits intomainfrom
go/affonsov-otel-span-from-context
Aug 8, 2025
Merged

CORE/FFI/GO - SpanFromContext implementation#4507
affonsov merged 12 commits intomainfrom
go/affonsov-otel-span-from-context

Conversation

@affonsov
Copy link
Copy Markdown
Collaborator

OpenTelemetry Span Context Attachment Enhancement

Overview

This PR significantly enhances the OpenTelemetry integration by implementing comprehensive span context attachment functionality, enabling proper parent-child span relationships and improved tracing capabilities across the Glide client library.

Key Features Added

🔧 FFI Layer Enhancements

  • New FFI Functions:

    • create_named_otel_span() - Creates spans with custom names for user operations
    • create_otel_span_with_parent() - Creates child spans with parent-child relationships
    • create_batch_otel_span_with_parent() - Creates batch spans with parent context
  • Enhanced Error Handling:

    • Input validation with detailed logging
    • Graceful fallback mechanisms for invalid parent spans
    • Memory safety improvements with pointer validation
    • Panic recovery in span cleanup operations

🎯 Go Client API Improvements

  • Public Span Management APIs:

    • CreateSpan(name string) - Create named spans for user operations
    • EndSpan(spanPtr uint64) - Properly clean up spans
    • WithSpan(ctx, spanPtr) - Attach span context to Go contexts
    • DefaultSpanFromContext(ctx) - Extract span pointers from contexts
  • Context Integration:

    • Seamless integration with Go's context.Context
    • Configurable span extraction strategies
    • Thread-safe span context management

🔗 Span Hierarchy Support

  • Parent-Child Relationships: Commands executed within a span context automatically become child spans
  • Batch Operations: Batch commands properly inherit parent span context
  • Mixed Scenarios: Support for operations with and without parent context in the same session

Backward Compatibility

  • All existing OpenTelemetry functionality remains unchanged
  • New features are opt-in and don't affect existing code
  • Graceful degradation when OpenTelemetry is not initialized

Issue link

This Pull Request is linked to issue (URL): #4243

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one issue.
  • Commit message has a detailed description of what changed and why.
  • Tests are added or updated.
  • CHANGELOG.md and documentation files are updated.
  • Destination branch is correct - main or release
  • Create merge commit if merging release branch into main, squash otherwise.

@yipin-chen yipin-chen added go 🏃 golang wrapper Core changes 🪐 Used to label a PR as PR with significant changes that should trigger a full matrix tests. Telemetry 📺 labels Aug 1, 2025
Copy link
Copy Markdown
Contributor

@jonathanl-bq jonathanl-bq left a comment

Choose a reason for hiding this comment

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

Can you add MIRI tests as well? We need coverage for the new functions you added to the FFI layer.

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
…I functions including:

-Enhanced error handling tests for invalid inputs (null pointers, oversized names, invalid request types)
-Comprehensive integration tests covering span creation, parent-child relationships, and memory safety
-Batch span functionality tests with hierarchy validation
-Concurrent access testing and boundary condition validation
-Memory safety tests for span cleanup operations

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
- integration tests
- Unit tests
- Base client modifications to support the new parent/child span

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
- Simplify SpanFromContext function signature from (uint64, bool) to uint64
  - Remove redundant 'found' boolean return value
  - Functions now return 0 to indicate no parent span available
  - Update DefaultSpanFromContext and all related implementations
  - Improve error handling and panic recovery logic

- Add mock-logger-core package for miri tests
  - Create no-op implementations of log functions (error, warn, debug, info)
  - Add proper Cargo.toml configuration and dependency integration
  - Ensure miri tests can run without complex logging infrastructure

- Enhance mock-telemetry for better miri test coverage
  - Implement Display trait for TraceError
  - Add concrete implementations for GlideOpenTelemetry methods
  - Replace todo!() placeholders with working mock implementations
  - Add span_from_pointer method for completeness

- Update Go client span extraction logic
  - Simplify parent span detection from context
  - Remove unnecessary tuple unpacking in executeCommandWithRoute and executeBatch
  - Maintain backward compatibility while improving code clarity

- Add comprehensive OpenTelemetry examples and documentation
  - Create opentelemetry_examples_test.go with runnable examples
  - Update examples.md to reference new OpenTelemetry examples
  - Move extensive documentation from inline comments to dedicated examples
  - Provide clear usage patterns for span context management

This change improves the developer experience by simplifying the OpenTelemetry
integration API while maintaining full functionality and backward compatibility.
The miri test improvements ensure better test coverage in memory-safe environments.

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
- Remove ExampleWithSpan and ExampleDefaultSpanFromContext functions
- Rename ExampleOpenTelemetry_CreateSpan to ExampleOpenTelemetry
- Focus example on demonstrating parent-child span relationships
- Add clearer comments explaining span context usage with Valkey commands
- Clean up formatting and remove redundant content

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
…t, Del, etc.)

- Implement enhanced Cmd struct with command_bytes field and default implementation
- Add comprehensive Miri tests for all OTEL span creation functions
- Fix function signatures to properly mark unsafe extern "C" functions
- Improve error handling and fallback behavior for invalid parent spans
- Add extensive test coverage for span hierarchies and concurrent access
- Clean up code formatting and organize imports consistently
- Strengthen memory safety validation in FFI boundary functions

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
@affonsov affonsov force-pushed the go/affonsov-otel-span-from-context branch from bdcdbfc to a467e34 Compare August 6, 2025 22:04
Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
@affonsov affonsov merged commit c7db02f into main Aug 8, 2025
53 of 59 checks passed
@jonathanl-bq
Copy link
Copy Markdown
Contributor

Changelog is updated for this PR in #4567

zarkash-aws pushed a commit to zarkash-aws/valkey-glide that referenced this pull request Aug 10, 2025
* Core: Added span pointer conversion and validation

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* ffi: The changes add extensive test coverage for the OpenTelemetry FFI functions including:

-Enhanced error handling tests for invalid inputs (null pointers, oversized names, invalid request types)
-Comprehensive integration tests covering span creation, parent-child relationships, and memory safety
-Batch span functionality tests with hierarchy validation
-Concurrent access testing and boundary condition validation
-Memory safety tests for span cleanup operations

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* go: New OpenTelemetry functionality

- integration tests
- Unit tests
- Base client modifications to support the new parent/child span

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fixing ffi tests failing on github actions

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* Change glide-core function to be unsafe

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* Simplify OpenTelemetry span context API and improve miri test mocks

- Simplify SpanFromContext function signature from (uint64, bool) to uint64
  - Remove redundant 'found' boolean return value
  - Functions now return 0 to indicate no parent span available
  - Update DefaultSpanFromContext and all related implementations
  - Improve error handling and panic recovery logic

- Add mock-logger-core package for miri tests
  - Create no-op implementations of log functions (error, warn, debug, info)
  - Add proper Cargo.toml configuration and dependency integration
  - Ensure miri tests can run without complex logging infrastructure

- Enhance mock-telemetry for better miri test coverage
  - Implement Display trait for TraceError
  - Add concrete implementations for GlideOpenTelemetry methods
  - Replace todo!() placeholders with working mock implementations
  - Add span_from_pointer method for completeness

- Update Go client span extraction logic
  - Simplify parent span detection from context
  - Remove unnecessary tuple unpacking in executeCommandWithRoute and executeBatch
  - Maintain backward compatibility while improving code clarity

- Add comprehensive OpenTelemetry examples and documentation
  - Create opentelemetry_examples_test.go with runnable examples
  - Update examples.md to reference new OpenTelemetry examples
  - Move extensive documentation from inline comments to dedicated examples
  - Provide clear usage patterns for span context management

This change improves the developer experience by simplifying the OpenTelemetry
integration API while maintaining full functionality and backward compatibility.
The miri test improvements ensure better test coverage in memory-safe environments.

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fix lint

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* simplify OpenTelemetry examples and improve span relationship demo

- Remove ExampleWithSpan and ExampleDefaultSpanFromContext functions
- Rename ExampleOpenTelemetry_CreateSpan to ExampleOpenTelemetry
- Focus example on demonstrating parent-child span relationships
- Add clearer comments explaining span context usage with Valkey commands
- Clean up formatting and remove redundant content

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fix linter

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* - Add proper RequestType enum with concrete command variants (Get, Set, Del, etc.)
- Implement enhanced Cmd struct with command_bytes field and default implementation
- Add comprehensive Miri tests for all OTEL span creation functions
- Fix function signatures to properly mark unsafe extern "C" functions
- Improve error handling and fallback behavior for invalid parent spans
- Add extensive test coverage for span hierarchies and concurrent access
- Clean up code formatting and organize imports consistently
- Strengthen memory safety validation in FFI boundary functions

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* ffi lint fixes

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* removed (current behavior) from the comment

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

---------

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
alexr-bq pushed a commit that referenced this pull request Aug 25, 2025
* Core: Added span pointer conversion and validation

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* ffi: The changes add extensive test coverage for the OpenTelemetry FFI functions including:

-Enhanced error handling tests for invalid inputs (null pointers, oversized names, invalid request types)
-Comprehensive integration tests covering span creation, parent-child relationships, and memory safety
-Batch span functionality tests with hierarchy validation
-Concurrent access testing and boundary condition validation
-Memory safety tests for span cleanup operations

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* go: New OpenTelemetry functionality

- integration tests
- Unit tests
- Base client modifications to support the new parent/child span

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fixing ffi tests failing on github actions

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* Change glide-core function to be unsafe

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* Simplify OpenTelemetry span context API and improve miri test mocks

- Simplify SpanFromContext function signature from (uint64, bool) to uint64
  - Remove redundant 'found' boolean return value
  - Functions now return 0 to indicate no parent span available
  - Update DefaultSpanFromContext and all related implementations
  - Improve error handling and panic recovery logic

- Add mock-logger-core package for miri tests
  - Create no-op implementations of log functions (error, warn, debug, info)
  - Add proper Cargo.toml configuration and dependency integration
  - Ensure miri tests can run without complex logging infrastructure

- Enhance mock-telemetry for better miri test coverage
  - Implement Display trait for TraceError
  - Add concrete implementations for GlideOpenTelemetry methods
  - Replace todo!() placeholders with working mock implementations
  - Add span_from_pointer method for completeness

- Update Go client span extraction logic
  - Simplify parent span detection from context
  - Remove unnecessary tuple unpacking in executeCommandWithRoute and executeBatch
  - Maintain backward compatibility while improving code clarity

- Add comprehensive OpenTelemetry examples and documentation
  - Create opentelemetry_examples_test.go with runnable examples
  - Update examples.md to reference new OpenTelemetry examples
  - Move extensive documentation from inline comments to dedicated examples
  - Provide clear usage patterns for span context management

This change improves the developer experience by simplifying the OpenTelemetry
integration API while maintaining full functionality and backward compatibility.
The miri test improvements ensure better test coverage in memory-safe environments.

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fix lint

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* simplify OpenTelemetry examples and improve span relationship demo

- Remove ExampleWithSpan and ExampleDefaultSpanFromContext functions
- Rename ExampleOpenTelemetry_CreateSpan to ExampleOpenTelemetry
- Focus example on demonstrating parent-child span relationships
- Add clearer comments explaining span context usage with Valkey commands
- Clean up formatting and remove redundant content

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fix linter

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* - Add proper RequestType enum with concrete command variants (Get, Set, Del, etc.)
- Implement enhanced Cmd struct with command_bytes field and default implementation
- Add comprehensive Miri tests for all OTEL span creation functions
- Fix function signatures to properly mark unsafe extern "C" functions
- Improve error handling and fallback behavior for invalid parent spans
- Add extensive test coverage for span hierarchies and concurrent access
- Clean up code formatting and organize imports consistently
- Strengthen memory safety validation in FFI boundary functions

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* ffi lint fixes

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* removed (current behavior) from the comment

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

---------

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
Signed-off-by: Alex Rehnby-Martin <alex.rehnby-martin@improving.com>
alexr-bq pushed a commit that referenced this pull request Sep 3, 2025
* Core: Added span pointer conversion and validation

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* ffi: The changes add extensive test coverage for the OpenTelemetry FFI functions including:

-Enhanced error handling tests for invalid inputs (null pointers, oversized names, invalid request types)
-Comprehensive integration tests covering span creation, parent-child relationships, and memory safety
-Batch span functionality tests with hierarchy validation
-Concurrent access testing and boundary condition validation
-Memory safety tests for span cleanup operations

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* go: New OpenTelemetry functionality

- integration tests
- Unit tests
- Base client modifications to support the new parent/child span

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fixing ffi tests failing on github actions

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* Change glide-core function to be unsafe

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* Simplify OpenTelemetry span context API and improve miri test mocks

- Simplify SpanFromContext function signature from (uint64, bool) to uint64
  - Remove redundant 'found' boolean return value
  - Functions now return 0 to indicate no parent span available
  - Update DefaultSpanFromContext and all related implementations
  - Improve error handling and panic recovery logic

- Add mock-logger-core package for miri tests
  - Create no-op implementations of log functions (error, warn, debug, info)
  - Add proper Cargo.toml configuration and dependency integration
  - Ensure miri tests can run without complex logging infrastructure

- Enhance mock-telemetry for better miri test coverage
  - Implement Display trait for TraceError
  - Add concrete implementations for GlideOpenTelemetry methods
  - Replace todo!() placeholders with working mock implementations
  - Add span_from_pointer method for completeness

- Update Go client span extraction logic
  - Simplify parent span detection from context
  - Remove unnecessary tuple unpacking in executeCommandWithRoute and executeBatch
  - Maintain backward compatibility while improving code clarity

- Add comprehensive OpenTelemetry examples and documentation
  - Create opentelemetry_examples_test.go with runnable examples
  - Update examples.md to reference new OpenTelemetry examples
  - Move extensive documentation from inline comments to dedicated examples
  - Provide clear usage patterns for span context management

This change improves the developer experience by simplifying the OpenTelemetry
integration API while maintaining full functionality and backward compatibility.
The miri test improvements ensure better test coverage in memory-safe environments.

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fix lint

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* simplify OpenTelemetry examples and improve span relationship demo

- Remove ExampleWithSpan and ExampleDefaultSpanFromContext functions
- Rename ExampleOpenTelemetry_CreateSpan to ExampleOpenTelemetry
- Focus example on demonstrating parent-child span relationships
- Add clearer comments explaining span context usage with Valkey commands
- Clean up formatting and remove redundant content

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* fix linter

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* - Add proper RequestType enum with concrete command variants (Get, Set, Del, etc.)
- Implement enhanced Cmd struct with command_bytes field and default implementation
- Add comprehensive Miri tests for all OTEL span creation functions
- Fix function signatures to properly mark unsafe extern "C" functions
- Improve error handling and fallback behavior for invalid parent spans
- Add extensive test coverage for span hierarchies and concurrent access
- Clean up code formatting and organize imports consistently
- Strengthen memory safety validation in FFI boundary functions

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* ffi lint fixes

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

* removed (current behavior) from the comment

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>

---------

Signed-off-by: affonsov <67347924+affonsov@users.noreply.github.com>
Signed-off-by: Alex Rehnby-Martin <alex.rehnby-martin@improving.com>
@affonsov affonsov deleted the go/affonsov-otel-span-from-context branch September 15, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core changes 🪐 Used to label a PR as PR with significant changes that should trigger a full matrix tests. go 🏃 golang wrapper Telemetry 📺

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants