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

Handle potential NullReferenceException in DSM #4744

Merged
merged 2 commits into from
Oct 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

using Datadog.Trace.DuckTyping;

namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Kafka
{
/// <summary>
/// Message interface for duck-typing
/// </summary>
internal interface IMessage
internal interface IMessage : IDuckType
{
/// <summary>
/// Gets the key of the message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#nullable enable

using System;
using System.Text;
using Datadog.Trace.DataStreamsMonitoring;
Expand All @@ -24,14 +26,14 @@ internal static class KafkaHelper
private static bool _headersInjectionEnabled = true;
private static string[] defaultProduceEdgeTags = new[] { "direction:out", "type:kafka" };

internal static Scope CreateProducerScope(
internal static Scope? CreateProducerScope(
Tracer tracer,
object producer,
ITopicPartition topicPartition,
bool isTombstone,
bool finishOnClose)
{
Scope scope = null;
Scope? scope = null;

try
{
Expand Down Expand Up @@ -125,7 +127,7 @@ private static long GetMessageSize<T>(T message)
return size;
}

internal static Scope CreateConsumerScope(
internal static Scope? CreateConsumerScope(
Tracer tracer,
DataStreamsManager dataStreamsManager,
object consumer,
Expand All @@ -134,7 +136,7 @@ internal static Scope CreateConsumerScope(
Offset? offset,
IMessage message)
{
Scope scope = null;
Scope? scope = null;

try
{
Expand All @@ -153,7 +155,7 @@ internal static Scope CreateConsumerScope(
return null;
}

SpanContext propagatedContext = null;
SpanContext? propagatedContext = null;
PathwayContext? pathwayContext = null;

// Try to extract propagated context from headers
Expand Down Expand Up @@ -269,7 +271,7 @@ internal static Scope CreateConsumerScope(
dataStreamsManager,
CheckpointKind.Consume,
edgeTags,
GetMessageSize(message),
message is null ? 0 : GetMessageSize(message),
tags.MessageQueueTimeMs == null ? 0 : (long)tags.MessageQueueTimeMs);
}
}
Expand Down Expand Up @@ -302,7 +304,7 @@ internal static void CloseConsumerScope(Tracer tracer)
}

// TODO: record end-to-end time?
activeScope.Dispose();
activeScope!.Dispose();
}
catch (Exception ex)
{
Expand All @@ -326,7 +328,7 @@ internal static void TryInjectHeaders<TTopicPartitionMarker, TMessage>(
TMessage message)
where TMessage : IMessage
{
if (!_headersInjectionEnabled)
if (!_headersInjectionEnabled || message.Instance is null)
{
return;
}
Expand Down