Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource allocation improvement #1463

Merged
merged 15 commits into from
Nov 9, 2020
Merged
8 changes: 7 additions & 1 deletion src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class ConsoleExporter<T> : BaseExporter<T>
where T : class
{
private readonly ConsoleExporterOptions options;
private TracerProvider tracerProvider;

public ConsoleExporter(ConsoleExporterOptions options)
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public override ExportResult Export(in Batch<T> batch)
}
}

var resource = activity.GetResource();
var resource = this.tracerProvider?.GetResource() ?? Resource.Empty;
if (resource != Resource.Empty)
{
this.WriteLine("Resource associated with Activity:");
Expand Down Expand Up @@ -140,6 +141,11 @@ public override ExportResult Export(in Batch<T> batch)
return ExportResult.Success;
}

protected override void OnTracerProviderSet(TracerProvider tracerProvider)
{
this.tracerProvider = tracerProvider;
}

private void WriteLine(string message)
{
if (this.options.Targets.HasFlag(ConsoleExporterOutputTargets.Console))
Expand Down
19 changes: 10 additions & 9 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ public override ExportResult Export(in Batch<Activity> activityBatch)
{
foreach (var activity in activityBatch)
{
if (this.processCache == null)
{
this.ApplyLibraryResource(activity.GetResource());
}

this.AppendSpan(activity.ToJaegerSpan());
}

Expand All @@ -87,18 +82,18 @@ public override ExportResult Export(in Batch<Activity> activityBatch)
}
}

