Skip to content

Commit

Permalink
Merge branch 'main' into yunl/process-template
Browse files Browse the repository at this point in the history
  • Loading branch information
Yun-Ting authored Feb 22, 2023
2 parents 8c6ceb3 + e080a57 commit 416156c
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 113 deletions.
2 changes: 1 addition & 1 deletion examples/grpc.core/Examples.GrpcCore.AspNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
tcs.SetResult(true);
});

return tcs.Task.ContinueWith(antecedent => tokenRegistration.Dispose());
return tcs.Task.ContinueWith(antecedent => tokenRegistration.Dispose(), stoppingToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.StackdriverTraceExporter(string projectId) -> void
OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils
OpenTelemetry.Trace.TracerProviderBuilderExtensions
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batchActivity) -> OpenTelemetry.ExportResult
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetDefaultResource(string projectId) -> Google.Api.MonitoredResource
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetProjectId() -> string
static OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.Default.get -> OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter
OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.StackdriverTraceExporter(string projectId) -> void
OpenTelemetry.Exporter.Stackdriver.Utils.CommonUtils
OpenTelemetry.Trace.TracerProviderBuilderExtensions
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batchActivity) -> OpenTelemetry.ExportResult
override OpenTelemetry.Exporter.Stackdriver.StackdriverTraceExporter.Export(in OpenTelemetry.Batch<System.Diagnostics.Activity> batch) -> OpenTelemetry.ExportResult
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetDefaultResource(string projectId) -> Google.Api.MonitoredResource
static OpenTelemetry.Exporter.Stackdriver.Implementation.GoogleCloudResourceUtils.GetProjectId() -> string
static OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration.Default.get -> OpenTelemetry.Exporter.Stackdriver.Implementation.StackdriverStatsConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Google.Cloud.Trace.V2;
using Google.Protobuf.WellKnownTypes;
Expand All @@ -27,7 +28,7 @@ namespace OpenTelemetry.Exporter.Stackdriver.Implementation;

