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
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/Common.prod.props
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@

<!--PublicApi Analyzer-->
<ItemGroup>
<AdditionalFiles Include=".publicApi\$(TargetFramework)\PublicAPI.Shipped.txt" />
<AdditionalFiles Include=".publicApi\$(TargetFramework)\PublicAPI.Unshipped.txt" />
<AdditionalFiles Include=".publicApi\*\PublicAPI.Shipped.txt" />
eddynaka marked this conversation as resolved.
Show resolved Hide resolved
<AdditionalFiles Include=".publicApi\*\PublicAPI.Unshipped.txt" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ OpenTelemetry.Baggage.RemoveBaggage(string name) -> OpenTelemetry.Baggage
OpenTelemetry.Baggage.SetBaggage(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> baggageItems) -> OpenTelemetry.Baggage
OpenTelemetry.Baggage.SetBaggage(params System.Collections.Generic.KeyValuePair<string, string>[] baggageItems) -> OpenTelemetry.Baggage
OpenTelemetry.Baggage.SetBaggage(string name, string value) -> OpenTelemetry.Baggage
OpenTelemetry.BaseProvider
OpenTelemetry.BaseProvider.BaseProvider() -> void
OpenTelemetry.BaseProvider.Dispose() -> void
OpenTelemetry.BaseProvider.~BaseProvider() -> void
OpenTelemetry.Context.Propagation.B3Propagator
OpenTelemetry.Context.Propagation.B3Propagator.B3Propagator() -> void
OpenTelemetry.Context.Propagation.B3Propagator.B3Propagator(bool singleHeader) -> void
Expand Down Expand Up @@ -70,7 +74,6 @@ OpenTelemetry.Metrics.Meter.Meter() -> void
OpenTelemetry.Metrics.MeterFactoryBase
OpenTelemetry.Metrics.MeterFactoryBase.MeterFactoryBase() -> void
OpenTelemetry.Metrics.MeterProvider
OpenTelemetry.Metrics.MeterProvider.Dispose() -> void
OpenTelemetry.Metrics.MeterProvider.MeterProvider() -> void
OpenTelemetry.Metrics.NoopBoundCounterMetric<T>
OpenTelemetry.Metrics.NoopBoundCounterMetric<T>.NoopBoundCounterMetric() -> void
Expand Down Expand Up @@ -166,7 +169,6 @@ OpenTelemetry.Trace.Tracer.StartRootSpan(string name, OpenTelemetry.Trace.SpanKi
OpenTelemetry.Trace.Tracer.StartSpan(string name, OpenTelemetry.Trace.SpanKind kind = OpenTelemetry.Trace.SpanKind.Internal, in OpenTelemetry.Trace.SpanContext parentContext = default(OpenTelemetry.Trace.SpanContext), OpenTelemetry.Trace.SpanAttributes initialAttributes = null, System.Collections.Generic.IEnumerable<OpenTelemetry.Trace.Link> links = null, System.DateTimeOffset startTime = default(System.DateTimeOffset)) -> OpenTelemetry.Trace.TelemetrySpan
OpenTelemetry.Trace.Tracer.StartSpan(string name, OpenTelemetry.Trace.SpanKind kind, in OpenTelemetry.Trace.TelemetrySpan parentSpan, OpenTelemetry.Trace.SpanAttributes initialAttributes = null, System.Collections.Generic.IEnumerable<OpenTelemetry.Trace.Link> links = null, System.DateTimeOffset startTime = default(System.DateTimeOffset)) -> OpenTelemetry.Trace.TelemetrySpan
OpenTelemetry.Trace.TracerProvider
OpenTelemetry.Trace.TracerProvider.Dispose() -> void
OpenTelemetry.Trace.TracerProvider.GetTracer(string name, string version = null) -> OpenTelemetry.Trace.Tracer
OpenTelemetry.Trace.TracerProvider.TracerProvider() -> void
abstract OpenTelemetry.Context.RuntimeContextSlot<T>.Get() -> T
Expand Down Expand Up @@ -300,10 +302,9 @@ static readonly OpenTelemetry.Metrics.NoopMeasureMetric<T>.Instance -> OpenTelem
static readonly OpenTelemetry.Trace.Status.Error -> OpenTelemetry.Trace.Status
static readonly OpenTelemetry.Trace.Status.Ok -> OpenTelemetry.Trace.Status
static readonly OpenTelemetry.Trace.Status.Unset -> OpenTelemetry.Trace.Status
virtual OpenTelemetry.BaseProvider.Dispose(bool disposing) -> void
virtual OpenTelemetry.Context.RuntimeContextSlot<T>.Dispose(bool disposing) -> void
virtual OpenTelemetry.Metrics.LabelSet.Labels.get -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>
virtual OpenTelemetry.Metrics.LabelSet.Labels.set -> void
virtual OpenTelemetry.Metrics.MeterFactoryBase.GetMeter(string name, string version = null) -> OpenTelemetry.Metrics.Meter
virtual OpenTelemetry.Metrics.MeterProvider.Dispose(bool disposing) -> void
virtual OpenTelemetry.Metrics.MeterProvider.GetMeter(string name, string version = null) -> OpenTelemetry.Metrics.Meter
virtual OpenTelemetry.Trace.TracerProvider.Dispose(bool disposing) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ OpenTelemetry.Baggage.RemoveBaggage(string name) -> OpenTelemetry.Baggage
OpenTelemetry.Baggage.SetBaggage(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> baggageItems) -> OpenTelemetry.Baggage
OpenTelemetry.Baggage.SetBaggage(params System.Collections.Generic.KeyValuePair<string, string>[] baggageItems) -> OpenTelemetry.Baggage
OpenTelemetry.Baggage.SetBaggage(string name, string value) -> OpenTelemetry.Baggage
OpenTelemetry.BaseProvider
OpenTelemetry.BaseProvider.BaseProvider() -> void
OpenTelemetry.BaseProvider.Dispose() -> void
OpenTelemetry.BaseProvider.~BaseProvider() -> void
OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>
OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.AsyncLocalRuntimeContextSlot(string name) -> void
OpenTelemetry.Context.Propagation.B3Propagator
Expand Down Expand Up @@ -70,7 +74,6 @@ OpenTelemetry.Metrics.Meter.Meter() -> void
OpenTelemetry.Metrics.MeterFactoryBase
OpenTelemetry.Metrics.MeterFactoryBase.MeterFactoryBase() -> void
OpenTelemetry.Metrics.MeterProvider
OpenTelemetry.Metrics.MeterProvider.Dispose() -> void
OpenTelemetry.Metrics.MeterProvider.MeterProvider() -> void
OpenTelemetry.Metrics.NoopBoundCounterMetric<T>
OpenTelemetry.Metrics.NoopBoundCounterMetric<T>.NoopBoundCounterMetric() -> void
Expand Down Expand Up @@ -166,7 +169,6 @@ OpenTelemetry.Trace.Tracer.StartRootSpan(string name, OpenTelemetry.Trace.SpanKi
OpenTelemetry.Trace.Tracer.StartSpan(string name, OpenTelemetry.Trace.SpanKind kind = OpenTelemetry.Trace.SpanKind.Internal, in OpenTelemetry.Trace.SpanContext parentContext = default(OpenTelemetry.Trace.SpanContext), OpenTelemetry.Trace.SpanAttributes initialAttributes = null, System.Collections.Generic.IEnumerable<OpenTelemetry.Trace.Link> links = null, System.DateTimeOffset startTime = default(System.DateTimeOffset)) -> OpenTelemetry.Trace.TelemetrySpan
OpenTelemetry.Trace.Tracer.StartSpan(string name, OpenTelemetry.Trace.SpanKind kind, in OpenTelemetry.Trace.TelemetrySpan parentSpan, OpenTelemetry.Trace.SpanAttributes initialAttributes = null, System.Collections.Generic.IEnumerable<OpenTelemetry.Trace.Link> links = null, System.DateTimeOffset startTime = default(System.DateTimeOffset)) -> OpenTelemetry.Trace.TelemetrySpan
OpenTelemetry.Trace.TracerProvider
OpenTelemetry.Trace.TracerProvider.Dispose() -> void
OpenTelemetry.Trace.TracerProvider.GetTracer(string name, string version = null) -> OpenTelemetry.Trace.Tracer
OpenTelemetry.Trace.TracerProvider.TracerProvider() -> void
abstract OpenTelemetry.Context.RuntimeContextSlot<T>.Get() -> T
Expand Down Expand Up @@ -300,10 +302,9 @@ static readonly OpenTelemetry.Metrics.NoopMeasureMetric<T>.Instance -> OpenTelem
static readonly OpenTelemetry.Trace.Status.Error -> OpenTelemetry.Trace.Status
static readonly OpenTelemetry.Trace.Status.Ok -> OpenTelemetry.Trace.Status
static readonly OpenTelemetry.Trace.Status.Unset -> OpenTelemetry.Trace.Status
virtual OpenTelemetry.BaseProvider.Dispose(bool disposing) -> void
virtual OpenTelemetry.Context.RuntimeContextSlot<T>.Dispose(bool disposing) -> void
virtual OpenTelemetry.Metrics.LabelSet.Labels.get -> System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>
virtual OpenTelemetry.Metrics.LabelSet.Labels.set -> void
virtual OpenTelemetry.Metrics.MeterFactoryBase.GetMeter(string name, string version = null) -> OpenTelemetry.Metrics.Meter
virtual OpenTelemetry.Metrics.MeterProvider.Dispose(bool disposing) -> void
virtual OpenTelemetry.Metrics.MeterProvider.GetMeter(string name, string version = null) -> OpenTelemetry.Metrics.Meter
virtual OpenTelemetry.Trace.TracerProvider.Dispose(bool disposing) -> void
49 changes: 49 additions & 0 deletions src/OpenTelemetry.Api/BaseProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <copyright file="BaseProvider.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System;

namespace OpenTelemetry
{
/// <summary>
/// Contains logic shared by all OpenTelemetry providers.
/// </summary>
public abstract class BaseProvider : IDisposable
{
/// <summary>
/// Finalizes an instance of the <see cref="BaseProvider"/> class.
/// </summary>
~BaseProvider()
{
this.Dispose(false);
}

/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases the unmanaged resources used by this class and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
}
}
}
17 changes: 1 addition & 16 deletions src/OpenTelemetry.Api/Metrics/MeterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace OpenTelemetry.Metrics
/// <summary>
/// MeterProvider is the entry point of the OpenTelemetry Metrics API. It provides access to Meters.
/// </summary>
public class MeterProvider : IDisposable
public class MeterProvider : BaseProvider
{
private static ProxyMeter proxyMeter = new ProxyMeter();
private static bool isInitialized;
Expand Down Expand Up @@ -71,13 +71,6 @@ public static void SetDefault(MeterProvider meterProvider)
isInitialized = true;
}

/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Returns a Meter for a given name and version.
/// </summary>
Expand All @@ -96,13 +89,5 @@ internal static void Reset()
isInitialized = false;
defaultProvider = new MeterProvider();
}

