-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix OTLP http receiver to correctly set Retry-After
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
- Loading branch information
1 parent
a4ae175
commit b3dd063
Showing
7 changed files
with
192 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: bug_fix | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: otlpreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Fix OTLP http receiver to correctly set Retry-After | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [12367] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api, user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
internal/httphelper/helper_test.go → internal/statusutil/helper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otlpreceiver | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
spb "google.golang.org/genproto/googleapis/rpc/status" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/types/known/durationpb" | ||
|
||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/internal/testutil" | ||
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" | ||
) | ||
|
||
func TestHttpRetryAfter(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
contentType string | ||
err error | ||
expectedStatusCode int | ||
expectedHasRetryAfter bool | ||
expectedRetryAfter string | ||
}{ | ||
{ | ||
name: "StatusErrorRetryableNoRetryAfter", | ||
err: status.New(codes.DeadlineExceeded, "").Err(), | ||
expectedStatusCode: http.StatusServiceUnavailable, | ||
}, | ||
{ | ||
name: "StatusErrorRetryableRetryAfter", | ||
err: func() error { | ||
st := status.New(codes.ResourceExhausted, "") | ||
dt, err := st.WithDetails(&errdetails.RetryInfo{ | ||
RetryDelay: durationpb.New(13 * time.Second), | ||
}) | ||
require.NoError(t, err) | ||
return dt.Err() | ||
}(), | ||
expectedStatusCode: http.StatusTooManyRequests, | ||
expectedHasRetryAfter: true, | ||
expectedRetryAfter: "13", | ||
}, | ||
{ | ||
name: "StatusErrorNotRetryableRetryAfter", | ||
err: func() error { | ||
st := status.New(codes.Unknown, "") | ||
dt, err := st.WithDetails(&errdetails.RetryInfo{ | ||
RetryDelay: durationpb.New(12 * time.Second), | ||
}) | ||
require.NoError(t, err) | ||
return dt.Err() | ||
}(), | ||
expectedStatusCode: http.StatusInternalServerError, | ||
}, | ||
{ | ||
name: "StatusErrorNotRetryableNoRetryAfter", | ||
err: status.New(codes.InvalidArgument, "").Err(), | ||
expectedStatusCode: http.StatusBadRequest, | ||
}, | ||
} | ||
addr := testutil.GetAvailableLocalAddress(t) | ||
|
||
// Set the buffer count to 1 to make it flush the test span immediately. | ||
sink := newErrOrSinkConsumer() | ||
recv := newHTTPReceiver(t, componenttest.NewNopTelemetrySettings(), addr, sink) | ||
|
||
require.NoError(t, recv.Start(context.Background(), componenttest.NewNopHost()), "Failed to start trace receiver") | ||
t.Cleanup(func() { require.NoError(t, recv.Shutdown(context.Background())) }) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
sink.Reset() | ||
sink.SetConsumeError(tt.err) | ||
|
||
for _, dr := range generateDataRequests(t) { | ||
url := "http://" + addr + dr.path | ||
req := createHTTPRequest(t, url, "", "application/x-protobuf", dr.protoBytes) | ||
resp, err := http.DefaultClient.Do(req) | ||
require.NoError(t, err) | ||
|
||
respBytes, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, resp.Body.Close()) | ||
// For cases like "application/json; charset=utf-8", the response will be only "application/json" | ||
require.True(t, strings.HasPrefix(strings.ToLower("application/x-protobuf"), resp.Header.Get("Content-Type"))) | ||
if tt.expectedHasRetryAfter { | ||
require.Equal(t, tt.expectedRetryAfter, resp.Header.Get("Retry-After")) | ||
} else { | ||
require.Len(t, resp.Header.Get("Retry-After"), 0) | ||
} | ||
|
||
assert.Equal(t, tt.expectedStatusCode, resp.StatusCode) | ||
|
||
if tt.err == nil { | ||
tr := ptraceotlp.NewExportResponse() | ||
require.NoError(t, tr.UnmarshalProto(respBytes)) | ||
sink.checkData(t, dr.data, 1) | ||
} else { | ||
errStatus := &spb.Status{} | ||
require.NoError(t, proto.Unmarshal(respBytes, errStatus)) | ||
s, ok := status.FromError(tt.err) | ||
require.True(t, ok) | ||
assert.True(t, proto.Equal(errStatus, s.Proto())) | ||
} | ||
} | ||
}) | ||
} | ||
} |