Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Changed

- Improve performance by reducing allocations in the gRPC stats handler in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#8035)
- Export the `ReadEvents` and `WriteEvents` constants in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp` so they can be used in `WithMessageEvents`. (#8153)

### Deprecated

Expand Down
8 changes: 5 additions & 3 deletions instrumentation/net/http/otelhttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@ func WithFilter(f Filter) Option {
})
}

type event int
// Event represents message event types for [WithMessageEvents].
type Event int
Comment thread
sonalgaud12 marked this conversation as resolved.

// Different types of events that can be recorded, see WithMessageEvents.
const (
ReadEvents event = iota
unspecifiedEvents Event = iota
ReadEvents
WriteEvents
)

Expand All @@ -161,7 +163,7 @@ const (
// using the ReadBytesKey
// - WriteEvents: Record the number of bytes written after every http.ResponeWriter.Write
// using the WriteBytesKey
func WithMessageEvents(events ...event) Option {
func WithMessageEvents(events ...Event) Option {
return optionFunc(func(c *config) {
for _, e := range events {
switch e {
Expand Down
45 changes: 45 additions & 0 deletions instrumentation/net/http/otelhttp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,48 @@ func TestSpanNameFormatter(t *testing.T) {
})
}
}

func TestEvent(t *testing.T) {
t.Run("constant values", func(t *testing.T) {
assert.Equal(t, otelhttp.ReadEvents, otelhttp.Event(1), "ReadEvents should be 1")
assert.Equal(t, otelhttp.WriteEvents, otelhttp.Event(2), "WriteEvents should be 2")
})

t.Run("unspecifiedEvent", func(t *testing.T) {
var unspecified otelhttp.Event // zero-value
assert.Equal(t, otelhttp.Event(0), unspecified, "unspecifiedEvent should be zero-value Event")

// Validate that unspecifiedEvent is different from defined events
assert.NotEqual(t, otelhttp.ReadEvents, unspecified, "unspecifiedEvent should not equal ReadEvents")
assert.NotEqual(t, otelhttp.WriteEvents, unspecified, "unspecifiedEvent should not equal WriteEvents")

// Validate WithMessageEvents accepts unspecifiedEvent
opt := otelhttp.WithMessageEvents(unspecified)
assert.NotNil(t, opt, "WithMessageEvents(unspecifiedEvent) should not return nil")

// Additional validation: test behavior with unspecified events
optMultiple := otelhttp.WithMessageEvents(unspecified, otelhttp.ReadEvents)
assert.NotNil(t, optMultiple, "WithMessageEvents with unspecified and valid events should not return nil")
})

t.Run("WithMessageEvents", func(t *testing.T) {
tests := []struct {
name string
events []otelhttp.Event
}{
{name: "ReadEvents", events: []otelhttp.Event{otelhttp.ReadEvents}},
{name: "WriteEvents", events: []otelhttp.Event{otelhttp.WriteEvents}},
{name: "multiple events", events: []otelhttp.Event{otelhttp.ReadEvents, otelhttp.WriteEvents}},
{name: "no events", events: []otelhttp.Event{}},
{name: "unspecified event only", events: []otelhttp.Event{otelhttp.Event(0)}},
{name: "mixed with unspecified", events: []otelhttp.Event{otelhttp.Event(0), otelhttp.ReadEvents}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := otelhttp.WithMessageEvents(tt.events...)
assert.NotNil(t, got, "WithMessageEvents(%v) should not return nil", tt.events)
})
}
})
}