/// <summary>
/// Releases the unmanaged resources used by this class and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
}
}
}
18 changes: 1 addition & 17 deletions src/OpenTelemetry.Api/Trace/TracerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
// limitations under the License.
// </copyright>

using System;
using System.Diagnostics;

namespace OpenTelemetry.Trace
{
/// <summary>
/// TracerProvider is the entry point of the OpenTelemetry API. It provides access to <see cref="Tracer"/>.
/// </summary>
public class TracerProvider : IDisposable
public class TracerProvider : BaseProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="TracerProvider"/> class.
Expand Down Expand Up @@ -51,20 +50,5 @@ public Tracer GetTracer(string name, string version = null)

return new Tracer(new ActivitySource(name, version));
}

/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases the unmanaged resources used by this class and optionally releases the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
}
}
}
3 changes: 1 addition & 2 deletions src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
using OpenTelemetry.Logs;
#endif
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter
{
Expand Down Expand Up @@ -99,7 +98,7 @@ public override ExportResult Export(in Batch<T> batch)
}
}

var resource = activity.GetResource();
var resource = this.ParentProvider.GetResource();
if (resource != Resource.Empty)
{
this.WriteLine("Resource associated with Activity:");
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 OnParentProviderSet()
{
this.SetResource(this.ParentProvider.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 OnParentProviderSet()
{
this.SetResource(this.ParentProvider.GetResource());
}
}
}
Loading