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

Distributed tracing: Producer #1278

Closed
Closed
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
3 changes: 3 additions & 0 deletions src/Confluent.Kafka/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Confluent.Kafka.IntegrationTests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100cd1e243be8c66588df8c1cec2cd2e81a0df139c996688f657475ea2105783efa6dcb659fff876dc51bc16b68ae3d48a0465e81b6b72e2d3d71e50f680fc2547bc3aa8014b9a862e5e7aa24e0fdc282220c5c8db6084cfb7cb4a503f76768020197cd7d93a215506ad203b7c94a2557ba6140c65935af3c3de1fe1846cac8e6ba")]
3 changes: 2 additions & 1 deletion src/Confluent.Kafka/Confluent.Kafka.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
<PackageReference Include="librdkafka.redist" Version="1.4.2">
<PrivateAssets Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">None</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Memory" Version="4.5.0" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
Expand Down
118 changes: 118 additions & 0 deletions src/Confluent.Kafka/Diagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2016-2018 Confluent Inc.
//
// 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.
//
// Refer to LICENSE for more information.

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace Confluent.Kafka
{
internal static class Diagnostics
{
public const string DiagnosticListenerName = "Confluent.Kafka.Listener";
public const string TraceParentHeaderName = "traceparent";
public const string TraceStateHeaderName = "tracestate";

public static DiagnosticListener DiagnosticSource { get; } = new DiagnosticListener(DiagnosticListenerName);

public static class Producer
{
public const string ActivityName = DiagnosticListenerName + ".MessageProduced";
public const string StartName = ActivityName + ".Start";
public const string StopName = ActivityName + ".Stop";
public const string ExceptionName = ActivityName + ".Exception";

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Activity Start<TKey, TValue>(string topic, Message<TKey, TValue> message)
{
if (DiagnosticSource.IsEnabled(ActivityName, topic))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new recommendation from .NET would be to use ActivitySource and StartActivity, from System.Diagnostics.DiagnosticSource package 5.0.0.
https://github.com/dotnet/designs/pull/98/files#diff-25bba2a21bc64ddeccd374c1c28a0fefR24

DiagnosticSource package supporting the new pattern is releasing stable in Nov 2020, but previews will be available this week onwards.

Should we wait for new diagnosticsource to be available, and then instrument using the new approach?
If instrumented with new approach, then there wouldn't be any need of adapters in OpenTelemetry.
@reyang also to share thoughts on this.

{
Activity activity = new Activity(ActivityName);
activity.Start();

AddActivityStateToHeaders(activity, message.Headers);

if (DiagnosticSource.IsEnabled(StartName))
{
DiagnosticSource.Write(StartName, new { Topic = topic, Message = message });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of writing the new anonymous type, (thus requiring adapters to have knowledge about this type etc), the new recommendation would be to use activity.AddTag("messaging.system", "kafka")

OpenTelemetry has conventions for the tag names, which can be used:
https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md

}

return activity;
}

return null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Stop<TKey, TValue>(string topic, DeliveryResult<TKey, TValue> deliveryResult)
{
if (DiagnosticSource.IsEnabled(StopName))
{
DiagnosticSource.Write(StopName, new { Topic = topic, DeliveryResult = deliveryResult });
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Exception<TKey, TValue>(string topic, Message<TKey, TValue> message, Exception exception)
{
if (DiagnosticSource.IsEnabled(ExceptionName))
{
DiagnosticSource.Write(ExceptionName, new { Topic = topic, Message = message, Exception = exception });
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void StopOrException<TKey, TValue>(string topic, DeliveryReport<TKey, TValue> deliveryReport)
{
if (deliveryReport.Error.IsError)
{
if (DiagnosticSource.IsEnabled(ExceptionName))
{
DiagnosticSource.Write(
ExceptionName,
new
{
Topic = topic,
deliveryReport.Message,
Exception = new ProduceException<TKey, TValue>(deliveryReport.Error, deliveryReport)
});
}
}
else if (DiagnosticSource.IsEnabled(StopName))
{
DiagnosticSource.Write(StopName, new { Topic = topic, DeliveryResult = deliveryReport });
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddActivityStateToHeaders(Activity activity, Headers headers)
{
if (headers?.Any(h => h.Key == TraceParentHeaderName) != true)
{
headers.Add(TraceParentHeaderName, Encoding.UTF8.GetBytes(activity.Id));

string traceState = activity.TraceStateString;
if (traceState != null)
{
headers.Add(TraceStateHeaderName, Encoding.UTF8.GetBytes(traceState));
}
}
}
}
}
}
Loading