Skip to content

Commit

Permalink
update to official CloudEvent proto
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan committed Dec 3, 2024
1 parent 108e5e7 commit d760686
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Octokit.Webhooks.Events.IssueComment;
using Octokit.Webhooks.Events.Issues;
using Octokit.Webhooks.Models;
using CloudNative.CloudEvents.V1;

namespace DevTeam.Backend;

Expand Down
1 change: 1 addition & 0 deletions dotnet/src/Microsoft.AutoGen/Abstractions/IAgentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// IAgentBase.cs

using Google.Protobuf;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Abstractions;

Expand Down
1 change: 1 addition & 0 deletions dotnet/src/Microsoft.AutoGen/Abstractions/IAgentRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// IAgentRuntime.cs

using System.Diagnostics;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Abstractions;

Expand Down
1 change: 1 addition & 0 deletions dotnet/src/Microsoft.AutoGen/Abstractions/IAgentWorker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// IAgentWorker.cs
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Abstractions;

Expand Down
3 changes: 2 additions & 1 deletion dotnet/src/Microsoft.AutoGen/Agents/AgentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Google.Protobuf;
using Microsoft.AutoGen.Abstractions;
using Microsoft.Extensions.Logging;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Agents;

Expand Down Expand Up @@ -104,7 +105,7 @@ protected internal async Task HandleRpcMessage(Message msg, CancellationToken ca
{
case Message.MessageOneofCase.CloudEvent:
{
var activity = this.ExtractActivity(msg.CloudEvent.Type, msg.CloudEvent.Metadata);
var activity = this.ExtractActivity(msg.CloudEvent.Type, msg.CloudEvent.Attributes);
await this.InvokeWithActivityAsync(
static ((AgentBase Agent, CloudEvent Item) state, CancellationToken _) => state.Agent.CallHandler(state.Item),
(this, msg.CloudEvent),
Expand Down
6 changes: 6 additions & 0 deletions dotnet/src/Microsoft.AutoGen/Agents/AgentBaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// AgentBaseExtensions.cs

using System.Diagnostics;
using Google.Protobuf.Collections;

namespace Microsoft.AutoGen.Agents;

Expand Down Expand Up @@ -57,6 +58,11 @@ public static class AgentBaseExtensions
return activity;
}

public static Activity? ExtractActivity(this AgentBase agent, string activityName, MapField<string, CloudNative.CloudEvents.V1.CloudEvent.Types.CloudEventAttributeValue> metadata)
{
return ExtractActivity(agent, activityName, metadata.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.CeString));
}

/// <summary>
/// Invokes a function asynchronously within the context of an <see cref="Activity"/>.
/// </summary>
Expand Down
33 changes: 32 additions & 1 deletion dotnet/src/Microsoft.AutoGen/Agents/AgentRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using System.Diagnostics;
using Microsoft.AutoGen.Abstractions;
using Microsoft.Extensions.Logging;
using CloudNative.CloudEvents.V1;
using Google.Protobuf.Collections;
using static CloudNative.CloudEvents.V1.CloudEvent.Types;

namespace Microsoft.AutoGen.Agents;

Expand All @@ -28,13 +31,28 @@ internal sealed class AgentRuntime(AgentId agentId, IAgentWorker worker, ILogger
out var traceState);
return (traceParent, traceState);
}
public (string?, string?) GetTraceIdAndState(MapField<string, CloudEventAttributeValue> metadata)
{
DistributedContextPropagator.ExtractTraceIdAndState(metadata,
static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>
{
var metadata = (MapField<string, CloudEventAttributeValue>)carrier!;
fieldValues = null;
metadata.TryGetValue(fieldName, out var ceValue);
fieldValue = ceValue?.CeString;
},
out var traceParent,
out var traceState);
return (traceParent, traceState);
}
public void Update(RpcRequest request, Activity? activity = null)
{
DistributedContextPropagator.Inject(activity, request.Metadata, static (carrier, key, value) => ((IDictionary<string, string>)carrier!)[key] = value);
}
public void Update(CloudEvent cloudEvent, Activity? activity = null)
{
DistributedContextPropagator.Inject(activity, cloudEvent.Metadata, static (carrier, key, value) => ((IDictionary<string, string>)carrier!)[key] = value);
DistributedContextPropagator.Inject(activity, cloudEvent.Attributes, static (carrier, key, value) =>
((MapField<string, CloudEventAttributeValue>)carrier!)[key].CeString = value);
}
public async ValueTask SendResponseAsync(RpcRequest request, RpcResponse response, CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -73,4 +91,17 @@ public IDictionary<string, string> ExtractMetadata(IDictionary<string, string> m

return baggage as IDictionary<string, string> ?? new Dictionary<string, string>();
}

public IDictionary<string, string> ExtractMetadata(MapField<string, CloudEventAttributeValue> metadata)
{
var baggage = DistributedContextPropagator.ExtractBaggage(metadata, static (object? carrier, string fieldName, out string? fieldValue, out IEnumerable<string>? fieldValues) =>
{
var metadata = (MapField<string, CloudEventAttributeValue>)carrier!;
fieldValues = null;
metadata.TryGetValue(fieldName, out var ceValue);
fieldValue = ceValue?.CeString;
});

return baggage as IDictionary<string, string> ?? new Dictionary<string, string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Agents;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Agents;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AutoGen.Abstractions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Agents;

Expand Down
1 change: 1 addition & 0 deletions dotnet/src/Microsoft.AutoGen/Agents/Services/IGateway.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// IGateway.cs
using Microsoft.AutoGen.Abstractions;
using CloudNative.CloudEvents.V1;

namespace Microsoft.AutoGen.Agents;

Expand Down

0 comments on commit d760686

Please sign in to comment.