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

Commit 17fff2a

Browse files
Simon ZeltserSergeyKanzhelev
authored andcommitted
Adding support for links and capturing all types of spans (#50)
* Introducing Stackdriver Exporter for Opencensus C# library - Current implementation can only store string values - Added the exporter and trace handler only - The exporter relies on newest Trace API from Stackdriver. * Updating translation from ISpan to Stackdriver's Span to cover more fields * Fixing the issue that prevented Stackdriver API call to succeed: now construction of Span resource is taken care of by SpanName class that is part of Stackdriver Trace V2 API. * - Added support for capturing all types of trace spans (long/bool/string) - Fixed csproj, so it produces both .NET Core and .NET versions. It also means signing the assembly using the same mechanism as other assemblies in the solution * - Added support for storing links - Minor fixes to proto<->opencensus translation methods * Fixing merge issue
1 parent f9a47eb commit 17fff2a

File tree

3 files changed

+81
-24
lines changed

3 files changed

+81
-24
lines changed

src/OpenCensus.Exporter.Stackdriver/Implementation/StackdriverTraceExporter.cs

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ static class SpanExtensions
2020
public static Span ToSpan(this ISpanData spanData, string projectId)
2121
{
2222
string spanId = spanData.Context.SpanId.ToLowerBase16();
23+
24+
// Base span settings
2325
var span = new Span
2426
{
2527
SpanName = new SpanName(projectId, spanData.Context.TraceId.ToLowerBase16(), spanId),
@@ -29,7 +31,26 @@ public static Span ToSpan(this ISpanData spanData, string projectId)
2931
EndTime = spanData.EndTimestamp.ToTimestamp(),
3032
ChildSpanCount = spanData.ChildSpanCount,
3133
};
34+
if (spanData.ParentSpanId != null)
35+
{
36+
string parentSpanId = spanData.ParentSpanId.ToLowerBase16();
37+
if (!string.IsNullOrEmpty(parentSpanId))
38+
{
39+
span.ParentSpanId = parentSpanId;
40+
}
41+
}
3242

43+
// Span Links
44+
if (spanData.Links != null)
45+
{
46+
span.Links = new Span.Types.Links
47+
{
48+
DroppedLinksCount = spanData.Links.DroppedLinksCount,
49+
Link = { spanData.Links.Links.Select(l => l.ToLink()) }
50+
};
51+
}
52+
53+
// Span Attributes
3354
if (spanData.Attributes != null)
3455
{
3556
span.Attributes = new Span.Types.Attributes
@@ -42,33 +63,59 @@ public static Span ToSpan(this ISpanData spanData, string projectId)
4263
};
4364
}
4465

45-
if (spanData.ParentSpanId != null)
66+
return span;
67+
}
68+
69+
public static Span.Types.Link ToLink(this ILink link)
70+
{
71+
var ret = new Span.Types.Link();
72+
ret.SpanId = link.SpanId.ToLowerBase16();
73+
ret.TraceId = link.TraceId.ToLowerBase16();
74+
75+
if (link.Attributes != null)
4676
{
47-
string parentSpanId = spanData.ParentSpanId.ToLowerBase16();
48-
if (!string.IsNullOrEmpty(parentSpanId))
77+
ret.Attributes = new Span.Types.Attributes
4978
{
50-
span.ParentSpanId = parentSpanId;
51-
}
79+
80+
DroppedAttributesCount = OpenCensus.Trace.Config.TraceParams.DEFAULT.MaxNumberOfAttributes - link.Attributes.Count,
81+
82+
AttributeMap = { link.Attributes.ToDictionary(
83+
att => att.Key,
84+
att => att.Value.ToAttributeValue()) }
85+
};
5286
}
5387

54-
return span;
88+
return ret;
5589
}
5690

5791
public static Google.Cloud.Trace.V2.AttributeValue ToAttributeValue(this IAttributeValue av)
5892
{
59-
// TODO Currently we assume we store only strings.
60-
return new Google.Cloud.Trace.V2.AttributeValue
93+
var ret = new Google.Cloud.Trace.V2.AttributeValue();
94+
var attributeType = av.GetType();
95+
96+
// Handle all primitive types
97+
if (attributeType == typeof(AttributeValue<bool>))
6198
{
62-
StringValue = new TruncatableString
99+
ret.BoolValue = ((AttributeValue<bool>)av).Value;
100+
}
101+
else if (attributeType == typeof(AttributeValue<long>))
102+
{
103+
ret.IntValue = ((AttributeValue<long>)av).Value;
104+
}
105+
else // String or anything else is written as string
106+
{
107+
ret.StringValue = new TruncatableString()
63108
{
64109
Value = av.Match(
65-
s => s,
66-
b => b.ToString(),
67-
l => l.ToString(),
68-
obj => obj.ToString(),
69-
obj => obj.ToString())
70-
}
71-
};
110+
s => s,
111+
b => b.ToString(),
112+
l => l.ToString(),
113+
d => d.ToString(),
114+
o => o.ToString())
115+
};
116+
}
117+
118+
return ret;
72119
}
73120
}
74121

src/OpenCensus.Exporter.Stackdriver/OpenCensus.Exporter.Stackdriver.csproj

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
<!--
32
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'OpenCensus.sln'))\build\Common.prod.props" />
4-
-->
5-
<PropertyGroup>
6-
<TargetFramework>netstandard2.0</TargetFramework>
7-
<IncludeSymbols>True</IncludeSymbols>
8-
</PropertyGroup>
9-
103
<PropertyGroup>
4+
<TargetFrameworks>net46;netstandard2.0</TargetFrameworks>
5+
<TargetFrameworks Condition="$(OS) != 'Windows_NT'">netstandard2.0</TargetFrameworks>
116
<Description>Stackdriver Exporter for OpenCensus.</Description>
7+
<IncludeSymbols>True</IncludeSymbols>
128
<PackageTags>Tracing;OpenCensus;Management;Monitoring;Stackdriver;Google;GCP;distributed-tracing</PackageTags>
139
<PackageIconUrl>https://opencensus.io/images/opencensus-logo.png</PackageIconUrl>
1410
<PackageProjectUrl>https://opencensus.io</PackageProjectUrl>
1511
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
1612
<Authors>OpenCensus authors</Authors>
1713
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
1814
</PropertyGroup>
19-
15+
16+
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
17+
<DebugType>full</DebugType>
18+
<DebugSymbols>true</DebugSymbols>
19+
</PropertyGroup>
20+
2021
<ItemGroup>
2122
<PackageReference Include="Google.Cloud.Trace.V2" Version="1.0.0-beta02" />
2223
</ItemGroup>

src/OpenCensus.Exporter.Stackdriver/Utils/ProtoExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@ namespace OpenCensus.Exporter.Stackdriver.Utils
44
using Google.Protobuf.WellKnownTypes;
55
using OpenCensus.Common;
66

7+
/// <summary>
8+
/// Translation methods from Opencensus structures to common
9+
/// Protobuf structures
10+
/// </summary>
711
public static class ProtoExtensions
812
{
13+
/// <summary>
14+
/// Translates Opencensus Timestamp to Protobuf's timestamp
15+
/// </summary>
16+
/// <param name="timestamp">Opencensus timestamp</param>
17+
/// <returns>Protobuf's timestamp</returns>
918
public static Timestamp ToTimestamp(this ITimestamp timestamp)
1019
{
1120
return new Timestamp { Seconds = timestamp.Seconds, Nanos = timestamp.Nanos };

0 commit comments

Comments
 (0)