internal static class ActivityExtensions
{
private static Dictionary<string, string> labelsToReplace = new Dictionary<string, string>
private static readonly Dictionary<string, string> LabelsToReplace = new Dictionary<string, string>
{
{ "component", "/component" },
{ "http.method", "/http/method" },
Expand Down Expand Up @@ -94,7 +95,7 @@ public static Span ToSpan(this Activity activity, string projectId)

// StackDriver uses different labels that are used to categorize spans
// replace attribute keys with StackDriver version
foreach (var entry in labelsToReplace)
foreach (var entry in LabelsToReplace)
{
if (span.Attributes.AttributeMap.TryGetValue(entry.Key, out var attrValue))
{
Expand Down Expand Up @@ -146,7 +147,7 @@ public static AttributeValue ToAttributeValue(this object? av)
case double d:
return new AttributeValue()
{
StringValue = new TruncatableString() { Value = d.ToString() },
StringValue = new TruncatableString() { Value = d.ToString(CultureInfo.InvariantCulture) },
};
case null:
return new AttributeValue();
Expand Down
39 changes: 20 additions & 19 deletions src/OpenTelemetry.Exporter.Stackdriver/Implementation/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,31 @@ namespace OpenTelemetry.Exporter.Stackdriver.Implementation;

internal class Constants
{
public static readonly string PackagVersionUndefined = "undefined";
public const string PackagVersionUndefined = "undefined";

public static readonly string LabelDescription = "OpenTelemetry string";
public static readonly string OpenTelemetryTask = "OpenTelemetry_task";
public static readonly string OpenTelemetryTaskDescription = "OpenTelemetry task identifier";
public const string LabelDescription = "OpenTelemetry string";
public const string OpenTelemetryTask = "OpenTelemetry_task";
public const string OpenTelemetryTaskDescription = "OpenTelemetry task identifier";

public static readonly string GcpGkeContainer = "k8s_container";
public static readonly string GcpGceInstance = "gce_instance";
public static readonly string AwsEc2Instance = "aws_ec2_instance";
public static readonly string Global = "global";
public const string GcpGkeContainer = "k8s_container";
public const string GcpGceInstance = "gce_instance";
public const string AwsEc2Instance = "aws_ec2_instance";
public const string Global = "global";

public static readonly string ProjectIdLabelKey = "project_id";
public static readonly string OpenTelemetryTaskValueDefault = GenerateDefaultTaskValue();
public const string ProjectIdLabelKey = "project_id";

public const string GceGcpInstanceType = "cloud.google.com/gce/instance";
public const string GcpInstanceIdKey = "cloud.google.com/gce/instance_id";
public const string GcpAccountIdKey = "cloud.google.com/gce/project_id";
public const string GcpZoneKey = "cloud.google.com/gce/zone";

public static readonly string GceGcpInstanceType = "cloud.google.com/gce/instance";
public static readonly string GcpInstanceIdKey = "cloud.google.com/gce/instance_id";
public static readonly string GcpAccountIdKey = "cloud.google.com/gce/project_id";
public static readonly string GcpZoneKey = "cloud.google.com/gce/zone";
public const string K8sContainerType = "k8s.io/container";
public const string K8sClusterNameKey = "k8s.io/cluster/name";
public const string K8sContainerNameKey = "k8s.io/container/name";
public const string K8sNamespaceNameKey = "k8s.io/namespace/name";
public const string K8sPodNameKey = "k8s.io/pod/name";

public static readonly string K8sContainerType = "k8s.io/container";
public static readonly string K8sClusterNameKey = "k8s.io/cluster/name";
public static readonly string K8sContainerNameKey = "k8s.io/container/name";
public static readonly string K8sNamespaceNameKey = "k8s.io/namespace/name";
public static readonly string K8sPodNameKey = "k8s.io/pod/name";
public static readonly string OpenTelemetryTaskValueDefault = GenerateDefaultTaskValue();

private static string GenerateDefaultTaskValue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public class StackdriverTraceExporter : BaseExporter<Activity>
private readonly TraceServiceSettings traceServiceSettings;
private readonly TraceServiceClient traceServiceClient;

#pragma warning disable CA1810 // Initialize reference type static fields inline
static StackdriverTraceExporter()
#pragma warning restore CA1810 // Initialize reference type static fields inline
{
try
{
Expand Down Expand Up @@ -87,7 +89,7 @@ internal StackdriverTraceExporter(string projectId, TraceServiceClient traceServ
}

/// <inheritdoc/>
public override ExportResult Export(in Batch<Activity> batchActivity)
public override ExportResult Export(in Batch<Activity> batch)
{
TraceServiceClient traceWriter = this.traceServiceClient;
if (this.traceServiceClient == null)
Expand All @@ -103,7 +105,7 @@ public override ExportResult Export(in Batch<Activity> batchActivity)
ProjectName = this.googleCloudProjectId,
};

foreach (var activity in batchActivity)
foreach (var activity in batch)
{
// It should never happen that the time has no correct kind, only if OpenTelemetry is used incorrectly.
if (activity.StartTimeUtc.Kind == DateTimeKind.Utc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public static TracerProviderBuilder UseStackdriverExporter(
{
Guard.ThrowIfNull(builder);

#pragma warning disable CA2000 // Dispose objects before losing scope
var activityExporter = new StackdriverTraceExporter(projectId);

return builder.AddProcessor(new BatchActivityExportProcessor(activityExporter));
#pragma warning restore CA2000 // Dispose objects before losing scope
}
}
5 changes: 5 additions & 0 deletions src/OpenTelemetry.Exporter.Stackdriver/Utils/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static class CommonUtils
/// <returns><see cref="IEnumerable{T}"/>.</returns>
public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> source, int size)
{
if (source == null)
{
throw new System.ArgumentNullException(nameof(source));
}

using var enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Core.Interceptors;
using OpenTelemetry.Context.Propagation;
Expand Down Expand Up @@ -52,6 +53,9 @@ public override TResponse BlockingUnaryCall<TRequest, TResponse>(
ClientInterceptorContext<TRequest, TResponse> context,
BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
Expand Down Expand Up @@ -81,11 +85,16 @@ public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
ClientInterceptorContext<TRequest, TResponse> context,
AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
rpcScope.RecordRequest(request);
var responseContinuation = continuation(request, rpcScope.Context);
var responseAsync = responseContinuation.ResponseAsync.ContinueWith(
Expand All @@ -103,7 +112,8 @@ public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
rpcScope.CompleteWithException(ex.InnerException);
throw ex.InnerException;
}
});
},
TaskScheduler.Current);

return new AsyncUnaryCall<TResponse>(
responseAsync,
Expand All @@ -128,11 +138,16 @@ public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreami
ClientInterceptorContext<TRequest, TResponse> context,
AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
var responseContinuation = continuation(rpcScope.Context);
var clientRequestStreamProxy = new ClientStreamWriterProxy<TRequest>(
responseContinuation.RequestStream,
Expand All @@ -154,7 +169,8 @@ public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreami
rpcScope.CompleteWithException(ex.InnerException);
throw ex.InnerException;
}
});
},
TaskScheduler.Current);

return new AsyncClientStreamingCall<TRequest, TResponse>(
clientRequestStreamProxy,
Expand All @@ -181,11 +197,16 @@ public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRe
ClientInterceptorContext<TRequest, TResponse> context,
AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
rpcScope.RecordRequest(request);
var responseContinuation = continuation(request, rpcScope.Context);

Expand Down Expand Up @@ -218,11 +239,16 @@ public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreami
ClientInterceptorContext<TRequest, TResponse> context,
AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

ClientRpcScope<TRequest, TResponse> rpcScope = null;

try
{
#pragma warning disable CA2000
rpcScope = new ClientRpcScope<TRequest, TResponse>(context, this.options);
#pragma warning restore CA2000
var responseContinuation = continuation(rpcScope.Context);

var requestStreamProxy = new ClientStreamWriterProxy<TRequest>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ClientTracingInterceptorOptions
/// <summary>
/// Gets or sets a value indicating whether or not to record individual message events.
/// </summary>
public bool RecordMessageEvents { get; set; } = false;
public bool RecordMessageEvents { get; set; }

/// <summary>
/// Gets the propagator.
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Instrumentation.GrpcCore/RpcScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal abstract class RpcScope<TRequest, TResponse> : IDisposable
/// <summary>
/// The complete flag.
/// </summary>
private long complete = 0;
private long complete;

/// <summary>
/// The request message counter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
ServerCallContext context,
UnaryServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand All @@ -77,6 +80,9 @@ public override async Task<TResponse> ClientStreamingServerHandler<TRequest, TRe
ServerCallContext context,
ClientStreamingServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand Down Expand Up @@ -104,6 +110,9 @@ public override async Task ServerStreamingServerHandler<TRequest, TResponse>(
ServerCallContext context,
ServerStreamingServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand All @@ -127,6 +136,9 @@ public override async Task ServerStreamingServerHandler<TRequest, TResponse>(
/// <inheritdoc/>
public override async Task DuplexStreamingServerHandler<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream, IServerStreamWriter<TResponse> responseStream, ServerCallContext context, DuplexStreamingServerMethod<TRequest, TResponse> continuation)
{
Guard.ThrowIfNull(context);
Guard.ThrowIfNull(continuation);

using var rpcScope = new ServerRpcScope<TRequest, TResponse>(context, this.options);

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ServerTracingInterceptorOptions
/// <summary>
/// Gets or sets a value indicating whether or not to record individual message events.
/// </summary>
public bool RecordMessageEvents { get; set; } = false;
public bool RecordMessageEvents { get; set; }

/// <summary>
/// Gets the propagator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace OpenTelemetry.Instrumentation.Hangfire.Implementation;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;

internal class HangfireInstrumentationJobFilterAttribute : JobFilterAttribute, IServerFilter, IClientFilter
internal sealed class HangfireInstrumentationJobFilterAttribute : JobFilterAttribute, IServerFilter, IClientFilter
{
private readonly HangfireInstrumentationOptions options;

Expand All @@ -37,6 +37,8 @@ public HangfireInstrumentationJobFilterAttribute(HangfireInstrumentationOptions
this.options = options;
}

public HangfireInstrumentationOptions Options { get; }

public void OnPerforming(PerformingContext performingContext)
{
// Short-circuit if nobody is listening
Expand Down Expand Up @@ -123,7 +125,7 @@ private static void InjectActivityProperties(IDictionary<string, string> jobPara

private static IEnumerable<string> ExtractActivityProperties(Dictionary<string, string> telemetryData, string key)
{
return telemetryData.ContainsKey(key) ? new[] { telemetryData[key] } : Enumerable.Empty<string>();
return telemetryData.TryGetValue(key, out var value) ? new[] { value } : Enumerable.Empty<string>();
}

private void SetStatusAndRecordException(Activity activity, Exception exception)
Expand Down
Loading

0 comments on commit 416156c

Please sign in to comment.