Skip to content
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
@@ -0,0 +1,82 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;

namespace Azure.Messaging.EventGrid
{
internal class CloudEventRequestContent : RequestContent
Comment thread
JoshLove-msft marked this conversation as resolved.
{
private IEnumerable<CloudEvent> _cloudEvents;
private const string TraceParentHeaderName = "traceparent";
private const string TraceStateHeaderName = "tracestate";
private byte[] _data;

public CloudEventRequestContent(IEnumerable<CloudEvent> cloudEvents)
{
_cloudEvents = cloudEvents;
}

public override void Dispose()
{
}

public override bool TryComputeLength(out long length)
{
EnsureSerialized();
length = _data.Length;
return true;
}

public override void WriteTo(Stream stream, CancellationToken cancellationToken)
{
EnsureSerialized();
stream.Write(_data, 0, _data.Length);
}

public override async Task WriteToAsync(Stream stream, CancellationToken cancellationToken)
{
EnsureSerialized();
await stream.WriteAsync(_data, 0, _data.Length, cancellationToken).ConfigureAwait(false);
}

private void EnsureSerialized()
{
if (_data != null)
{
return;
}

string currentActivityId = null;
string traceState = null;
Activity currentActivity = Activity.Current;
Comment thread
JoshLove-msft marked this conversation as resolved.
if (currentActivity != null && currentActivity.IsW3CFormat())
{
currentActivityId = currentActivity.Id;
currentActivity.TryGetTraceState(out traceState);
}

foreach (CloudEvent cloudEvent in _cloudEvents)
{
if (currentActivityId != null &&
!cloudEvent.ExtensionAttributes.ContainsKey(TraceParentHeaderName) &&
!cloudEvent.ExtensionAttributes.ContainsKey(TraceStateHeaderName))
{
cloudEvent.ExtensionAttributes.Add(TraceParentHeaderName, currentActivityId);
if (traceState != null)
{
cloudEvent.ExtensionAttributes.Add(TraceStateHeaderName, traceState);
}
}
}
_data = JsonSerializer.SerializeToUtf8Bytes(_cloudEvents, typeof(List<CloudEvent>));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -28,9 +27,6 @@ public class EventGridPublisherClient
private readonly HttpPipeline _pipeline;
private readonly string _apiVersion;

private const string TraceParentHeaderName = "traceparent";
private const string TraceStateHeaderName = "tracestate";

private static readonly JsonObjectSerializer s_jsonSerializer = new JsonObjectSerializer();

/// <summary>Initalizes a new instance of the <see cref="EventGridPublisherClient"/> class for mocking.</summary>
Expand Down Expand Up @@ -235,46 +231,9 @@ private async Task<Response> SendCloudEventsInternal(IEnumerable<CloudEvent> eve
{
// List of events cannot be null
Argument.AssertNotNull(events, nameof(events));

string activityId = null;
string traceState = null;
Activity currentActivity = Activity.Current;
if (currentActivity != null && currentActivity.IsW3CFormat())
{
activityId = currentActivity.Id;
currentActivity.TryGetTraceState(out traceState);
}

foreach (CloudEvent cloudEvent in events)
{
// Individual events cannot be null
Argument.AssertNotNull(cloudEvent, nameof(cloudEvent));

if (activityId != null &&
!cloudEvent.ExtensionAttributes.ContainsKey(TraceParentHeaderName) &&
!cloudEvent.ExtensionAttributes.ContainsKey(TraceStateHeaderName))
{
cloudEvent.ExtensionAttributes.Add(TraceParentHeaderName, activityId);
if (traceState != null)
{
cloudEvent.ExtensionAttributes.Add(TraceStateHeaderName, traceState);
}
}
}
using HttpMessage message = _pipeline.CreateMessage();
Request request = CreateEventRequest(message, "application/cloudevents-batch+json; charset=utf-8");

BinaryData data;
if (async)
{
data = await s_jsonSerializer.SerializeAsync(events, typeof(List<CloudEvent>), cancellationToken).ConfigureAwait(false);
}
else
{
data = s_jsonSerializer.Serialize(events, typeof(List<CloudEvent>), cancellationToken);
}

RequestContent content = RequestContent.Create(data.ToMemory());
CloudEventRequestContent content = new CloudEventRequestContent(events);
request.Content = content;

if (async)
Expand Down
Loading