-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds GoogleTraceProvider to be used by Logging when Tracing is not co…
…nfigured. Closes #5359.
- Loading branch information
1 parent
924c503
commit 5710321
Showing
4 changed files
with
223 additions
and
4 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
80 changes: 80 additions & 0 deletions
80
....AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Tests/Logging/GoogleTraceProviderTests.cs
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,80 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Cloud.Diagnostics.Common; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
using Moq; | ||
using System; | ||
using Xunit; | ||
|
||
namespace Google.Cloud.Diagnostics.AspNetCore.Tests | ||
{ | ||
public class GoogleTraceProviderTests | ||
{ | ||
[Fact] | ||
public void GetCurrentTraceContext() | ||
{ | ||
string traceId = "105445aa7843bc8bf206b12000100f00"; | ||
ulong spanId = 0x12D687; | ||
// The spanId set on the log entry should confirm to x16 | ||
// format so that the backend can really associate the log entry | ||
// to the span. | ||
string expectedSpanId = "000000000012d687"; | ||
|
||
IServiceProvider serviceProvider = MockServiceProvider(traceId, spanId, true); | ||
|
||
GoogleTraceProvider traceProvider = new GoogleTraceProvider(); | ||
TraceContextForLogEntry traceContext = traceProvider.GetCurrentTraceContext(serviceProvider); | ||
|
||
Assert.Equal(traceId, traceContext.TraceId); | ||
Assert.Equal(expectedSpanId, traceContext.SpanId); | ||
} | ||
|
||
[Fact] | ||
public void GetCurrentTraceContext_ShouldNotTrace() | ||
{ | ||
string traceId = "105445aa7843bc8bf206b12000100f00"; | ||
ulong spanId = 1234567; | ||
|
||
IServiceProvider serviceProvider = MockServiceProvider(traceId, spanId, false); | ||
|
||
GoogleTraceProvider traceProvider = new GoogleTraceProvider(); | ||
Assert.Null(traceProvider.GetCurrentTraceContext(serviceProvider)); | ||
} | ||
|
||
private IServiceProvider MockServiceProvider(string traceId, ulong spanId, bool shouldTrace) | ||
{ | ||
char shouldTraceBit = shouldTrace ? '1' : '0'; | ||
StringValues headerValue = $"{traceId}/{spanId};o={shouldTraceBit}"; | ||
|
||
Mock<IHeaderDictionary> headerDictionaryMock = new Mock<IHeaderDictionary>(MockBehavior.Strict); | ||
headerDictionaryMock.Setup(hd => hd.TryGetValue(TraceHeaderContext.TraceHeader, out headerValue)).Returns(true); | ||
|
||
Mock<HttpRequest> requestMock = new Mock<HttpRequest>(MockBehavior.Strict); | ||
requestMock.Setup(r => r.Headers).Returns(headerDictionaryMock.Object); | ||
|
||
Mock<HttpContext> contextMock = new Mock<HttpContext>(MockBehavior.Strict); | ||
contextMock.Setup(c => c.Request).Returns(requestMock.Object); | ||
|
||
Mock<IHttpContextAccessor> contextAccessorMock = new Mock<IHttpContextAccessor>(MockBehavior.Strict); | ||
contextAccessorMock.Setup(a => a.HttpContext).Returns(contextMock.Object); | ||
|
||
Mock<IServiceProvider> serviceProviderMock = new Mock<IServiceProvider>(MockBehavior.Strict); | ||
serviceProviderMock.Setup(p => p.GetService(typeof(IHttpContextAccessor))).Returns(contextAccessorMock.Object); | ||
|
||
return serviceProviderMock.Object; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore/Logging/GoogleTraceProvider.cs
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,36 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
|
||
namespace Google.Cloud.Diagnostics.AspNetCore | ||
{ | ||
/// <summary> | ||
/// If this is registered as a dependency, then Log Entries will be associated with | ||
/// the Google trace and span. | ||
/// </summary> | ||
/// <remarks> | ||
/// To be used when the Tracing component of the Google.Cloud.Diagnostics libraries | ||
/// is not configured, but Google traces are still being generated, for instance, | ||
/// because the application is being run in Google Cloud. | ||
/// If the Tracing component is configured, log entries are automatically associated | ||
/// to Google traces and spans. | ||
/// </remarks> | ||
public class GoogleTraceProvider : IExternalTraceProvider | ||
{ | ||
/// <inheritdoc/> | ||
public TraceContextForLogEntry GetCurrentTraceContext(IServiceProvider serviceProvider) => | ||
TraceContextForLogEntry.FromGoogleTraceHeader(serviceProvider); | ||
} | ||
} |
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