question

SilverChips avatar image
0 Votes"
SilverChips asked ·

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

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-hubazure-iot-sdk
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @ SilverChips Just checking back, could you please let us know if you need further help in this matter and get a chance to refer to the response provided by SandervandeVelde42 ?

0 Votes 0 ·
SatishBoddu-MSFT avatar image
0 Votes"
SatishBoddu-MSFT answered ·

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 @rkiss and other community champs, hope this helps to guide with your query.

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


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SandervandeVelde42 avatar image
1 Vote"
SandervandeVelde42 answered ·

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.



image.png (21.7 KiB)
image.png (6.4 KiB)
· 4 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Awesome E2E, I vote for this!

0 Votes 0 ·

Thanks for the explanation. I would like to try this out. I am confused with the GetBearer Token part ? I am not sure how to obtain the appID,password and tenant. Also I would not like to specify the password under any condition. Is there a way for me to optain this token in any other way ? Perhaps by registering the app ?

0 Votes 0 ·

Do not worry, these credentials are not related to an actual person!

If you execute the two powershell commands ...

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

... you then get the fields needed for generating a bearer token (which has limited access, eg. for a few hours).

You only have to generate these credentials once and put them in a configuration (eg. Azure key vault). You can revoke the credentials at any time.

Run the code above as part of your service on the cloud, not on your PC (so the credentials stay in the cloud).

This is just how access to Azure with bearer tokens works in general. Never share (IoT Hub) credentials outside azure...

0 Votes 0 ·

Hello @ SilverChips Just checking back, could you please let us know if you need further help in this matter and get a chance to refer to the response provided by SandervandeVelde42 ?

0 Votes 0 ·