Skip to content

Commit

Permalink
docs: Adds docs about using tracing in non ASP.NET Core applications
Browse files Browse the repository at this point in the history
  • Loading branch information
amanda-tarafa committed May 26, 2021
1 parent ad42b2e commit a60de00
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<ProjectReference Include="..\..\Google.Cloud.Diagnostics.Common\Google.Cloud.Diagnostics.Common.Tests\Google.Cloud.Diagnostics.Common.Tests.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Moq" Version="4.12.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<ProjectReference Include="..\..\Google.Cloud.Diagnostics.Common\Google.Cloud.Diagnostics.Common.Tests\Google.Cloud.Diagnostics.Common.Tests.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Moq" Version="4.12.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2021 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.ClientTesting;
using Google.Cloud.Diagnostics.Common;
using Google.Cloud.Diagnostics.Common.IntegrationTests;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;
using Xunit;

namespace Google.Cloud.Diagnostics.AspNetCore.Snippets
{
public class StandaloneTraceSnippets
{
private static readonly string ProjectId = TestEnvironment.GetTestProjectId();

private static readonly TraceEntryPolling s_polling = new TraceEntryPolling();

private readonly string _testId;

private readonly Timestamp _startTime;

public StandaloneTraceSnippets()
{
_testId = IdGenerator.FromDateTime();
_startTime = Timestamp.FromDateTime(DateTime.UtcNow);

// The rate limiter instance is static and only set once. If we do not reset it at the
// beginning of each tests the qps will not change. This is dependent on the tests not
// running in parallel.
RateLimiter.Reset();
}

// Sample: Configure
public static IHostBuilder CreateHostBuilder() =>
Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddGoogleTrace(options => options.ProjectId = ProjectId);
// Register other services here if you need them.
});
// End sample

[Fact]
public async Task TraceAsync()
{
// Naming it like an instance variable so that it looks like that on sample code.
IHost _host = null;

try
{
// Sample: Start
_host = CreateHostBuilder().Build();
await _host.StartAsync();
// End sample

// Sample: IncomingContext
ITraceContext traceContext = GetTraceContextFromIncomingRequest();
var tracerFactory = _host.Services.GetRequiredService<Func<ITraceContext, IManagedTracer>>();
IManagedTracer tracer = tracerFactory(traceContext);
ContextTracerManager.SetCurrentTracer(tracer);
// End sample

// Let's just start a span with the test ID so we can find it faster.
// But we don't show this in sample code.
using (tracer.StartSpan(_testId))
{
// Sample: Trace
IManagedTracer currentTracer = _host.Services.GetRequiredService<IManagedTracer>();
using (currentTracer.StartSpan("standalone_tracing"))
{
Console.WriteLine("Using Cloud Trace from a non ASP.NET Core app");
}
// End sample
}

var trace = s_polling.GetTrace(_testId, _startTime);
TraceEntryVerifiers.AssertParentChildSpan(trace, _testId, "standalone_tracing");
Assert.Equal(traceContext.TraceId, trace.TraceId);
}
finally
{
if (_host != null)
{
await _host.StopAsync();
}
}
}

private static ITraceContext GetTraceContextFromIncomingRequest() =>
new SimpleTraceContext(TraceIdFactory.Create().NextId(), null, true);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\Google.Cloud.Diagnostics.Common\Google.Cloud.Diagnostics.Common.Tests\Google.Cloud.Diagnostics.Common.Tests.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Moq" Version="4.12.0" />
Expand Down
31 changes: 31 additions & 0 deletions apis/Google.Cloud.Diagnostics.AspNetCore/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,34 @@ that is propagated in a `custom_trace_id` header. This is of no use in the real

{{sample:Trace.CustomTraceContext}}

## Using Google Trace in other than ASP.NET Core applications

If you want to use Google Cloud Trace in applications that are not ASP.NET Core you may use the
`Google.Cloud.Diagnostics.Common` package directly and .NET's dependency injection mechanism.
You can read more about .NET dependency injection in non ASP.NET Core apps
[here](https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-usage).

Note that this is useful for both installed applications and services that process incoming messages
other than HTTP requests, for instance, a service that it's subscribed to Pub/Sub topics.

Following you'll see a very simplified example of how you could set up Google Cloud Trace for these
types of applications.

- Configure Google Cloud Trace. You can set tracing options the same as you would do for ASP.NET Core apps.

{{sample:StandaloneTrace.Configure}}

- Build and start a `Microsoft.Extensions.Hosting.IHost`.

{{sample:StandaloneTrace.Start}}

- Create a tracing context when appropiate, for instance, when you receive a Pub/Sub message. You can create
an empty tracing context (with all null values) if there's none. The tracer will create a tracing context
depending on configuration options like QPS, etc.

{{sample:StandaloneTrace.IncomingContext}}

- Trace normally in your code

{{sample:StandaloneTrace.Trace}}

3 changes: 2 additions & 1 deletion apis/apis.json
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,8 @@
"Google.Cloud.Diagnostics.Common.IntegrationTests": "project",
"Google.Cloud.Diagnostics.Common.Tests": "project",
"Microsoft.AspNetCore.Mvc": "2.1.3",
"Microsoft.AspNetCore.TestHost": "2.1.1"
"Microsoft.AspNetCore.TestHost": "2.1.1",
"Microsoft.Extensions.Hosting": "3.0.0"
},
"additionalAnalyzerTestDependencies": {
"Microsoft.AspNetCore.Mvc.Core": "1.0.4",
Expand Down

0 comments on commit a60de00

Please sign in to comment.