Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
25 changes: 24 additions & 1 deletion extension/memorylimiterextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ The memory limiter extension is used to prevent out of memory situations on
the collector. The extension will potentially replace the Memory Limiter Processor.
It provides better guarantees from running out of memory as it will be used by the
receivers to reject requests before converting them into OTLP. All the configurations
are the same as Memory Limiter Processor. The extension is under development and does nothing.
are the same as Memory Limiter Processor.


This extension can be used as an extension for all HTTP and gRPC receivers that
are configured through the standard `confighttp` and `configgrpc` libraries. For
example, to configure this extension in the OTLP receiver:

```
receivers:
otlp:
protocols:
grpc:
middlewares:
- id: memory_limiter
http:
middlewares:
- id: memory_limiter

extensions:
memory_limiter:
check_interval: 1s
limit_percentage: 1
spike_limit_percentage: 0.05
```

see [memorylimiterprocessor](../../processor/memorylimiterprocessor/README.md) for additional details
25 changes: 25 additions & 0 deletions extension/memorylimiterextension/memorylimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

import (
"context"
"net/http"

"go.uber.org/zap"
"google.golang.org/grpc"

Check failure on line 11 in extension/memorylimiterextension/memorylimiter.go

View workflow job for this annotation

GitHub Actions / govulncheck

could not import google.golang.org/grpc (invalid package name: "")
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/internal/memorylimiter"
Expand Down Expand Up @@ -38,3 +42,24 @@
func (ml *memoryLimiterExtension) MustRefuse() bool {
return ml.memLimiter.MustRefuse()
}

func (ml *memoryLimiterExtension) GetHTTPHandler(base http.Handler) (http.Handler, error) {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if ml.MustRefuse() {
http.Error(resp, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
return
}
base.ServeHTTP(resp, req)
}), nil
}

func (ml *memoryLimiterExtension) GetGRPCServerOptions() ([]grpc.ServerOption, error) {
return []grpc.ServerOption{grpc.UnaryInterceptor(
func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
if ml.MustRefuse() {
return nil, status.Errorf(codes.ResourceExhausted, "RESOURCE_EXHAUSTED")
}
return handler(ctx, req)
},
)}, nil
}
6 changes: 6 additions & 0 deletions extension/memorylimiterextension/memorylimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/extension/extensionmiddleware"
"go.opentelemetry.io/collector/internal/memorylimiter"
"go.opentelemetry.io/collector/internal/memorylimiter/iruntime"
)
Expand Down Expand Up @@ -95,3 +96,8 @@ func TestMemoryPressureResponse(t *testing.T) {
})
}
}

func TestMiddleware(t *testing.T) {
Copy link
Contributor

@evan-bradley evan-bradley Nov 20, 2025

Choose a reason for hiding this comment

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

Instead of having a test for this, could we use the type checker to assert interface implementation?

You could put the following above line 19 in memorylimiter.go for example:

var _ extensionmiddleware.GRPCServer = (*memoryLimiterExtension)(nil)
var _ extensionmiddleware.HTTPServer = (*memoryLimiterExtension)(nil)

var _ extensionmiddleware.HTTPServer = memoryLimiterExtension{}
var _ extensionmiddleware.GRPCServer = memoryLimiterExtension{}
}
Loading