Otelgin: Add A WithSpanOptions Option
Problem Statement
The otelgin instrumentation is a crucial part of the OpenTelemetry ecosystem, providing a way to instrument Go applications and generate spans for distributed tracing. However, one of the limitations of the current implementation is the lack of a way to add extra attributes, links, etc. to the spans it generates. This is in contrast to the otelhttp instrumentation, which provides a WithSpanOptions
option to customize the span generation process.
The inability to add extra attributes to spans generated by otelgin can make it difficult to provide additional context to the traces, which can be crucial for debugging and performance optimization purposes. For example, in a web application, it may be necessary to add attributes such as the user ID, session ID, or request ID to the span, which can be used to correlate the span with the corresponding request or user session.
Proposed Solution
To address this limitation, we propose adding a WithSpanOptions
option to the otelgin instrumentation. This option would allow users to customize the span generation process by providing additional attributes, links, etc. to the span.
The WithSpanOptions
option would take a variable number of trace.SpanStartOption
arguments, which would be used to configure the span generation process. This would provide a flexible way to add custom attributes, links, etc. to the span, without modifying the underlying instrumentation.
Here is an example of how the WithSpanOptions
option could be implemented:
type Option func(*otelgin) error
func WithSpanOptions(opts ...trace.SpanStartOption) Option {
return func(o *otelgin) error {
o.spanOptions = append(o.spanOptions, opts...)
return nil
}
}
This implementation defines a new Option
type, which represents a function that takes a pointer to an otelgin
instance and returns an error. The WithSpanOptions
function takes a variable number of trace.SpanStartOption
arguments and returns a new Option
instance that appends the provided options to the otelgin
instance's spanOptions
field.
Benefits
The addition of the WithSpanOptions
option would provide several benefits, including:
- Improved flexibility: The
WithSpanOptions
option would allow users to customize the span generation process by providing additional attributes, links, etc. to the span. - Better debugging and performance optimization: By providing additional context to the traces, users would be able to better debug and optimize their applications.
- Consistency with other instrumentations: The addition of the
WithSpanOptions
option would bring the otelgin instrumentation in line with other OpenTelemetry instrumentations, such as otelhttp.
Implementation
To implement the WithSpanOptions
option, we would need to modify the otelgin instrumentation to accept the new option. This would involve updating the otelgin
struct to include a new spanOptions
field, which would store the provided options.
Here is an example of how the updated otel
struct could be implemented:
type otelgin struct {
// ...
spanOptions []trace.SpanStartOption
}
We would also need to update the otelgin
constructor to accept the new option and append the provided options to the spanOptions
field.
Here is an example of how the updated otelgin
constructor could be implemented:
func Newotelgin(opts ...Option) (*otelgin, error) {
o := &otelgin{}
for _, opt := range opts {
if err := opt(o); err != nil {
return nil, err
}
}
return o, nil
}
Example Use Case
Here is an example of how the WithSpanOptions
option could be used in a real-world application:
func main() {
otelgin := otelgin.New(
otelgin.WithSpanOptions(
trace.WithAttributes("user_id", "123"),
trace.WithAttributes("session_id", "456"),
),
)
// ...
}
In this example, we create a new otelgin
instance and pass the WithSpanOptions
option with two attributes: user_id
and session_id
. The otelgin
instance would then generate spans with these attributes, providing additional context to the traces.
Conclusion
Q: What is the purpose of the WithSpanOptions option?
A: The WithSpanOptions option is designed to provide a flexible way to customize the span generation process by adding extra attributes, links, etc. to the spans generated by the otelgin instrumentation.
Q: Why is this option necessary?
A: The current implementation of the otelgin instrumentation does not provide a way to add extra attributes to the spans it generates. This can make it difficult to provide additional context to the traces, which can be crucial for debugging and performance optimization purposes.
Q: How does the WithSpanOptions option work?
A: The WithSpanOptions option takes a variable number of trace.SpanStartOption arguments, which are used to configure the span generation process. This allows users to add custom attributes, links, etc. to the span without modifying the underlying instrumentation.
Q: What are some examples of how the WithSpanOptions option could be used?
A: Here are a few examples:
- Adding a user ID attribute to the span:
otelgin.WithSpanOptions(trace.WithAttributes("user_id", "123"))
- Adding a session ID attribute to the span:
otelgin.WithSpanOptions(trace.WithAttributes("session_id", "456"))
- Adding a custom link to the span:
otelgin.WithSpanOptions(trace.WithLink("custom_link", "https://example.com"))
Q: How does the WithSpanOptions option affect the performance of the otelgin instrumentation?
A: The WithSpanOptions option should not have a significant impact on the performance of the otelgin instrumentation. The option is designed to be flexible and customizable, but it should not introduce any significant overhead.
Q: Is the WithSpanOptions option compatible with other OpenTelemetry instrumentations?
A: Yes, the WithSpanOptions option is designed to be compatible with other OpenTelemetry instrumentations, such as otelhttp. This means that users can use the WithSpanOptions option with other instrumentations to add custom attributes, links, etc. to the spans.
Q: How can I contribute to the development of the WithSpanOptions option?
A: If you are interested in contributing to the development of the WithSpanOptions option, please submit a pull request to the OpenTelemetry repository. We welcome contributions from the community and are happy to work with you to implement this feature.
Q: What are the next steps for the WithSpanOptions option?
A: The next steps for the WithSpanOptions option are to implement the feature and test it thoroughly. We will also work with the OpenTelemetry community to ensure that the feature is well-documented and easy to use.
Q: When can I expect to see the WithSpanOptions option in a release?
A: We anticipate that the WithSpanOptions option will be included in the next major release of the OpenTelemetry project. However, this is subject to change and we will provide updates on the project's roadmap as more information becomes available.
Q: How can I get involved in the OpenTelemetry project?
A: If you are interested in getting involved in the OpenTelemetry project, please visit our website and join our community. We welcome contributions from the community and are happy to work with you to implement new features and improve project.
Q: What are some other features that are being developed for the OpenTelemetry project?
A: We are currently working on several other features for the OpenTelemetry project, including:
- Improved support for distributed tracing
- Enhanced security features
- Better integration with other OpenTelemetry instrumentations
We will provide updates on these features as more information becomes available.