Skip to content

Getting Started with Instrumentation SDK

This SDK helps you to include Telemetry and report dependency for the following sub-systems:

  • Kafka
  • Redis
  • PostgreSQL database

These sub-systems are not inherently supported by Application Insights, hence this SDK

In order to get started with Hubtel.Instrumentation, do the following activities

Install-Package Hubtel.Instrumentation

The code snippet below is a command that is used to install Hubtel.Instrumentation package in a .NET project.

C#
    Install-Package Hubtel.Instrumentation

Add to IServiceCollection in Program.cs or Startup.cs

  • The provided C# code snippet is configuring Application Insights telemetry for different types of applications. Application Insights is a service provided by Microsoft Azure that allows developers to monitor their live applications, detect performance anomalies, and diagnose issues with their applications.

services.AddApplicationInsightsTelemetry()

  • This above code is adding Application Insights telemetry to a web application. This is typically placed in the ConfigureServices method in the Startup.cs file of an ASP.NET Core application. It enables the collection of telemetry data like request rates, response times, failure rates, etc.

services.AddApplicationInsightsTelemetryWorkerService("your instrumentation key here")

  • This code given above is adding Application Insights telemetry to a console application or a worker service. The method takes an instrumentation key as a parameter, which is a unique identifier for your Application Insights resource. This key tells the SDK where to send the telemetry data.

services.AddApplicationInsightsTelemtryHubtel("your instrumentation key here")

  • This code above is adding Application Insights telemetry to a Hubtel application. However, AddApplicationInsightsTelemtryHubtel is not a standard method in the Application Insights SDK, so it might be a custom extension method. Like the previous method, it takes an instrumentation key as a parameter.
c#
    services.AddApplicationInsightsTelemetry(); //for web applications
    services.AddApplicationInsightsTelemetryWorkerService("your instrumentation key here"); //for console applications / worker services
    services.AddApplicationInsightsTelemtryHubtel("your instrumentation key here");

Inject and consume service methods

private readonly IHubtelDiagnosticsPublisher _publisher

This line declares a private, read-only field of type IHubtelDiagnosticsPublisher named _publisher. This interface presumably provides methods for reporting diagnostics.

The code then shows three examples of using the _publisher to report diagnostics for different types of operations.

  1. Kafka Diagnostics:

The StartKafkaDiagnostics method is called with a bootstrap server URL as an argument. This method presumably starts a diagnostic session for a Kafka operation. The method returns an object that is used in a using statement, which ensures that resources are cleaned up when the operation is complete. If an exception occurs during the operation, it is caught and the Failed and Exception properties of the diagnostic object are set.

  1. Redis Diagnostics:

The StartRedisDiagnostics method is called with the Redis server details as arguments. Similar to the Kafka example, a diagnostic session is started and any exceptions are caught and reported.

  1. PostgreSQL Diagnostics:

The StartPostgreSqlDiagnostics method is called with a database connection string as an argument. Again, a diagnostic session is started and any exceptions are caught and reported.

In each case, the actual logic for the operation (e.g., producing a Kafka message, executing a Redis command, executing a PostgreSQL query) is not shown and would be added where the comments indicate.

c#
    private readonly IHubtelDiagnosticsPublisher _publisher;    

    //P.S. be sure to include as constructor parameter

    //to report Kafka diagnostics
     using (var kdp = _publisher.StartKafkaDiagnostics("bootstrap server URL"))
                {
                    try
                    {
                        k.Topic = "your kafka topic";
                        k.Payload = "your kafka payload";

                        //. ... your producer logic
                        // _producer.Produce("topic","message")
                    }
                    catch (Exception e)
                    {                        
                        kdp.Failed = true;
                        kdp.Exception = e;
                    }
                }

    //to report Redis diagnostic
     using (var rd = _publisher.StartRedisDiagnostics("localhost","6379","1"))
            {
                try
                {                                      
                    rd.Command = $"SET mykey myvalue";
                    //your redis logic
                    // _cacheManager.Set("mykey","myvalue");
                }
                catch (Exception e)
                {
                    rd.Exception = e;
                    rd.Failed = true;
                }
            }

    //to report PG diagnostics
       using (var pgd =
                _publisher.StartPostgreSqlDiagnostics(
                    "your DB connection string"))
            {
               
                 try
                {                                      
                    var sql = "SELECT * FROM \"public\".\"MyTable\"";
                    pgd.Sql = sql;
                    //your PG logic
                    // return await connection.QueryAsync<T>(sql)
                }
                catch (Exception e)
                {
                    pgd.Exception = e;
                    pgd.Failed = true;
                }
            }

Was this page helpful?

Happy React is loading...