Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit d5c3963

Browse files
committed
fix: Adds AddGoogleDiagnostics empty overload.
To avoid ambiguity, which results in a compiler error, when not specifying any of the optional parameters for the other overloads. This is the same as what was done on #7642 for the ASP.NET Core packages, but for the Common package instead, which had the same issue.
1 parent ba21870 commit d5c3963

4 files changed

Lines changed: 43 additions & 15 deletions

File tree

apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.IntegrationTests/DiagnosticsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public async Task UseGoogleDiagnostics_ConfiguresServices_Default()
6565
using var client = server.CreateClient();
6666
await TestTrace(testId, startTime, client);
6767
await TestLogging(testId, startTime, client);
68-
await TestErrorReporting(testId, client);
68+
await TestErrorReporting(testId, client, verifyServiceAndVersion: false);
6969
}
7070

7171
[Fact]
@@ -182,7 +182,7 @@ private static async Task TestTrace(string testId, DateTimeOffset startTime, Htt
182182
Assert.False(response.Headers.Contains(TraceHeaderContext.TraceHeader));
183183
}
184184

185-
private static async Task TestErrorReporting(string testId, HttpClient client)
185+
private static async Task TestErrorReporting(string testId, HttpClient client, bool verifyServiceAndVersion = true)
186186
{
187187
await Assert.ThrowsAsync<Exception>(() => client.GetAsync($"/ErrorReporting/{nameof(ErrorReportingController.ThrowsException)}/{testId}"));
188188
var errorEvent = ErrorEventEntryVerifiers.VerifySingle(ErrorEventEntryPolling.Default, testId);

apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.IntegrationTests/DiagnosticsTests.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using Google.Api.Gax;
1516
using Google.Cloud.ClientTesting;
1617
using Google.Cloud.Logging.Type;
1718
using Microsoft.Extensions.DependencyInjection;
@@ -40,15 +41,20 @@ public DiagnosticsTests()
4041
_startTime = DateTimeOffset.UtcNow;
4142
}
4243

44+
private static class EmptyHostBuilder
45+
{
46+
public static IHostBuilder CreateHostBuilder() =>
47+
new HostBuilder()
48+
.ConfigureLogging(builder => builder.ClearProviders())
49+
.ConfigureServices(services => services.AddGoogleDiagnostics());
50+
}
51+
4352
private static class DefaultHostBuilder
4453
{
4554
public static IHostBuilder CreateHostBuilder() =>
4655
new HostBuilder()
4756
.ConfigureLogging(builder => builder.ClearProviders())
48-
.ConfigureServices(services =>
49-
{
50-
services.AddGoogleDiagnostics(ProjectId, Service, Version);
51-
});
57+
.ConfigureServices(services => services.AddGoogleDiagnostics(ProjectId, Service, Version));
5258
}
5359

5460
private static class WithOptionsHostBuilder
@@ -57,14 +63,12 @@ public static IHostBuilder CreateHostBuilder() =>
5763
new HostBuilder()
5864
.ConfigureLogging(builder => builder.ClearProviders())
5965
.ConfigureServices(services =>
60-
{
6166
// We don't care much about which options we are setting.
6267
// These are for testing that all the extension method overloads work as expected.
6368
services.AddGoogleDiagnostics(ProjectId, Service, Version,
6469
TraceOptions.Create(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate)),
6570
LoggingOptions.Create(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate)),
66-
ErrorReportingOptions.CreateInstance(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate)));
67-
});
71+
ErrorReportingOptions.CreateInstance(retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate))));
6872
}
6973

7074
private static class WithServiceOptionsHostBuilder
@@ -106,6 +110,11 @@ public static IEnumerable<object[]> HostBuilderData
106110
yield return new object[] { (Func<IHostBuilder>)DefaultHostBuilder.CreateHostBuilder };
107111
yield return new object[] { (Func<IHostBuilder>)WithOptionsHostBuilder.CreateHostBuilder };
108112
yield return new object[] { (Func<IHostBuilder>)WithServiceOptionsHostBuilder.CreateHostBuilder };
113+
// Skip these tests if we are not running on GCP
114+
if (Platform.Instance().Type != PlatformType.Unknown)
115+
{
116+
yield return new object[] { (Func<IHostBuilder>)EmptyHostBuilder.CreateHostBuilder };
117+
}
109118
}
110119
}
111120

