Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Automatic spans collection for Redis (#93)
Browse files Browse the repository at this point in the history
Automatic spans collection for Redis
  • Loading branch information
SergeyKanzhelev authored Jan 17, 2019
1 parent 46ecfab commit e5f3822
Show file tree
Hide file tree
Showing 42 changed files with 992 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ the release.
and process links, annotations and messages.
- ASP.NET Core collector now uses `http.route` for the span name.
- Initial implementation of Resource Specification.
- Plug in to collect Redis calls made using StackExchange.Redis package.
- Object of type `ISpanData` can be created using only Abstractions package.
- Number of minor APIs adjustments.

## 0.1.0-alpha-33381

Expand Down
12 changes: 12 additions & 0 deletions OpenCensus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Exporter.Stackdr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Exporter.ApplicationInsights.Tests", "test\OpenCensus.Exporter.ApplicationInsights.Tests\OpenCensus.Exporter.ApplicationInsights.Tests.csproj", "{1FA1F509-7722-48E3-9A35-16CBB6774957}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Collector.StackExchangeRedis", "src\OpenCensus.Collector.StackExchangeRedis\OpenCensus.Collector.StackExchangeRedis.csproj", "{6B681D72-D68A-44CC-8C75-53B9A322E6EC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenCensus.Collector.StackExchangeRedis.Tests", "test\OpenCensus.Collector.StackExchangeRedis.Tests\OpenCensus.Collector.StackExchangeRedis.Tests.csproj", "{CA98AF29-0852-4ADD-A66B-7E96266EE7B7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -133,6 +137,14 @@ Global
{1FA1F509-7722-48E3-9A35-16CBB6774957}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FA1F509-7722-48E3-9A35-16CBB6774957}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FA1F509-7722-48E3-9A35-16CBB6774957}.Release|Any CPU.Build.0 = Release|Any CPU
{6B681D72-D68A-44CC-8C75-53B9A322E6EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B681D72-D68A-44CC-8C75-53B9A322E6EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B681D72-D68A-44CC-8C75-53B9A322E6EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B681D72-D68A-44CC-8C75-53B9A322E6EC}.Release|Any CPU.Build.0 = Release|Any CPU
{CA98AF29-0852-4ADD-A66B-7E96266EE7B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CA98AF29-0852-4ADD-A66B-7E96266EE7B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CA98AF29-0852-4ADD-A66B-7E96266EE7B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CA98AF29-0852-4ADD-A66B-7E96266EE7B7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ We encourage contributions. Use tags [up-for-grabs][up-for-grabs-issues] and
| ----------------------- | ---------------- | -----------------|
| Asp.Net Core | [![MyGet Nightly][opencensus-collect-aspnetcore-myget-image]][opencensus-collect-aspnetcore-myget-url] | [![NuGet Release][opencensus-collect-aspnetcore-nuget-image]][opencensus-collect-aspnetcore-nuget-url] |
| .Net Core HttpClient | [![MyGet Nightly][opencensus-collect-deps-myget-image]][opencensus-collect-deps-myget-url] | [![NuGet Release][opencensus-collect-deps-nuget-image]][opencensus-collect-deps-nuget-url] |
| StackExchange.Redis | [![MyGet Nightly][opencensus-collect-stackexchange-redis-myget-image]][opencensus-collect-stackexchange-redis-myget-url] | [![NuGet Release][opencensus-collect-stackexchange-redis-nuget-image]][opencensus-collect-stackexchange-redis-nuget-url]|

### Exporters Packages

Expand Down Expand Up @@ -109,6 +110,41 @@ Outgoing http calls made by .NET Core `HttpClient` can be automatically tracked.
var depCollector = app.ApplicationServices.GetService<DependenciesCollector>();
```

### Using StackExchange.Redis collector

Outgoing http calls to Redis made usign StackExchange.Redis library can be automatically tracked.

1. Install package to your project:
[OpenCensus.Collector.StackExchangeRedis][opencensus-collect-stackexchange-redis-nuget-url]

2. Make sure `ITracer`, `ISampler`, and `IExportComponent` registered in DI.
``` csharp
services.AddSingleton<ITracer>(Tracing.Tracer);
services.AddSingleton<ISampler>(Samplers.AlwaysSample);
services.AddSingleton<IExportComponent>(Tracing.ExportComponent);
```

3. Configure data collection singletons in ConfigureServices method:
``` csharp
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSingleton<StackExchangeRedisCallsCollectorOptions>(new StackExchangeRedisCallsCollectorOptions());
services.AddSingleton<StackExchangeRedisCallsCollector>();
```

4. Initiate data collection by instantiating singleton in Configure method
``` csharp
public void Configure(IApplicationBuilder app, /*... other arguments*/ )
{
// ...
var redisCollector = app.ApplicationServices.GetService<StackExchangeRedisCallsCollector>();

// use collector to configure the profiler
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("localhost:6379");
connection.RegisterProfiler(redisCollector.GetProfilerSessionsFactory());
```

## OpenCensus QuickStart: exporting data

### Using Zipkin exporter
Expand Down Expand Up @@ -236,6 +272,8 @@ deprecate it for 18 months before removing it, if possible.
[opencensus-collect-aspnetcore-myget-url]: https://www.myget.org/feed/opencensus/package/nuget/OpenCensus.Collector.AspNetCore
[opencensus-collect-deps-myget-image]:https://img.shields.io/myget/opencensus/vpre/OpenCensus.Collector.Dependencies.svg
[opencensus-collect-deps-myget-url]: https://www.myget.org/feed/opencensus/package/nuget/OpenCensus.Collector.Dependencies
[opencensus-collect-stackexchange-redis-myget-image]:https://img.shields.io/myget/opencensus/vpre/OpenCensus.Collector.StackExchangeRedis.svg
[opencensus-collect-stackexchange-redis-myget-url]: https://www.myget.org/feed/opencensus/package/nuget/OpenCensus.Collector.StackExchangeRedis
[opencensus-nuget-image]:https://img.shields.io/nuget/vpre/OpenCensus.svg
[opencensus-nuget-url]:https://www.nuget.org/packages/OpenCensus
[opencensus-abs-nuget-image]:https://img.shields.io/nuget/vpre/OpenCensus.Abstractions.svg
Expand All @@ -252,6 +290,8 @@ deprecate it for 18 months before removing it, if possible.
[opencensus-collect-aspnetcore-nuget-url]: https://www.nuget.org/packages/OpenCensus.Collector.AspNetCore
[opencensus-collect-deps-nuget-image]:https://img.shields.io/nuget/vpre/OpenCensus.Collector.Dependencies.svg
[opencensus-collect-deps-nuget-url]: https://www.nuget.org/packages/OpenCensus.Collector.Dependencies
[opencensus-collect-stackexchange-redis-nuget-image]:https://img.shields.io/nuget/vpre/OpenCensus.Collector.StackExchangeRedis.svg
[opencensus-collect-stackexchange-redis-nuget-url]: https://www.nuget.org/packages/OpenCensus.Collector.StackExchangeRedis
[up-for-grabs-issues]: https://github.com/census-instrumentation/opencensus-csharp/issues?q=is%3Aissue+is%3Aopen+label%3Aup-for-grabs
[good-first-issues]: https://github.com/census-instrumentation/opencensus-csharp/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22
[zipkin-get-started]: https://zipkin.io/pages/quickstart.html
Expand Down
2 changes: 0 additions & 2 deletions src/OpenCensus.Abstractions/OpenCensus.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Common\" />
<Folder Include="Internal\" />
<Folder Include="Stats\" />
<Folder Include="Tags\" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Utils\Arrays.cs" />
<Compile Remove="Utils\AttributesWithCapacity.cs" />
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion src/OpenCensus.Abstractions/Tags/TagValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
namespace OpenCensus.Tags
{
using System.Collections.Generic;
using OpenCensus.Utils.Abstractions;
using OpenCensus.Abstractions.Utils;
using OpenCensus.Utils;

/// <summary>
/// Collection of tags.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace OpenCensus.Trace
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using OpenCensus.Utils;
using OpenCensus.Abstractions.Utils;

public sealed class Annotation : IAnnotation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
// </copyright>

namespace OpenCensus.Trace.Internal
namespace OpenCensus.Trace
{
public interface IRandomGenerator
{
Expand Down
5 changes: 5 additions & 0 deletions src/OpenCensus.Abstractions/Trace/ISpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public interface ISpan
/// </summary>
SpanKind? Kind { get; set; }

/// <summary>
/// Gets a value indicating whether this span was already stopped.
/// </summary>
bool HasEnded { get; }

/// <summary>
/// Puts a new attribute to the span.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
namespace OpenCensus.Trace
{
using System;
using OpenCensus.Trace.Internal;
using OpenCensus.Utils;

public sealed class SpanId : ISpanId
{
public const int Size = 8;
public static readonly SpanId Invalid = new SpanId(new byte[Size]);

private static readonly SpanId InvalidSpanId = new SpanId(new byte[Size]);

private readonly byte[] bytes;

Expand All @@ -32,6 +32,14 @@ private SpanId(byte[] bytes)
this.bytes = bytes;
}

public static ISpanId Invalid
{
get
{
return InvalidSpanId;
}
}

public byte[] Bytes
{
get
Expand All @@ -44,7 +52,7 @@ public byte[] Bytes

public bool IsValid
{
get { return !Arrays.Equals(this.bytes, Invalid.bytes); }
get { return !Arrays.Equals(this.bytes, InvalidSpanId.bytes); }
}

public static ISpanId FromBytes(byte[] buffer)
Expand Down Expand Up @@ -89,7 +97,7 @@ public static ISpanId GenerateRandomId(IRandomGenerator random)
{
random.NextBytes(bytes);
}
while (Arrays.Equals(bytes, Invalid.bytes));
while (Arrays.Equals(bytes, InvalidSpanId.bytes));
return new SpanId(bytes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
namespace OpenCensus.Trace
{
using System;
using OpenCensus.Trace.Internal;
using OpenCensus.Utils;

public sealed class TraceId : ITraceId
{
public const int Size = 16;
public static readonly TraceId Invalid = new TraceId(new byte[Size]);
private static readonly TraceId InvalidTraceId = new TraceId(new byte[Size]);

private readonly byte[] bytes;

Expand All @@ -32,6 +31,14 @@ private TraceId(byte[] bytes)
this.bytes = bytes;
}

public static ITraceId Invalid
{
get
{
return InvalidTraceId;
}
}

public byte[] Bytes
{
get
Expand All @@ -46,7 +53,7 @@ public bool IsValid
{
get
{
return !Arrays.Equals(this.bytes, Invalid.bytes);
return !Arrays.Equals(this.bytes, InvalidTraceId.bytes);
}
}

Expand Down Expand Up @@ -114,7 +121,7 @@ public static ITraceId GenerateRandomId(IRandomGenerator random)
{
random.NextBytes(bytes);
}
while (Arrays.Equals(bytes, Invalid.bytes));
while (Arrays.Equals(bytes, InvalidTraceId.bytes));
return new TraceId(bytes);
}

Expand Down
Loading

0 comments on commit e5f3822

Please sign in to comment.