Skip to content
This repository has been archived by the owner on Apr 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #103 from mdsol/develop
Browse files Browse the repository at this point in the history
Release of 3.2.0
  • Loading branch information
Herry Kurniawan authored Nov 21, 2016
2 parents 6283a4b + 06e0ca8 commit 4d052c7
Show file tree
Hide file tree
Showing 23 changed files with 1,083 additions and 251 deletions.
Binary file modified .nuget/NuGet.exe
Binary file not shown.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ service/environment.
- `NotToBeDisplayedDomainList`(optional) - It will be used when logging host name by excluding these strings in service name attribute
e.g. domain: ".xyz.com", host: "abc.xyz.com" will be logged as "abc" only
- `ExcludedPathList`(optional) - Path list that is not needed for tracing. Each item must start with "/".
- `Create128BitTraceId` - Create new traces using 128 bit (32 hex character) traceId


```csharp
Expand Down Expand Up @@ -166,3 +167,5 @@ ZipkinTracer is (c) Medidata Solutions Worldwide and owned by its major contribu
* [Kenya Matsumoto](https://github.com/kenyamat)
* [Brent Villanueva](https://github.com/bvillanueva-mdsol)
* [Laszlo Schreck](https://github.com/lschreck-mdsol)
* [Jordi Carres](https://github.com/jcarres-mdsol)
* [Herry Kurniawan](https://github.com/hkurniawan-mdsol)
394 changes: 329 additions & 65 deletions src/Medidata.ZipkinTracer.Core/App_Packages/LibLog.4.2/LibLog.cs

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions src/Medidata.ZipkinTracer.Core/Helpers/ParserHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Globalization;

namespace Medidata.ZipkinTracer.Core.Helpers
{
internal static class ParserHelper
{
/// <summary>
/// Checks if string value can be converted to 128bit (Guid) or 64bit (long)
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
internal static bool IsParsableTo128Or64Bit(this string value)
{
if (value.IsParsableToGuid()) return true;
return value.IsParsableToLong();
}

/// <summary>
/// Checks if hex string value is parsable to Guid
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
internal static bool IsParsableToGuid(this string value)
{
Guid result;
return Guid.TryParseExact(value, "N", out result);
}

/// <summary>
/// Checks if hex string value is parsable to long
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
internal static bool IsParsableToLong(this string value)
{
long result;
return !string.IsNullOrWhiteSpace(value) && long.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result);
}
}
}
4 changes: 3 additions & 1 deletion src/Medidata.ZipkinTracer.Core/IZipkinConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public interface IZipkinConfig

IList<string> NotToBeDisplayedDomainList { get; set; }

bool ShouldBeSampled(IOwinContext context, string sampled);
bool Create128BitTraceId { get; set; }

bool ShouldBeSampled(string sampled, string requestPath);

void Validate();
}
Expand Down
16 changes: 13 additions & 3 deletions src/Medidata.ZipkinTracer.Core/Medidata.ZipkinTracer.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -13,6 +14,8 @@
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">.\</SolutionDir>
<RestorePackages>true</RestorePackages>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -36,8 +39,8 @@
<HintPath>..\..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
Expand All @@ -56,8 +59,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\SyncHelper.cs" />
<Compile Include="App_Packages\LibLog.4.2\LibLog.cs" />
<Compile Include="Helpers\ParserHelper.cs" />
<Compile Include="Helpers\SyncHelper.cs" />
<Compile Include="ITraceProvider.cs" />
<Compile Include="ITracerClient.cs" />
<Compile Include="IZipkinConfig.cs" />
Expand Down Expand Up @@ -99,6 +103,12 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ internal class JsonSpan
private readonly Span span;

[JsonProperty("traceId")]
public string TraceId => span.TraceId.ToString("x4");
public string TraceId => span.TraceId;

[JsonProperty("name")]
public string Name => span.Name;

[JsonProperty("id")]
public string Id => span.Id.ToString("x4");
public string Id => span.Id;

[JsonProperty("parentId", NullValueHandling = NullValueHandling.Ignore)]
public string ParentId => span.ParentId?.ToString("x4");
public string ParentId => string.IsNullOrWhiteSpace(span.ParentId) ? null : span.ParentId;

[JsonProperty("annotations")]
public IEnumerable<JsonAnnotation> Annotations =>
Expand Down
9 changes: 5 additions & 4 deletions src/Medidata.ZipkinTracer.Core/Models/Span.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Medidata.ZipkinTracer.Models
{
public class Span
{
public long TraceId { get; set; }
public string TraceId { get; set; }

public string Name { get; set; }

public long Id { get; set; }
public string Id { get; set; }

public long? ParentId { get; set; }
public string ParentId { get; set; }

public IList<AnnotationBase> Annotations { get; } = new List<AnnotationBase>();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Medidata.ZipkinTracer.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.1.0")]
[assembly: AssemblyFileVersion("3.1.0")]
[assembly: AssemblyInformationalVersion("3.1.0")]
[assembly: AssemblyVersion("3.2.0")]
[assembly: AssemblyFileVersion("3.2.0")]
[assembly: AssemblyInformationalVersion("3.2.0")]
[assembly: InternalsVisibleTo("Medidata.ZipkinTracer.Core.Test")]
1 change: 0 additions & 1 deletion src/Medidata.ZipkinTracer.Core/SpanCollector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Concurrent;
using Medidata.ZipkinTracer.Core.Logging;
using Medidata.ZipkinTracer.Models;
using Medidata.ZipkinTracer.Core.Helpers;

Expand Down
26 changes: 11 additions & 15 deletions src/Medidata.ZipkinTracer.Core/SpanTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public virtual void SendServerSpan(Span span)
{
if (span == null)
{
throw new ArgumentNullException("Null server span");
throw new ArgumentNullException(nameof(span));
}

if (span.Annotations == null || !span.Annotations.Any())
Expand Down Expand Up @@ -103,7 +103,7 @@ public virtual void ReceiveClientSpan(Span span, int statusCode)
{
if (span == null)
{
throw new ArgumentNullException("Null client span");
throw new ArgumentNullException(nameof(span));
}

if (span.Annotations == null || !span.Annotations.Any())
Expand All @@ -126,7 +126,7 @@ public virtual void ReceiveClientSpan(Span span, int statusCode)
public virtual void Record(Span span, string value)
{
if (span == null)
throw new ArgumentNullException("span", "In order to record an annotation, the span must be not null.");
throw new ArgumentNullException(nameof(span), "In order to record an annotation, the span must be not null.");

span.Annotations.Add(new Annotation()
{
Expand All @@ -138,7 +138,7 @@ public virtual void Record(Span span, string value)
public void RecordBinary<T>(Span span, string key, T value)
{
if (span == null)
throw new ArgumentNullException("span", "In order to record a binary annotation, the span must be not null.");
throw new ArgumentNullException(nameof(span), "In order to record a binary annotation, the span must be not null.");

span.Annotations.Add(new BinaryAnnotation()
{
Expand All @@ -148,19 +148,15 @@ public void RecordBinary<T>(Span span, string key, T value)
});
}

private Span CreateNewSpan(string spanName, string traceId, string parentSpanId, string spanId)
internal static Span CreateNewSpan(string spanName, string traceId, string parentSpanId, string spanId)
{
var newSpan = new Span();
newSpan.Id = Int64.Parse(spanId, System.Globalization.NumberStyles.HexNumber);
newSpan.TraceId = Int64.Parse(traceId, System.Globalization.NumberStyles.HexNumber);

if (!String.IsNullOrEmpty(parentSpanId))
return new Span
{
newSpan.ParentId = Int64.Parse(parentSpanId, System.Globalization.NumberStyles.HexNumber);
}

newSpan.Name = spanName;
return newSpan;
Name = spanName,
TraceId = traceId,
ParentId = parentSpanId,
Id = spanId
};
}

private void AddBinaryAnnotation<T>(string key, T value, Span span, Endpoint endpoint)
Expand Down
36 changes: 21 additions & 15 deletions src/Medidata.ZipkinTracer.Core/TraceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Globalization;
using Microsoft.Owin;
using Medidata.ZipkinTracer.Core.Helpers;

namespace Medidata.ZipkinTracer.Core
{
Expand Down Expand Up @@ -39,6 +39,7 @@ internal class TraceProvider : ITraceProvider
/// </summary>
public bool IsSampled { get; }

// TODO: Make another constructor to accept System.Web.HttpContext for non Owin applications
/// <summary>
/// Initializes a new instance of the TraceProvider class.
/// </summary>
Expand All @@ -50,6 +51,7 @@ internal TraceProvider(IZipkinConfig config, IOwinContext context = null)
string headerSpanId = null;
string headerParentSpanId = null;
string headerSampled = null;
string requestPath = null;

if (context != null)
{
Expand All @@ -70,13 +72,15 @@ internal TraceProvider(IZipkinConfig config, IOwinContext context = null)
headerSpanId = context.Request.Headers[SpanIdHeaderName];
headerParentSpanId = context.Request.Headers[ParentSpanIdHeaderName];
headerSampled = context.Request.Headers[SampledHeaderName];
}

TraceId = Parse(headerTraceId) ? headerTraceId : GenerateHexEncodedInt64FromNewGuid();
SpanId = Parse(headerSpanId) ? headerSpanId : TraceId;
ParentSpanId = Parse(headerParentSpanId) ? headerParentSpanId : string.Empty;
IsSampled = config.ShouldBeSampled(context, headerSampled);

requestPath = context.Request.Path.ToString();
}

TraceId = headerTraceId.IsParsableTo128Or64Bit() ? headerTraceId : GenerateNewTraceId(config.Create128BitTraceId);
SpanId = headerSpanId.IsParsableToLong() ? headerSpanId : GenerateHexEncodedInt64Id();
ParentSpanId = headerParentSpanId.IsParsableToLong() ? headerParentSpanId : string.Empty;
IsSampled = config.ShouldBeSampled(headerSampled, requestPath);

if (SpanId == ParentSpanId)
{
throw new ArgumentException("x-b3-SpanId and x-b3-ParentSpanId must not be the same value.");
Expand Down Expand Up @@ -108,27 +112,29 @@ public ITraceProvider GetNext()
{
return new TraceProvider(
TraceId,
GenerateHexEncodedInt64FromNewGuid(),
GenerateHexEncodedInt64Id(),
SpanId,
IsSampled);
}

/// <summary>
/// Parse id value
/// Generate a traceId.
/// </summary>
/// <param name="value">header's value</param>
/// <returns>true: parsed</returns>
private bool Parse(string value)
/// <param name="create128Bit">true for 128bit, false for 64 bit</param>
/// <returns></returns>
private string GenerateNewTraceId(bool create128Bit)
{
long result;
return !string.IsNullOrWhiteSpace(value) && Int64.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result);
if (create128Bit)
return Guid.NewGuid().ToString("N");
else
return GenerateHexEncodedInt64Id();
}

/// <summary>
/// Generate a hex encoded Int64 from new Guid.
/// </summary>
/// <returns>The hex encoded int64</returns>
private string GenerateHexEncodedInt64FromNewGuid()
private string GenerateHexEncodedInt64Id()
{
return Convert.ToString(BitConverter.ToInt64(Guid.NewGuid().ToByteArray(), 0), 16);
}
Expand Down
Loading

0 comments on commit 4d052c7

Please sign in to comment.