@@ -193,7 +202,7 @@ public async Task LogsExceptionAsync(Func<IHostBuilder> createHostBuilder)
193202

194203
var errorEvent = ErrorEventEntryVerifiers.VerifySingle(ErrorEventEntryPolling.Default, _testId);
195204
ErrorEventEntryVerifiers.VerifyFullErrorEventLogged(
196-
errorEvent, _testId, nameof(ThrowsException), verifyHttpContext: false);
205+
errorEvent, _testId, nameof(ThrowsException), verifyHttpContext: false, verifyServiceAndVersion: false);
197206
}
198207
finally
199208
{

apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.IntegrationTests/ErrorReporting/ErrorEventEntryVerifiers.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ public static IEnumerable<ErrorEvent> VerifyMany(ErrorEventEntryPolling polling,
5050
/// Checks that an <see cref="ErrorEvent"/> contains valid data,
5151
/// including HTTP Context data.
5252
/// </summary>
53-
public static void VerifyFullErrorEventLogged(ErrorEvent errorEvent, string testId, string functionName, bool verifyHttpContext = true)
53+
public static void VerifyFullErrorEventLogged(ErrorEvent errorEvent, string testId, string functionName,
54+
bool verifyHttpContext = true, bool verifyServiceAndVersion = true)
5455
{
55-
VerifyErrorEventLogged(errorEvent, testId, functionName);
56+
VerifyErrorEventLogged(errorEvent, testId, functionName, verifyServiceAndVersion);
5657
if (verifyHttpContext)
5758
{
5859
VerifyHttpContextLogged(errorEvent);
@@ -66,10 +67,13 @@ public static void VerifyFullErrorEventLogged(ErrorEvent errorEvent, string test
6667
/// Google.Cloud.Diagnostics.GoogleExceptionLogger is used instead of the
6768
/// Google.Cloud.Diagnostics.GoogleWebApiExceptionLogger.
6869
/// </summary>
69-
public static void VerifyErrorEventLogged(ErrorEvent errorEvent, string testId, string functionName)
70+
public static void VerifyErrorEventLogged(ErrorEvent errorEvent, string testId, string functionName, bool verifyServiceAndVersion = true)
7071
{
71-
Assert.Equal(EntryData.Service, errorEvent.ServiceContext.Service);
72-
Assert.Equal(EntryData.Version, errorEvent.ServiceContext.Version);
72+
if (verifyServiceAndVersion)
73+
{
74+
Assert.Equal(EntryData.Service, errorEvent.ServiceContext.Service);
75+
Assert.Equal(EntryData.Version, errorEvent.ServiceContext.Version);
76+
}
7377

7478
Assert.Contains(functionName, errorEvent.Message);
7579
Assert.Contains(testId, errorEvent.Message);

apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common/GoogleDiagnosticsExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ namespace Google.Cloud.Diagnostics.Common
2121
/// </summary>
2222
public static class GoogleDiagnosticsExtensions
2323
{
24+
/// <summary>
25+
/// Configures Google Diagnostics to be used in non ASP.NET Core applications.
26+
/// </summary>
27+
/// <remarks>
28+
/// Note that the Google Cloud Project ID to use is required and it can only be
29+
/// obtained from the environment if running on GCP. This means that this overload
30+
/// can only be used when running on GCP. If you are not running on GCP or need to specify
31+
/// the Google Cloud Project ID, you can use any of
32+
/// <see cref="AddGoogleDiagnostics(IServiceCollection, TraceServiceOptions, LoggingServiceOptions, ErrorReportingServiceOptions)"/>
33+
/// or
34+
/// <see cref="AddGoogleDiagnostics(IServiceCollection, string, string, string, TraceOptions, LoggingOptions, ErrorReportingOptions)"/>.
35+
/// </remarks>
36+
public static IServiceCollection AddGoogleDiagnostics(this IServiceCollection services) =>
37+
services.AddGoogleDiagnostics((TraceServiceOptions)null);
38+
2439
/// <summary>
2540
/// Configures Google Diagnostics to be used in non ASP.NET Core applications.
2641
/// </summary>

0 commit comments

Comments
 (0)