How to get a count of all telemetry messages currently available ?

SilverChips 71 Reputation points
2020-11-28T16:05:52.9+00:00

I am currently doing something like this to get all available telemetry (device to cloud messages).
ReadEventsAsync would block once there are no new messages. I know I can specify a time period but then that will be a wait game. Is there a way for me to determine how many messages are currently available ?

I wanted to know if there is a way for me to stop listening for messages once I have consumed all previous messages.
The only way I can think of right now is to know the no of available messages

Task.Run(async () =>
            {
                var cancellationSource = new CancellationTokenSource();
                await using EventHubConsumerClient consumer = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connectionString, DeviceHubConstants.EventHubName);
                try
                {   
                    await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync( cancellationSource.Token))
                    {
                        string data = Encoding.UTF8.GetString(partitionEvent.Data.Body.ToArray());

                    }
                }
                catch (TaskCanceledException)
                {

                }
            }).GetAwaiter().GetResult();
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,128 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
208 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde 29,271 Reputation points MVP
    2020-11-30T21:56:26.733+00:00

    The REST API of the IoT Hub gives exactly what you need.

    This goes in two steps:

    1. You need some credentials for your subscription so you can construct a bearer token

    Run these two powershell commands

    az login
    az ad sp create-for-rbac -n "testaccount"

    This gives you credentials for a non-human account...

    (perhaps you need to install the powershell az module first)

    1. Execute this code after you filled in the variables:

    Run this application with two Rest calls:

       internal class Program  
           {  
               private static void Main(string[] args)  
               {  
                   var appId = "";  
                   var password = "";  
                   var tennant = "";  
             
                   var bearer = GetBearer(tennant, appId, password);  
             
                   GetQuota(bearer, "[your subscriptionId]", "[resourceGroup of your IoT Hub]", "[name of your iotHub]");  
             
                   Console.WriteLine("Press a key to exit");  
             
                   Console.ReadKey();  
               }  
             
               private static void GetQuota(string bearer, string subscriptionId, string resourceGroup, string iotHub)  
               {  
                   var url = $"https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{rg}/providers/Microsoft.Devices/IotHubs/{iotHub}/quotaMetrics?api-version=2018-04-01";  
             
                   using var client = new HttpClient();  
                   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearer);  
                   var req = new HttpRequestMessage(HttpMethod.Get, url);  
             
                   using var res = client.SendAsync(req).Result;  
                   var jsonString = res.Content.ReadAsStringAsync().Result;  
             
                   Console.WriteLine(jsonString);  
               }  
             
               private static string GetBearer(string tennant, string appId, string password)  
               {  
                   var nvc = new List<KeyValuePair<string, string>>  
                   {  
                       new KeyValuePair<string, string>("grant_type", "client_credentials"),  
                       new KeyValuePair<string, string>("client_id", appId),  
                       new KeyValuePair<string, string>("client_secret", password),  
                       new KeyValuePair<string, string>("resource", "https://management.azure.com/")  
                   };  
             
                   var url = $"https://login.microsoftonline.com/{tennant}/oauth2/token";  
             
                   using var client = new HttpClient();  
                   var req = new HttpRequestMessage(HttpMethod.Post, url)  
                   {  
                       Content = new FormUrlEncodedContent(nvc)  
                   };  
             
                   using var res = client.SendAsync(req).Result;  
                   var jsonString = res.Content.ReadAsStringAsync().Result;  
                   var json = JsonConvert.DeserializeObject<dynamic>(jsonString);  
                   return json.access_token;  
               }  
           }  
    

    As you can see, you need three other values. You can find them on the overview page of your iothub:

    43770-image.png

    this will result in the right numbers:

    43891-image.png

    Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.

    1 person found this answer helpful.

  2. QuantumCache 20,031 Reputation points
    2020-11-30T21:14:36.11+00:00

    Hello @ SilverChips This is a great question!

    Platform metrics and the Activity log are collected and stored automatically, but can be routed to other locations by using a diagnostic setting.

    Metrics and logs can be routed to several locations including:

    • The Azure Monitor Logs store via an associated Log Analytics workspace. There they can be analyzed using Log Analytics.
    • Azure Storage for archiving and offline analysis
    • An Event Hubs endpoint where they can be read by external applications, for example, third-party SIEM tools.

    Please have look at Collection and routing.

    Also please have a look at this Stackoverflow thread responded by Microsoft MVP @Roman Kiss and other community champs, hope this helps to guide with your query.

    Please comment below if you need further help in this matter.

    0 comments No comments