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

Commit dfeee49

Browse files
direct spandata export API (#100)
* direct spandata export API * fix merge issues and build
1 parent ed56b69 commit dfeee49

File tree

6 files changed

+93
-17
lines changed

6 files changed

+93
-17
lines changed

src/OpenCensus.Abstractions/Trace/Export/ISpanExporter.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
namespace OpenCensus.Trace.Export
1818
{
1919
using System;
20+
using System.Collections.Generic;
21+
using System.Threading;
22+
using System.Threading.Tasks;
2023

2124
/// <summary>
2225
/// Span exporter.
@@ -29,6 +32,16 @@ public interface ISpanExporter : IDisposable
2932
/// <param name="span">Span to export.</param>
3033
void AddSpan(ISpan span);
3134

35+
/// <summary>
36+
/// Exports collection of spans. This method is used for the situation when the
37+
/// span objects have been created from external sources, not using the Open Censis API.
38+
/// For example, read from file or generated from objects recieved in async queue.
39+
/// </summary>
40+
/// <param name="export">Set of <see cref="ISpanData"/> objects to export.</param>
41+
/// <param name="token">Cancellation token.</param>
42+
/// <returns>A <see cref="Task"/> representing asynchronous export operation.</returns>
43+
Task ExportAsync(IEnumerable<ISpanData> export, CancellationToken token);
44+
3245
/// <summary>
3346
/// Registers the exporter handler.
3447
/// </summary>

src/OpenCensus/Trace/Export/NoopSpanExporter.cs

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

1717
namespace OpenCensus.Trace.Export
1818
{
19+
using System.Collections.Generic;
20+
using System.Threading;
21+
using System.Threading.Tasks;
22+
1923
internal sealed class NoopSpanExporter : ISpanExporter
2024
{
2125
public void AddSpan(ISpan span)
2226
{
2327
}
2428

29+
public Task ExportAsync(IEnumerable<ISpanData> export, CancellationToken token)
30+
{
31+
return Task.CompletedTask;
32+
}
33+
2534
public void Dispose()
2635
{
2736
}

src/OpenCensus/Trace/Export/SpanExporter.cs

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

1717
namespace OpenCensus.Trace.Export
1818
{
19+
using System.Collections.Generic;
1920
using System.Threading;
21+
using System.Threading.Tasks;
2022
using OpenCensus.Common;
2123

2224
public sealed class SpanExporter : SpanExporterBase
@@ -49,6 +51,14 @@ public override void AddSpan(ISpan span)
4951
this.worker.AddSpan(span);
5052
}
5153

54+
/// <inheritdoc/>
55+
public override Task ExportAsync(IEnumerable<ISpanData> export, CancellationToken token)
56+
{
57+
this.worker.ExportAsync(export, token);
58+
59+
return Task.CompletedTask;
60+
}
61+
5262
public override void RegisterHandler(string name, IHandler handler)
5363
{
5464
this.worker.RegisterHandler(name, handler);

src/OpenCensus/Trace/Export/SpanExporterBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
using System.Collections.Generic;
18+
using System.Threading;
19+
using System.Threading.Tasks;
20+
1721
namespace OpenCensus.Trace.Export
1822
{
1923
public abstract class SpanExporterBase : ISpanExporter
@@ -30,6 +34,9 @@ public static ISpanExporter NoopSpanExporter
3034

3135
public abstract void AddSpan(ISpan span);
3236

37+
/// <inheritdoc/>
38+
public abstract Task ExportAsync(IEnumerable<ISpanData> export, CancellationToken token);
39+
3340
public abstract void Dispose();
3441

3542
public abstract void RegisterHandler(string name, IHandler handler);

src/OpenCensus/Trace/Export/SpanExporterWorker.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace OpenCensus.Trace.Export
1919
using System;
2020
using System.Collections.Concurrent;
2121
using System.Collections.Generic;
22+
using System.Threading;
23+
using System.Threading.Tasks;
2224
using OpenCensus.Common;
2325
using OpenCensus.Implementation;
2426

@@ -54,6 +56,25 @@ internal void AddSpan(ISpan span)
5456
}
5557
}
5658

59+
internal Task ExportAsync(IEnumerable<ISpanData> export, CancellationToken token)
60+
{
61+
var handlers = this.serviceHandlers.Values;
62+
foreach (var handler in handlers)
63+
{
64+
try
65+
{
66+
// TODO: when handlers interface will be switched to async - this need to await
67+
handler.Export(export);
68+
}
69+
catch (Exception ex)
70+
{
71+
OpenCensusEventSource.Log.ExporterThrownExceptionWarning(ex);
72+
}
73+
}
74+
75+
return Task.CompletedTask;
76+
}
77+
5778
internal void Run(object obj)
5879
{
5980
List<ISpanData> toExport = new List<ISpanData>();
@@ -67,7 +88,7 @@ internal void Run(object obj)
6788
this.BuildList(item, toExport);
6889

6990
// Export them
70-
this.Export(toExport);
91+
this.ExportAsync(toExport, CancellationToken.None);
7192

7293
// Get ready for next batch
7394
toExport.Clear();
@@ -128,21 +149,5 @@ private void BuildList(ISpan item, List<ISpanData> toExport)
128149
}
129150
}
130151
}
131-
132-
private void Export(IEnumerable<ISpanData> export)
133-
{
134-
var handlers = this.serviceHandlers.Values;
135-
foreach (var handler in handlers)
136-
{
137-
try
138-
{
139-
handler.Export(export);
140-
}
141-
catch (Exception ex)
142-
{
143-
OpenCensusEventSource.Log.ExporterThrownExceptionWarning(ex);
144-
}
145-
}
146-
}
147152
}
148153
}

