Skip to content

Commit

Permalink
Simplify Azure Event Grid service profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ciprianjichici committed Dec 23, 2024
1 parent 275410b commit e76710e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/dotnet/Common/Interfaces/IEventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public interface IEventService
/// <summary>
/// Sends an event to the event service.
/// </summary>
/// <param name="eventCategory">The category of the event.</param>
/// <param name="topicName">The name of the topic where the event should be sent.</param>
/// <param name="cloudEvent">The <see cref="CloudEvent"/> object containing the details of the event.</param>
/// <returns></returns>
Task SendEvent(string eventCategory, CloudEvent cloudEvent);
Task SendEvent(string topicName, CloudEvent cloudEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,48 +45,5 @@ public class EventGridTopicProfile
/// </summary>
[JsonIgnore]
public bool SubscriptionAvailable { get; set; }

/// <summary>
/// The list of <see cref="EventGridEventTypeProfile"/> event type profiles used to configure handling for event types.
/// </summary>
public List<EventGridEventTypeProfile> EventTypeProfiles { get; set; } = [];
}

/// <summary>
/// The profile used to configure event handling for a specified Azure Event Grid event type.
/// </summary>
public class EventGridEventTypeProfile
{
/// <summary>
/// The name of the Azure Event Grid event type.
/// </summary>
public required string EventType { get; set; }

/// <summary>
/// The list of <see cref="EventGridEventSet"/> event sets used to configure event handling for a specific subset of events of a specified event type.
/// </summary>
public List<EventGridEventSet> EventSets { get; set; } = [];
}

/// <summary>
/// The event set used to configure event handling for a specific subset of events of a specified event type.
/// </summary>
public class EventGridEventSet
{
/// <summary>
/// The namespace associated with the event set.
/// FoundationaLLM event subscribers can use this to attach handlers that process events from this set.
/// </summary>
public required string Namespace { get; set; }

/// <summary>
/// The event source that defines the set.
/// </summary>
public required string Source { get; set; }

/// <summary>
/// The event subject prefix that defines the set.
/// </summary>
public required string SubjectPrefix { get; set; }
}
}
16 changes: 9 additions & 7 deletions src/dotnet/Common/Services/Events/AzureEventGridEventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ public AzureEventGridEventService(
IHttpClientFactoryService httpClientFactory,
ILogger<AzureEventGridEventService> logger)
{
_serviceInstanceName =
Environment.GetEnvironmentVariable(EnvironmentVariables.AKS_Pod_Name)
_serviceInstanceName = $"https://foundationallm.ai/events/serviceinstances/{Environment.GetEnvironmentVariable(EnvironmentVariables.AKS_Pod_Name)
?? Environment.GetEnvironmentVariable(EnvironmentVariables.ACA_Container_App_Replica_Name)
?? Guid.NewGuid().ToString();
?? Guid.NewGuid().ToString().ToLower()}";

_settings = settingsOptions.Value;
_profile = profileOptions.Value;
Expand Down Expand Up @@ -182,7 +181,7 @@ public void UnsubscribeFromEventTypeEvent(string eventType, EventTypeEventDelega
}

/// <inheritdoc/>
public async Task SendEvent(string eventCategory, CloudEvent cloudEvent)
public async Task SendEvent(string topicName, CloudEvent cloudEvent)
{
if (!_active)
{
Expand All @@ -191,10 +190,13 @@ public async Task SendEvent(string eventCategory, CloudEvent cloudEvent)
return;
}

if (!_senderClients.TryGetValue(eventCategory, out var senderClient))
// Enforce the correct event source when sending through the Azure Event Grid service.
cloudEvent.Source = _serviceInstanceName;

if (!_senderClients.TryGetValue(topicName, out var senderClient))
{
_logger.LogError("Could not send event {EventId} of type {EventType} from source {EventSource}. The Azure Event Grid event service does not have a sender client for the event category {EventCategory}.",
cloudEvent.Id, cloudEvent.Type, cloudEvent.Source, eventCategory);
_logger.LogError("Could not send event {EventId} of type {EventType} from source {EventSource}. The Azure Event Grid event service does not have a sender client for the topic {EventTopic}.",
cloudEvent.Id, cloudEvent.Type, cloudEvent.Source, topicName);
return;
}

Expand Down

0 comments on commit e76710e

Please sign in to comment.