|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using Google.Protobuf; |
| 5 | +using Google.Protobuf.WellKnownTypes; |
| 6 | +using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient.Grpc; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient.Tests; |
| 10 | + |
| 11 | +#pragma warning disable CA1515 // Consider making public types internal |
| 12 | +public class GrpcRetryTestCase |
| 13 | +#pragma warning restore CA1515 // Consider making public types internal |
| 14 | +{ |
| 15 | + private readonly string testRunnerName; |
| 16 | + |
| 17 | + private GrpcRetryTestCase(string testRunnerName, GrpcRetryAttempt[] retryAttempts, int expectedRetryAttempts = 1) |
| 18 | + { |
| 19 | + this.ExpectedRetryAttempts = expectedRetryAttempts; |
| 20 | + this.RetryAttempts = retryAttempts; |
| 21 | + this.testRunnerName = testRunnerName; |
| 22 | + } |
| 23 | + |
| 24 | + public int ExpectedRetryAttempts { get; } |
| 25 | + |
| 26 | + internal GrpcRetryAttempt[] RetryAttempts { get; } |
| 27 | + |
| 28 | + public static TheoryData<GrpcRetryTestCase> GetGrpcTestCases() |
| 29 | + { |
| 30 | + return |
| 31 | + [ |
| 32 | + new("Cancelled", [new(StatusCode.Cancelled)]), |
| 33 | + new("DeadlineExceeded", [new(StatusCode.DeadlineExceeded)]), |
| 34 | + new("Aborted", [new(StatusCode.Aborted)]), |
| 35 | + new("OutOfRange", [new(StatusCode.OutOfRange)]), |
| 36 | + new("DataLoss", [new(StatusCode.DataLoss)]), |
| 37 | + new("Unavailable", [new(StatusCode.Unavailable)]), |
| 38 | + |
| 39 | + new("OK", [new(StatusCode.OK, expectedSuccess: false)]), |
| 40 | + new("PermissionDenied", [new(StatusCode.PermissionDenied, expectedSuccess: false)]), |
| 41 | + new("Unknown", [new(StatusCode.Unknown, expectedSuccess: false)]), |
| 42 | + |
| 43 | + new("ResourceExhausted w/o RetryInfo", [new(StatusCode.ResourceExhausted, expectedSuccess: false)]), |
| 44 | + new("ResourceExhausted w/ RetryInfo", [new(StatusCode.ResourceExhausted, throttleDelay: GetThrottleDelayString(new Duration { Seconds = 2 }), expectedNextRetryDelayMilliseconds: 3000)]), |
| 45 | + |
| 46 | + new("Unavailable w/ RetryInfo", [new(StatusCode.Unavailable, throttleDelay: GetThrottleDelayString(Duration.FromTimeSpan(TimeSpan.FromMilliseconds(2000))), expectedNextRetryDelayMilliseconds: 3000)]), |
| 47 | + |
| 48 | + new("Expired deadline", [new(StatusCode.Unavailable, deadlineExceeded: true, expectedSuccess: false)]), |
| 49 | + |
| 50 | + new( |
| 51 | + "Exponential backoff", |
| 52 | + [ |
| 53 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 1500), |
| 54 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 2250), |
| 55 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 3375), |
| 56 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 5000), |
| 57 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 5000) |
| 58 | + ], |
| 59 | + expectedRetryAttempts: 5), |
| 60 | + |
| 61 | + new( |
| 62 | + "Retry until non-retryable status code encountered", |
| 63 | + [ |
| 64 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 1500), |
| 65 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 2250), |
| 66 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 3375), |
| 67 | + new(StatusCode.PermissionDenied, expectedSuccess: false), |
| 68 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 5000) |
| 69 | + ], |
| 70 | + expectedRetryAttempts: 4), |
| 71 | + |
| 72 | + // Test throttling affects exponential backoff. |
| 73 | + new( |
| 74 | + "Exponential backoff after throttling", |
| 75 | + [ |
| 76 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 1500), |
| 77 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 2250), |
| 78 | + new(StatusCode.Unavailable, throttleDelay: GetThrottleDelayString(Duration.FromTimeSpan(TimeSpan.FromMilliseconds(500))), expectedNextRetryDelayMilliseconds: 750), |
| 79 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 1125), |
| 80 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 1688), |
| 81 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 2532), |
| 82 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 3798), |
| 83 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 5000), |
| 84 | + new(StatusCode.Unavailable, expectedNextRetryDelayMilliseconds: 5000) |
| 85 | + ], |
| 86 | + expectedRetryAttempts: 9), |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + public override string ToString() |
| 91 | + { |
| 92 | + return this.testRunnerName; |
| 93 | + } |
| 94 | + |
| 95 | + private static string GetThrottleDelayString(Duration throttleDelay) |
| 96 | + { |
| 97 | + var status = new Google.Rpc.Status |
| 98 | + { |
| 99 | + Code = 4, |
| 100 | + Message = "Only nanos", |
| 101 | + Details = |
| 102 | + { |
| 103 | + Any.Pack(new Google.Rpc.RetryInfo |
| 104 | + { |
| 105 | + RetryDelay = throttleDelay, |
| 106 | + }), |
| 107 | + }, |
| 108 | + }; |
| 109 | + |
| 110 | + return Convert.ToBase64String(status.ToByteArray()); |
| 111 | + } |
| 112 | + |
| 113 | + internal struct GrpcRetryAttempt |
| 114 | + { |
| 115 | + internal GrpcRetryAttempt( |
| 116 | + StatusCode statusCode, |
| 117 | + bool deadlineExceeded = false, |
| 118 | + string? throttleDelay = null, |
| 119 | + int expectedNextRetryDelayMilliseconds = 1500, |
| 120 | + bool expectedSuccess = true) |
| 121 | + { |
| 122 | + var status = new Status(statusCode, "Error"); |
| 123 | + |
| 124 | + // Using arbitrary +1 hr for deadline for test purposes. |
| 125 | + var deadlineUtc = deadlineExceeded ? DateTime.UtcNow.AddSeconds(-1) : DateTime.UtcNow.AddHours(1); |
| 126 | + |
| 127 | + this.ThrottleDelay = throttleDelay; |
| 128 | + |
| 129 | + this.Response = new ExportClientGrpcResponse(expectedSuccess, deadlineUtc, null, status, this.ThrottleDelay); |
| 130 | + |
| 131 | + this.ExpectedNextRetryDelayMilliseconds = expectedNextRetryDelayMilliseconds; |
| 132 | + |
| 133 | + this.ExpectedSuccess = expectedSuccess; |
| 134 | + } |
| 135 | + |
| 136 | + public string? ThrottleDelay { get; } |
| 137 | + |
| 138 | + public int? ExpectedNextRetryDelayMilliseconds { get; } |
| 139 | + |
| 140 | + public bool ExpectedSuccess { get; } |
| 141 | + |
| 142 | + internal ExportClientGrpcResponse Response { get; } |
| 143 | + } |
| 144 | +} |
0 commit comments