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

Added ServiceName option to OtlpExporter #1420

Merged
merged 6 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions examples/AspNetCore/Examples.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.AspNetCore\OpenTelemetry.Instrumentation.AspNetCore.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Extensions.Hosting\OpenTelemetry.Extensions.Hosting.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.Http\OpenTelemetry.Instrumentation.Http.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.OpenTelemetryProtocol\OpenTelemetry.Exporter.OpenTelemetryProtocol.csproj" />
</ItemGroup>


Expand Down
10 changes: 10 additions & 0 deletions examples/AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public void ConfigureServices(IServiceCollection services)
zipkinOptions.Endpoint = new Uri(this.Configuration.GetValue<string>("Zipkin:Endpoint"));
}));
break;
case "otlp":
services.AddOpenTelemetryTracing((builder) => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddOtlpExporter(otlpOptions =>
{
otlpOptions.ServiceName = this.Configuration.GetValue<string>("Otlp:ServiceName");
otlpOptions.Endpoint = this.Configuration.GetValue<string>("Otlp:Endpoint");
}));
break;
default:
services.AddOpenTelemetryTracing((builder) => builder
.AddAspNetCoreInstrumentation()
Expand Down
6 changes: 5 additions & 1 deletion examples/AspNetCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
},
"AllowedHosts": "*",
"UseExporter": "console",
"UseExporter": "console",
"Jaeger": {
"ServiceName": "jaeger-test",
"Host": "localhost",
Expand All @@ -16,5 +16,9 @@
"Zipkin": {
"ServiceName": "zipkin-test",
"Endpoint": "http://localhost:9411/api/v2/spans"
},
"Otlp": {
"ServiceName": "otlp-test",
"Endpoint": "localhost:55680"
}
}
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
following the [Zipkin remote endpoint
rules](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk_exporters/zipkin.md#remote-endpoint)
([#1392](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1392))
* Added `ServiceName` to options available on the `AddOtlpExporter` extension
([#1420](https://github.com/open-telemetry/opentelemetry-dotnet/pull/1420))

## 0.7.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
using Google.Protobuf;
using Google.Protobuf.Collections;
using OpenTelemetry.Internal;
using OpenTelemetry.Resources;
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 @@ -39,10 +37,10 @@ internal static class ActivityExtensions
private static readonly ConcurrentBag<OtlpTrace.InstrumentationLibrarySpans> SpanListPool = new ConcurrentBag<OtlpTrace.InstrumentationLibrarySpans>();
private static readonly Action<RepeatedField<OtlpTrace.Span>, int> RepeatedFieldOfSpanSetCountAction = CreateRepeatedFieldOfSpanSetCountAction();
private static readonly Func<byte[], ByteString> ByteStringCtorFunc = CreateByteStringCtorFunc();
private static OtlpResource.Resource processResource;

internal static void AddBatch(
this OtlpCollector.ExportTraceServiceRequest request,
OtlpExporter otlpExporter,
in Batch<Activity> activityBatch)
{
Dictionary<string, OtlpTrace.InstrumentationLibrarySpans> spansByLibrary = new Dictionary<string, OtlpTrace.InstrumentationLibrarySpans>();
Expand All @@ -52,14 +50,10 @@ internal static void AddBatch(
{
if (resourceSpans == null)
{
resourceSpans = new OtlpTrace.ResourceSpans();

if (processResource == null)
resourceSpans = new OtlpTrace.ResourceSpans
{
BuildProcessResource(activity.GetResource());
}

resourceSpans.Resource = processResource;
Resource = otlpExporter.EnsureProcessResource(activity),
};
request.ResourceSpans.Add(resourceSpans);
}

Expand Down Expand Up @@ -101,22 +95,6 @@ internal static void Return(this OtlpCollector.ExportTraceServiceRequest request
}
}

internal static void BuildProcessResource(Resource resource)
{
OtlpResource.Resource processResource = new OtlpResource.Resource();

foreach (KeyValuePair<string, object> attribute in resource.Attributes)
{
var oltpAttribute = ToOtlpAttribute(attribute);
if (oltpAttribute != null)
{
processResource.Attributes.Add(oltpAttribute);
}
}

ActivityExtensions.processResource = processResource;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpTrace.InstrumentationLibrarySpans GetSpanListFromPool(string name, string version)
{
Expand Down Expand Up @@ -227,6 +205,41 @@ internal static OtlpTrace.Span ToOtlpSpan(this Activity activity)
return otlpSpan;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpCommon.KeyValue ToOtlpAttribute(this KeyValuePair<string, object> kvp)
{
if (kvp.Value == null)
{
return null;
}

var attrib = new OtlpCommon.KeyValue { Key = kvp.Key, Value = new OtlpCommon.AnyValue { } };

switch (kvp.Value)
{
case string s:
attrib.Value.StringValue = s;
break;
case bool b:
attrib.Value.BoolValue = b;
break;
case int i:
attrib.Value.IntValue = i;
break;
case long l:
attrib.Value.IntValue = l;
break;
case double d:
attrib.Value.DoubleValue = d;
break;
default:
attrib.Value.StringValue = kvp.Value.ToString();
break;
}

return attrib;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static OtlpTrace.Status ToOtlpStatus(ref TagEnumerationState otlpTags)
{
Expand Down Expand Up @@ -308,116 +321,6 @@ private static OtlpTrace.Span.Types.Event ToOtlpEvent(ActivityEvent activityEven
return otlpEvent;
}

private static OtlpCommon.KeyValue ToOtlpAttribute(KeyValuePair<string, object> kvp)
{
if (kvp.Value == null)
{
return null;
}

var attrib = new OtlpCommon.KeyValue { Key = kvp.Key, Value = new OtlpCommon.AnyValue { } };

switch (kvp.Value)
{
case string s:
attrib.Value.StringValue = s;
break;
case bool b:
attrib.Value.BoolValue = b;
break;
case int i:
attrib.Value.IntValue = i;
break;
case long l:
attrib.Value.IntValue = l;
break;
case double d:
attrib.Value.DoubleValue = d;
break;
default:
attrib.Value.StringValue = kvp.Value.ToString();
break;
}

return attrib;
}

private static List<OtlpCommon.KeyValue> ToOtlpAttributes(KeyValuePair<string, object> kvp)
{
if (kvp.Value == null)
{
return null;
}

var attributes = new List<OtlpCommon.KeyValue>();
var attrib = new OtlpCommon.KeyValue { Key = kvp.Key, Value = new OtlpCommon.AnyValue { } };
switch (kvp.Value)
{
case string s:
attrib.Value.StringValue = s;
attributes.Add(attrib);
break;
case bool b:
attrib.Value.BoolValue = b;
attributes.Add(attrib);
break;
case int i:
attrib.Value.IntValue = i;
attributes.Add(attrib);
break;
case long l:
attrib.Value.IntValue = l;
attributes.Add(attrib);
break;
case double d:
attrib.Value.DoubleValue = d;
attributes.Add(attrib);
break;
case int[] intArray:
foreach (var item in intArray)
{
attrib = new OtlpCommon.KeyValue { Key = kvp.Key, Value = new OtlpCommon.AnyValue { } };
attrib.Value.IntValue = item;
attributes.Add(attrib);
}

break;
case double[] doubleArray:
foreach (var item in doubleArray)
{
attrib = new OtlpCommon.KeyValue { Key = kvp.Key, Value = new OtlpCommon.AnyValue { } };
attrib.Value.DoubleValue = item;
attributes.Add(attrib);
}

break;
case bool[] boolArray:
foreach (var item in boolArray)
{
attrib = new OtlpCommon.KeyValue { Key = kvp.Key, Value = new OtlpCommon.AnyValue { } };
attrib.Value.BoolValue = item;
attributes.Add(attrib);
}

break;
case string[] stringArray:
foreach (var item in stringArray)
{
attrib = new OtlpCommon.KeyValue { Key = kvp.Key, Value = new OtlpCommon.AnyValue { } };
attrib.Value.StringValue = item;
attributes.Add(attrib);
}

break;
default:
attrib.Value.StringValue = kvp.Value.ToString();
attributes.Add(attrib);
break;
}

return attributes;
}

private static Action<RepeatedField<OtlpTrace.Span>, int> CreateRepeatedFieldOfSpanSetCountAction()
{
FieldInfo repeatedFieldOfSpanCountField = typeof(RepeatedField<OtlpTrace.Span>).GetField("count", BindingFlags.NonPublic | BindingFlags.Instance);
Expand Down
51 changes: 49 additions & 2 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
// </copyright>

using System;
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;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OtlpCollector = Opentelemetry.Proto.Collector.Trace.V1;
using OtlpCommon = Opentelemetry.Proto.Common.V1;
using OtlpResource = Opentelemetry.Proto.Resource.V1;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol
{
Expand All @@ -29,9 +36,11 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol
/// </summary>
public class OtlpExporter : BaseExporter<Activity>
{
private readonly OtlpExporterOptions options;
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 @@ -40,7 +49,8 @@ public class OtlpExporter : BaseExporter<Activity>
/// <param name="traceServiceClient"><see cref="OtlpCollector.TraceService.TraceServiceClient"/>.</param>
internal OtlpExporter(OtlpExporterOptions options, OtlpCollector.TraceService.ITraceServiceClient traceServiceClient = null)
{
this.headers = options?.Headers ?? throw new ArgumentNullException(nameof(options));
this.options = options ?? throw new ArgumentNullException(nameof(options));
this.headers = options.Headers ?? throw new ArgumentException("Headers were not provided on options.", nameof(options));
if (traceServiceClient != null)
{
this.traceClient = traceServiceClient;
Expand All @@ -57,7 +67,7 @@ public override ExportResult Export(in Batch<Activity> activityBatch)
{
OtlpCollector.ExportTraceServiceRequest request = new OtlpCollector.ExportTraceServiceRequest();

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

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

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

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

foreach (KeyValuePair<string, object> attribute in activity.GetResource().Attributes)
{
var oltpAttribute = attribute.ToOtlpAttribute();
if (oltpAttribute != null)
{
processResource.Attributes.Add(oltpAttribute);
}
}

if (!processResource.Attributes.Any(kvp => kvp.Key == Resource.ServiceNameKey))
{
string serviceName = this.options.ServiceName;
if (string.IsNullOrEmpty(serviceName))
{
serviceName = OtlpExporterOptions.DefaultServiceName;
}

processResource.Attributes.Add(new OtlpCommon.KeyValue
{
Key = Resource.ServiceNameKey,
Value = new OtlpCommon.AnyValue { StringValue = serviceName },
});
}

return this.processResource = processResource;
}

/// <inheritdoc/>
protected override bool OnShutdown(int timeoutMilliseconds)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol
/// </summary>
public class OtlpExporterOptions
{
internal const string DefaultServiceName = "OpenTelemetry Exporter";

/// <summary>
/// Gets or sets the name of the service reporting telemetry. Default value: OpenTelemetry Exporter.
/// </summary>
public string ServiceName { get; set; } = DefaultServiceName;

/// <summary>
/// Gets or sets the target to which the exporter is going to send traces or metrics.
/// The valid syntax is described at https://github.com/grpc/grpc/blob/master/doc/naming.md.
Expand Down
Loading