internal void ApplyLibraryResource(Resource libraryResource)
internal void SetResource(Resource resource)
{
if (libraryResource is null)
if (resource is null)
{
throw new ArgumentNullException(nameof(libraryResource));
throw new ArgumentNullException(nameof(resource));
}

var process = this.Process;

string serviceName = null;
string serviceNamespace = null;
foreach (var label in libraryResource.Attributes)
foreach (var label in resource.Attributes)
{
string key = label.Key;

Expand Down Expand Up @@ -203,6 +198,12 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

/// <inheritdoc/>
protected override void OnTracerProviderSet(TracerProvider tracerProvider)
{
this.SetResource(tracerProvider.GetResource());
}

private void SendCurrentBatches(Batch workingBatch)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using OpenTelemetry.Trace;
using OtlpCollector = Opentelemetry.Proto.Collector.Trace.V1;
using OtlpCommon = Opentelemetry.Proto.Common.V1;
using OtlpResource = Opentelemetry.Proto.Resource.V1;
using OtlpTrace = Opentelemetry.Proto.Trace.V1;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
Expand All @@ -40,23 +41,18 @@ internal static class ActivityExtensions

internal static void AddBatch(
this OtlpCollector.ExportTraceServiceRequest request,
OtlpExporter otlpExporter,
OtlpResource.Resource processResource,
in Batch<Activity> activityBatch)
{
Dictionary<string, OtlpTrace.InstrumentationLibrarySpans> spansByLibrary = new Dictionary<string, OtlpTrace.InstrumentationLibrarySpans>();
OtlpTrace.ResourceSpans resourceSpans = null;
OtlpTrace.ResourceSpans resourceSpans = new OtlpTrace.ResourceSpans
{
Resource = processResource,
};
request.ResourceSpans.Add(resourceSpans);

foreach (var activity in activityBatch)
{
if (resourceSpans == null)
{
resourceSpans = new OtlpTrace.ResourceSpans
{
Resource = otlpExporter.EnsureProcessResource(activity),
};
request.ResourceSpans.Add(resourceSpans);
}

OtlpTrace.Span span = activity.ToOtlpSpan();
if (span == null)
{
Expand Down
24 changes: 12 additions & 12 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Grpc.Core;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
Expand All @@ -40,7 +39,6 @@ public class OtlpExporter : BaseExporter<Activity>
private readonly Channel channel;
private readonly OtlpCollector.TraceService.ITraceServiceClient traceClient;
private readonly Metadata headers;
private OtlpResource.Resource processResource;

/// <summary>
/// Initializes a new instance of the <see cref="OtlpExporter"/> class.
Expand All @@ -62,12 +60,14 @@ internal OtlpExporter(OtlpExporterOptions options, OtlpCollector.TraceService.IT
}
}

internal OtlpResource.Resource ProcessResource { get; private set; }

/// <inheritdoc/>
public override ExportResult Export(in Batch<Activity> activityBatch)
{
OtlpCollector.ExportTraceServiceRequest request = new OtlpCollector.ExportTraceServiceRequest();

request.AddBatch(this, activityBatch);
request.AddBatch(this.ProcessResource, activityBatch);

try
{
Expand All @@ -87,17 +87,11 @@ public override ExportResult Export(in Batch<Activity> activityBatch)
return ExportResult.Success;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal OtlpResource.Resource EnsureProcessResource(Activity activity)
internal void SetResource(Resource resource)
{
if (this.processResource != null)
{
return this.processResource;
}

OtlpResource.Resource processResource = new OtlpResource.Resource();

foreach (KeyValuePair<string, object> attribute in activity.GetResource().Attributes)
foreach (KeyValuePair<string, object> attribute in resource.Attributes)
{
var oltpAttribute = attribute.ToOtlpAttribute();
if (oltpAttribute != null)
Expand All @@ -121,7 +115,7 @@ internal OtlpResource.Resource EnsureProcessResource(Activity activity)
});
}

return this.processResource = processResource;
this.ProcessResource = processResource;
}

/// <inheritdoc/>
Expand All @@ -134,5 +128,11 @@ protected override bool OnShutdown(int timeoutMilliseconds)

return Task.WaitAny(new Task[] { this.channel.ShutdownAsync(), Task.Delay(timeoutMilliseconds) }) == 0;
}

/// <inheritdoc/>
protected override void OnTracerProviderSet(TracerProvider tracerProvider)
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
{
this.SetResource(tracerProvider.GetResource());
}
}
}
23 changes: 10 additions & 13 deletions src/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#else
using System.Text.Json;
#endif
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Exporter.Zipkin.Implementation;
Expand Down Expand Up @@ -92,14 +91,8 @@ public override ExportResult Export(in Batch<Activity> batch)
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ZipkinEndpoint EnsureLocalEndpoint(Activity activity)
internal void SetResource(Resource resource)
{
if (this.LocalEndpoint != null)
{
return this.LocalEndpoint;
}

var hostName = ResolveHostName();

string ipv4 = null;
Expand All @@ -113,7 +106,7 @@ internal ZipkinEndpoint EnsureLocalEndpoint(Activity activity)
string serviceName = null;
string serviceNamespace = null;
Dictionary<string, object> tags = null;
foreach (var label in activity.GetResource().Attributes)
foreach (var label in resource.Attributes)
{
string key = label.Key;

Expand Down Expand Up @@ -149,14 +142,20 @@ internal ZipkinEndpoint EnsureLocalEndpoint(Activity activity)
serviceName = this.options.ServiceName;
}

return this.LocalEndpoint = new ZipkinEndpoint(
this.LocalEndpoint = new ZipkinEndpoint(
serviceName,
ipv4,
ipv6,
port: null,
tags);
}

/// <inheritdoc/>
protected override void OnTracerProviderSet(TracerProvider tracerProvider)
{
this.SetResource(tracerProvider.GetResource());
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
}

private static string ResolveHostAddress(string hostName, AddressFamily family)
{
string result = null;
Expand Down Expand Up @@ -257,9 +256,7 @@ protected override Task SerializeToStreamAsync(Stream stream, TransportContext c

foreach (var activity in this.batch)
{
var localEndpoint = this.exporter.EnsureLocalEndpoint(activity);

var zipkinSpan = activity.ToZipkinSpan(localEndpoint, this.exporter.options.UseShortTraceIds);
var zipkinSpan = activity.ToZipkinSpan(this.exporter.LocalEndpoint, this.exporter.options.UseShortTraceIds);

zipkinSpan.Write(this.writer);

Expand Down
7 changes: 7 additions & 0 deletions src/OpenTelemetry/BaseExportProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

namespace OpenTelemetry
{
Expand Down Expand Up @@ -46,6 +47,12 @@ public sealed override void OnStart(T data)
/// <inheritdoc />
public abstract override void OnEnd(T data);

/// <inheritdoc />
internal override void SetTracerProvider(TracerProvider tracerProvider)
{
this.exporter.SetTracerProvider(tracerProvider);
}

/// <inheritdoc />
protected override bool OnShutdown(int timeoutMilliseconds)
{
Expand Down
14 changes: 14 additions & 0 deletions src/OpenTelemetry/BaseExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Threading;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

namespace OpenTelemetry
{
Expand Down Expand Up @@ -100,6 +101,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

internal void SetTracerProvider(TracerProvider tracerProvider)
{
this.OnTracerProviderSet(tracerProvider);
}

/// <summary>
/// Called by <c>Shutdown</c>. This function should block the current
/// thread until shutdown completed or timed out.
Expand Down Expand Up @@ -132,5 +138,13 @@ protected virtual bool OnShutdown(int timeoutMilliseconds)
protected virtual void Dispose(bool disposing)
{
}

/// <summary>
/// Called when the parent <see cref="TracerProvider"/> is set on the exporter.
/// </summary>
/// <param name="tracerProvider"><see cref="TracerProvider"/>.</param>
protected virtual void OnTracerProviderSet(TracerProvider tracerProvider)
CodeBlanch marked this conversation as resolved.
Show resolved Hide resolved
{
}
}
}
9 changes: 9 additions & 0 deletions src/OpenTelemetry/BaseProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Threading;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

namespace OpenTelemetry
{
Expand Down Expand Up @@ -141,6 +142,14 @@ public void Dispose()
GC.SuppressFinalize(this);
}

/// <summary>
/// Set the parent <see cref="TracerProvider"/>.
/// </summary>
/// <param name="tracerProvider"><see cref="TracerProvider"/>.</param>
internal virtual void SetTracerProvider(TracerProvider tracerProvider)
{
}

/// <summary>
/// Called by <c>ForceFlush</c>. This function should block the current
/// thread until flush completed, shutdown signaled or timed out.
Expand Down
53 changes: 0 additions & 53 deletions src/OpenTelemetry/Trace/ActivityExtensions.cs

This file was deleted.

Loading