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

Commit c5ad7a4

Browse files
Enable propagation and documented propagation interfaces... (#49)
* basic propagation, no tests * implement basic tests * getter works on multiple header values * make tests to be part of the same class
1 parent 17fff2a commit c5ad7a4

File tree

50 files changed

+527
-350
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+527
-350
lines changed

build/Common.test.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<CodeAnalysisRuleSet>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildProjectDirectory), 'OpenCensus.sln'))\build\OpenCensus.test.ruleset</CodeAnalysisRuleSet>
1212
</PropertyGroup>
1313

14-
1514
<ItemGroup>
1615
<AdditionalFiles Include="$(MSBuildThisFileDirectory)/stylecop.json" />
1716
</ItemGroup>

src/OpenCensus.Abstractions/Tags/TagValues.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
namespace OpenCensus.Tags
1818
{
19-
using System;
2019
using System.Collections.Generic;
21-
using OpenCensus.Utils;
2220
using OpenCensus.Utils.Abstractions;
2321

2422
public sealed class TagValues

src/OpenCensus.Abstractions/Trace/Propagation/IGetter.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/OpenCensus.Abstractions/Trace/Propagation/IPropagationComponent.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616

1717
namespace OpenCensus.Trace.Propagation
1818
{
19+
/// <summary>
20+
/// Configuration of wire protocol to use to extract and inject span context from the wire.
21+
/// </summary>
1922
public interface IPropagationComponent
2023
{
24+
/// <summary>
25+
/// Gets the binary format propagator.
26+
/// </summary>
2127
IBinaryFormat BinaryFormat { get; }
2228

29+
/// <summary>
30+
/// Gets the text format propagator.
31+
/// </summary>
2332
ITextFormat TextFormat { get; }
2433
}
2534
}

src/OpenCensus.Abstractions/Trace/Propagation/ISetter.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/OpenCensus.Abstractions/Trace/Propagation/ITextFormat.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,38 @@
1616

1717
namespace OpenCensus.Trace.Propagation
1818
{
19+
using System;
1920
using System.Collections.Generic;
2021

22+
/// <summary>
23+
/// Text format wire context propagator. Helps to extract and inject context from textual
24+
/// representation (typically http headers or metadata colleciton).
25+
/// </summary>
2126
public interface ITextFormat
2227
{
28+
/// <summary>
29+
/// Gets the list of headers used by propagator. The use cases of this are:
30+
/// * allow pre-allocation of fields, especially in systems like gRPC Metadata
31+
/// * allow a single-pass over an iterator (ex OpenTracing has no getter in TextMap).
32+
/// </summary>
2333
IList<string> Fields { get; }
2434

25-
void Inject<T>(ISpanContext spanContext, T carrier, ISetter<T> setter);
35+
/// <summary>
36+
/// Injects textual representation of span context to transmit over the wire.
37+
/// </summary>
38+
/// <typeparam name="T">Type of an object to set context on. Typically HttpRequest or similar.</typeparam>
39+
/// <param name="spanContext">Span context to transmit over the wire.</param>
40+
/// <param name="carrier">Object to set context on. Instance of this object will be passed to setter.</param>
41+
/// <param name="setter">Action that will set name and value pair on the object.</param>
42+
void Inject<T>(ISpanContext spanContext, T carrier, Action<T, string, string> setter);
2643

27-
ISpanContext Extract<T>(T carrier, IGetter<T> getter);
44+
/// <summary>
45+
/// Extracts span context from textual representation.
46+
/// </summary>
47+
/// <typeparam name="T">Type of object to extract context from. Typically HttpRequest or similar.</typeparam>
48+
/// <param name="carrier">Object to extract context from. Instance of this object will be passed to the getter.</param>
49+
/// <param name="getter">Function that will return string value of a key with the specified name.</param>
50+
/// <returns>Span context from it's text representation.</returns>
51+
ISpanContext Extract<T>(T carrier, Func<T, string, IEnumerable<string>> getter);
2852
}
2953
}

src/OpenCensus.Collector.AspNetCore/Implementation/HttpInListener.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ namespace OpenCensus.Collector.AspNetCore.Implementation
2121
using Microsoft.AspNetCore.Http.Features;
2222
using OpenCensus.Collector.Implementation.Common;
2323
using OpenCensus.Trace;
24+
using OpenCensus.Trace.Propagation;
2425

2526
internal class HttpInListener : ListenerHandler
2627
{
2728
private readonly PropertyFetcher startContextFetcher = new PropertyFetcher("HttpContext");
2829
private readonly PropertyFetcher stopContextFetcher = new PropertyFetcher("HttpContext");
30+
private readonly IPropagationComponent propagationComponent;
2931

30-
public HttpInListener(ITracer tracer, ISampler sampler) : base("Microsoft.AspNetCore", tracer, sampler)
32+
public HttpInListener(ITracer tracer, ISampler sampler, IPropagationComponent propagationComponent) : base("Microsoft.AspNetCore", tracer, sampler)
3133
{
34+
this.propagationComponent = propagationComponent;
3235
}
3336

3437
public override void OnStartActivity(Activity activity, object payload)
@@ -43,7 +46,12 @@ public override void OnStartActivity(Activity activity, object payload)
4346

4447
var request = context.Request;
4548

46-
this.Tracer.SpanBuilder("HttpIn").SetSampler(this.Sampler).StartScopedSpan();
49+
var ctx = this.propagationComponent.TextFormat.Extract<HttpRequest>(
50+
request,
51+
(r, name) => r.Headers[name]
52+
);
53+
54+
this.Tracer.SpanBuilderWithRemoteParent("HttpIn", ctx).SetSampler(this.Sampler).StartScopedSpan();
4755

4856
var span = this.Tracer.CurrentSpan;
4957

src/OpenCensus.Collector.AspNetCore/RequestsCollector.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace OpenCensus.Collector.AspNetCore
2121
using OpenCensus.Collector.AspNetCore.Implementation;
2222
using OpenCensus.Collector.Implementation.Common;
2323
using OpenCensus.Trace;
24+
using OpenCensus.Trace.Propagation;
2425

2526
/// <summary>
2627
/// Dependencies collector.
@@ -35,11 +36,12 @@ public class RequestsCollector : IDisposable
3536
/// <param name="options">Configuration options for dependencies collector.</param>
3637
/// <param name="tracer">Tracer to record traced with.</param>
3738
/// <param name="sampler">Sampler to use to sample dependnecy calls.</param>
38-
public RequestsCollector(RequestsCollectorOptions options, ITracer tracer, ISampler sampler)
39+
/// <param name="propagationComponent">Wire context propagation component.</param>
40+
public RequestsCollector(RequestsCollectorOptions options, ITracer tracer, ISampler sampler, IPropagationComponent propagationComponent)
3941
{
4042
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
4143
new Dictionary<string, Func<ITracer, ISampler, ListenerHandler>>()
42-
{ {"Microsoft.AspNetCore", (t, s) => new HttpInListener(t, s) } },
44+
{ {"Microsoft.AspNetCore", (t, s) => new HttpInListener(t, s, propagationComponent) } },
4345
tracer,
4446
sampler);
4547
this.diagnosticSourceSubscriber.Subscribe();

src/OpenCensus.Collector.Dependencies/DependenciesCollector.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace OpenCensus.Collector.Dependencies
2121
using OpenCensus.Collector.Dependencies.Implementation;
2222
using OpenCensus.Collector.Implementation.Common;
2323
using OpenCensus.Trace;
24+
using OpenCensus.Trace.Propagation;
2425

2526
/// <summary>
2627
/// Dependencies collector.
@@ -35,11 +36,11 @@ public class DependenciesCollector : IDisposable
3536
/// <param name="options">Configuration options for dependencies collector.</param>
3637
/// <param name="tracer">Tracer to record traced with.</param>
3738
/// <param name="sampler">Sampler to use to sample dependnecy calls.</param>
38-
public DependenciesCollector(DependenciesCollectorOptions options, ITracer tracer, ISampler sampler)
39+
public DependenciesCollector(DependenciesCollectorOptions options, ITracer tracer, ISampler sampler, IPropagationComponent propagationComponent)
3940
{
4041
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
4142
new Dictionary<string, Func<ITracer, ISampler, ListenerHandler>>()
42-
{ {"HttpHandlerDiagnosticListener", (t, s) => new HttpHandlerDiagnosticListener(t, s) } },
43+
{ {"HttpHandlerDiagnosticListener", (t, s) => new HttpHandlerDiagnosticListener(t, s, propagationComponent) } },
4344
tracer,
4445
sampler);
4546
this.diagnosticSourceSubscriber.Subscribe();

src/OpenCensus.Collector.Dependencies/Implementation/HttpHandlerDiagnosticListener.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
namespace OpenCensus.Collector.Dependencies.Implementation
1818
{
1919
using System;
20+
using System.Collections.Generic;
2021
using System.Diagnostics;
2122
using System.Linq;
2223
using System.Net;
2324
using System.Net.Http;
2425
using System.Threading.Tasks;
2526
using OpenCensus.Collector.Implementation.Common;
2627
using OpenCensus.Trace;
28+
using OpenCensus.Trace.Propagation;
2729

2830
internal class HttpHandlerDiagnosticListener : ListenerHandler
2931
{
@@ -32,8 +34,11 @@ internal class HttpHandlerDiagnosticListener : ListenerHandler
3234
private readonly PropertyFetcher stopExceptionFetcher = new PropertyFetcher("Exception");
3335
private readonly PropertyFetcher stopRequestStatusFetcher = new PropertyFetcher("RequestTaskStatus");
3436

35-
public HttpHandlerDiagnosticListener(ITracer tracer, ISampler sampler) : base("HttpHandlerDiagnosticListener", tracer, sampler)
37+
private readonly IPropagationComponent propagationComponent;
38+
39+
public HttpHandlerDiagnosticListener(ITracer tracer, ISampler sampler, IPropagationComponent propagationComponent) : base("HttpHandlerDiagnosticListener", tracer, sampler)
3640
{
41+
this.propagationComponent = propagationComponent;
3742
}
3843

3944
public override void OnStartActivity(Activity activity, object payload)
@@ -56,7 +61,10 @@ public override void OnStartActivity(Activity activity, object payload)
5661
span.PutHttpMethodAttribute(request.Method.ToString());
5762
span.PutHttpHostAttribute(request.RequestUri.Host, request.RequestUri.Port);
5863
span.PutHttpPathAttribute(request.RequestUri.AbsolutePath);
59-
span.PutHttpUserAgentAttribute(request.Headers.GetValues("User-Agent").FirstOrDefault());
64+
request.Headers.TryGetValues("User-Agent", out IEnumerable<string> userAgents);
65+
span.PutHttpUserAgentAttribute(userAgents?.FirstOrDefault());
66+
67+
this.propagationComponent.TextFormat.Inject<HttpRequestMessage>(span.Context, request, (r, k, v) => r.Headers.Add(k, v));
6068
}
6169

6270
public override void OnStopActivity(Activity activity, object payload)

0 commit comments

Comments
 (0)