test/OpenCensus.Tests/Impl/Trace/Export/SpanExporterTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace OpenCensus.Trace.Export.Test
2020
using System.Collections.Generic;
2121
using System.Linq;
2222
using System.Threading;
23+
using System.Threading.Tasks;
2324
using Moq;
2425
using OpenCensus.Common;
2526
using OpenCensus.Internal;
@@ -181,7 +182,38 @@ public void ExportNotSampledSpans()
181182
Assert.Single(exported);
182183
Assert.DoesNotContain(span1.ToSpanData(), exported);
183184
Assert.Contains(span2.ToSpanData(), exported);
185+
}
186+
187+
[Fact]
188+
public async Task ExportAsyncCallsAllHandlers()
189+
{
190+
var exporter = SpanExporter.Create(4, Duration.Create(1, 0));
191+
192+
var handler1 = new Mock<IHandler>();
193+
var handler2 = new Mock<IHandler>();
194+
195+
196+
exporter.RegisterHandler("first", handler1.Object);
197+
exporter.RegisterHandler("second", handler2.Object);
198+
199+
var span1 = new Mock<ISpanData>();
200+
var span2 = new Mock<ISpanData>();
201+
202+
await exporter.ExportAsync(new ISpanData[] { span1.Object, span2.Object }, CancellationToken.None);
203+
204+
Assert.Single(handler1.Invocations);
205+
var args = (IEnumerable<ISpanData>)handler1.Invocations.First().Arguments.First();
206+
207+
208+
handler1.Verify(c => c.Export(It.Is<IEnumerable<ISpanData>>(
209+
(x) => x.Where((s) => s == span1.Object).Count() > 0 &&
210+
x.Where((s) => s == span2.Object).Count() > 0 &&
211+
x.Count() == 2)));
184212

213+
handler2.Verify(c => c.Export(It.Is<IEnumerable<ISpanData>>(
214+
(x) => x.Where((s) => s == span1.Object).Count() > 0 &&
215+
x.Where((s) => s == span2.Object).Count() > 0 &&
216+
x.Count() == 2)));
185217
}
186218
}
187219

0 commit comments

Comments
 (0)