-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[otelarrowreceiver, otelarrowexporter] Add internal/grpcutil package (#…
…33688) **Description:** With utilities to encode and decode timeout in the syntax specified by gRPC. This will be used by the otelarrow components to encode and decode timeout values. In that sense, the new code could also be added into the pending internal/otelarrow package, see #33579 As this code is derived from gRPC-Go, some text is added in `NOTICE` according to the license. The code that this was derived from contained a TODO, which was largely addressed by eliminating very short timeouts from being encoded. This code will encode timeouts in milliseconds, seconds, hours, and days but it will not encode microsecond/nanosecond durations, these are rounded to `0m`. The decoder will handle all durations that gRPC specifies, so that this logic can change in the future. **Link to tracking Issue:** open-telemetry/otel-arrow#227 **Testing:** Adds basic tests.
- Loading branch information
Showing
20 changed files
with
255 additions
and
13 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,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: internal/grpcutil | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add internal/grpcutil package with gRPC-specified timeout parsing | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33688] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# 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] |
Validating CODEOWNERS rules …
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
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
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
6 changes: 3 additions & 3 deletions
6
exporter/otelarrowexporter/internal/metadata/generated_status.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
include ../../Makefile.Common |
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,11 @@ | ||
module github.com/open-telemetry/opentelemetry-collector-contrib/internal/grpcutil | ||
|
||
go 1.21.0 | ||
|
||
require github.com/stretchr/testify v1.9.0 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
status: | ||
codeowners: | ||
active: [jmacd, moh-osman3, lquerel] |
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,114 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// The EncodeTimeout function is forked and modified from the original | ||
// https://github.com/grpc/grpc-go/blob/master/internal/grpcutil/encode_duration.go | ||
|
||
// This DecodeTimeout function is forked and modified from the original | ||
// https://github.com/grpc/grpc-go/blob/master/internal/transport/http_util.go | ||
|
||
package grpcutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/grpcutil" | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const maxTimeoutValue int64 = 100000000 - 1 | ||
|
||
// div does integer division and round-up the result. Note that this is | ||
// equivalent to (d+r-1)/r but has less chance to overflow. | ||
func div(d, r time.Duration) int64 { | ||
if d%r > 0 { | ||
return int64(d/r + 1) | ||
} | ||
return int64(d / r) | ||
} | ||
|
||
type timeoutUnit uint8 | ||
|
||
const ( | ||
hour timeoutUnit = 'H' | ||
minute timeoutUnit = 'M' | ||
second timeoutUnit = 'S' | ||
millisecond timeoutUnit = 'm' | ||
microsecond timeoutUnit = 'u' | ||
nanosecond timeoutUnit = 'n' | ||
) | ||
|
||
// EncodeTimeout encodes the duration to the format grpc-timeout | ||
// header accepts. This is copied from the gRPC-Go implementation, | ||
// with two branches of the original six branches removed, leaving the | ||
// four you see for milliseconds, seconds, minutes, and hours. This | ||
// code will not encode timeouts less than one millisecond. See: | ||
// | ||
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests | ||
func EncodeTimeout(t time.Duration) string { | ||
if t < time.Millisecond { | ||
return "0m" | ||
} | ||
if d := div(t, time.Millisecond); d <= maxTimeoutValue { | ||
return fmt.Sprintf("%d%c", d, millisecond) | ||
} | ||
if d := div(t, time.Second); d <= maxTimeoutValue { | ||
return fmt.Sprintf("%d%c", d, second) | ||
} | ||
if d := div(t, time.Minute); d <= maxTimeoutValue { | ||
return fmt.Sprintf("%d%c", d, minute) | ||
} | ||
// Note that maxTimeoutValue * time.Hour > MaxInt64. | ||
return fmt.Sprintf("%d%c", div(t, time.Hour), hour) | ||
} | ||
|
||
func timeoutUnitToDuration(u timeoutUnit) (d time.Duration, ok bool) { | ||
switch u { | ||
case hour: | ||
return time.Hour, true | ||
case minute: | ||
return time.Minute, true | ||
case second: | ||
return time.Second, true | ||
case millisecond: | ||
return time.Millisecond, true | ||
case microsecond: | ||
return time.Microsecond, true | ||
case nanosecond: | ||
return time.Nanosecond, true | ||
default: | ||
} | ||
return | ||
} | ||
|
||
// DecodeTimeout parses a string associated with the "grpc-timeout" | ||
// header. Note this will accept all valid gRPC units including | ||
// microseconds and nanoseconds, which EncodeTimeout avoids. This is | ||
// specified in: | ||
// | ||
// https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#requests | ||
func DecodeTimeout(s string) (time.Duration, error) { | ||
size := len(s) | ||
if size < 2 { | ||
return 0, fmt.Errorf("transport: timeout string is too short: %q", s) | ||
} | ||
if size > 9 { | ||
// Spec allows for 8 digits plus the unit. | ||
return 0, fmt.Errorf("transport: timeout string is too long: %q", s) | ||
} | ||
unit := timeoutUnit(s[size-1]) | ||
d, ok := timeoutUnitToDuration(unit) | ||
if !ok { | ||
return 0, fmt.Errorf("transport: timeout unit is not recognized: %q", s) | ||
} | ||
t, err := strconv.ParseInt(s[:size-1], 10, 64) | ||
if err != nil { | ||
return 0, err | ||
} | ||
const maxHours = math.MaxInt64 / int64(time.Hour) | ||
if d == time.Hour && t > maxHours { | ||
// This timeout would overflow math.MaxInt64; clamp it. | ||
return time.Duration(math.MaxInt64), nil | ||
} | ||
return d * time.Duration(t), nil | ||
} |
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,54 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package grpcutil | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTimeoutEncode(t *testing.T) { | ||
// Note the gRPC specification limits durations to 8 digits, | ||
// so the use of 123456789 as a multiplier below forces the | ||
// next-larger unit to be used. | ||
require.Equal(t, "0m", EncodeTimeout(-time.Second)) | ||
require.Equal(t, "1000m", EncodeTimeout(time.Second)) | ||
require.Equal(t, "123m", EncodeTimeout(123*time.Millisecond)) | ||
require.Equal(t, "123457S", EncodeTimeout(123456789*time.Millisecond)) | ||
require.Equal(t, "2057614M", EncodeTimeout(123456789*time.Second)) | ||
require.Equal(t, "2057614H", EncodeTimeout(123456789*time.Minute)) | ||
} | ||
|
||
func mustDecode(t *testing.T, s string) time.Duration { | ||
d, err := DecodeTimeout(s) | ||
require.NoError(t, err, "must parse a timeout") | ||
return d | ||
} | ||
|
||
func TestTimeoutDecode(t *testing.T) { | ||
// Note the gRPC specification limits durations to 8 digits, | ||
// so the use of 123456789 as a multiplier below forces the | ||
// next-larger unit to be used. | ||
require.Equal(t, time.Duration(0), mustDecode(t, "0m")) | ||
require.Equal(t, time.Second, mustDecode(t, "1000m")) | ||
require.Equal(t, 123*time.Millisecond, mustDecode(t, "123m")) | ||
require.Equal(t, 123*time.Second, mustDecode(t, "123S")) | ||
require.Equal(t, 123*time.Minute, mustDecode(t, "123M")) | ||
require.Equal(t, 123*time.Hour, mustDecode(t, "123H")) | ||
|
||
// these are not encoded by EncodeTimeout, but will be decoded | ||
require.Equal(t, 123*time.Microsecond, mustDecode(t, "123u")) | ||
require.Equal(t, 123*time.Nanosecond, mustDecode(t, "123n")) | ||
|
||
// error cases | ||
testError := func(s string) { | ||
_, err := DecodeTimeout(s) | ||
require.Error(t, err) | ||
} | ||
testError("123x") | ||
testError("x") | ||
testError("") | ||
} |
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
6 changes: 3 additions & 3 deletions
6
receiver/otelarrowreceiver/internal/metadata/generated_status.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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