0

I'm trying to test a function subscribed to an event grid.

I found the following code that creates a custom event:

string topicEndpoint = "https://<topic-name>.<region>-1.eventgrid.azure.net/api/events";
string topicKey = "<topic-key>";
string topicHostname = new Uri(topicEndpoint).Host;

TopicCredentials topicCredentials = new TopicCredentials(topicKey);
EventGridClient client = new EventGridClient(topicCredentials);

client.PublishEventsAsync(topicHostname, GetEventsList()).GetAwaiter().GetResult();
Console.Write("Published events to Event Grid.");

static IList<EventGridEvent> GetEventsList()
{
    List<EventGridEvent> eventsList = new List<EventGridEvent>();
    for (int i = 0; i < 1; i++)
    {
        eventsList.Add(new EventGridEvent()
        {
            Id = Guid.NewGuid().ToString(),
            EventType = "Contoso.Items.ItemReceivedEvent",
            Data = new ContosoItemReceivedEventData()
            {
                ItemUri = "ContosoSuperItemUri"
            },

            EventTime = DateTime.Now,
            Subject = "Door1",
            DataVersion = "2.0"
        });
    }
    return eventsList;
}

Now, how can I check if the function was actually triggered and if it is sending the sending response with C# code?

Also, in the above code, is the EventType the name of the function to be triggered? Or how does the Event Grid knows which function to run?

Jack
  • 241
  • 2
  • 14

1 Answers1

0

The above Function is used to send events to an Event Grid Topic by specifying the topic endpoint and the topic key of the desired Event Grid Topic. On executing this function manually, events will be sent to the event grid topics which can further be subscribed by the event grid subscriptions you have created for the Event grid topic.

To know more details on event grid entities refer this link

In order to test whether events have been sent to the event grid topic and successfully subscribed by the event grid subscriptions you can view their metrics in the azure portal.

Delivery metrics The portal displays metrics for the status of delivering event messages.

For topics, the metrics are:

Publish Succeeded: Event successfully sent to the topic, and processed with a 2xx response.

Publish Failed: Event sent to the topic but rejected with an error code.

Unmatched: Event successfully published to the topic, but not matched to an event subscription. The event was dropped.

For subscriptions, the metrics are:

Delivery Succeeded: Event successfully delivered to the subscription's endpoint, and received a 2xx response.

Delivery Failed: Event sent to subscription's endpoint, but received a 4xx or 5xx response.

Expired Events: Event was not delivered and all retry attempts were sent. The event was dropped.

Matched Events: Event in the topic was matched by the event subscription.

You can use these metrics to test your function to test sending events to your event grid topic

Community
  • 1
  • 1
Ranjith Eswaran
  • 333
  • 2
  • 12
  • OK, but is there a way to check programmatically? Without having to go to the dashboard and see the metrics? – Jack Oct 16 '18 at 14:41
  • 1
    @Jack You can use the REST API to get the Metrics for specific interval, for example: //management.azure.com/subscriptions/xxxxx/resourceGroups/xxxxx/providers/Microsoft.EventGrid/topics/xxxxx/providers/Microsoft.Insights/metrics/PublishSuccessCount?api-version=2018-01-01 Note, that the Bearer token is required for any REST API management call. – Roman Kiss Oct 16 '18 at 22:39
  • 1
    @Jack.Yes you can get the metrics of your event grid entities by using the REST API as Roman Kiss mentioned. You can authenticate the REST API with the bearer token using your Service Principal details. – Ranjith Eswaran Oct 17 '18 at 03:40