Push ServiceStack Redis usage to Application Insights Dependency telemtry

440 Views Asked by At

We use the ServiceStack ICacheClient to talk to our redis server. How can I send the telemetry from these calls (command, call success/failure, duration, etc.) to application insights. Is there an event I can subscribe to thats gets fired after a ICacheClient call where I can write some code to send the data to Applicaiton Insights?

For example, this is how I do it for our MongoDB Driver:

mongoClientSettings.ClusterConfigurator = clusterConfigurator =>
{
    clusterConfigurator.Subscribe<CommandSucceededEvent>(e =>
    {
        // Log a successful dependency event on every CommandSucceededEvent
        TelemetryLogger.SendDependencyTelemetry(DependencyTypeName, e.ConnectionId.ServerId.EndPoint.ToString().Split('/', ':')[1], e.CommandName,
            e.CommandName, DateTimeHelper.GetUnspecifiedTimeNow().Subtract(e.Duration), e.Duration, true);
    });
    clusterConfigurator.Subscribe<CommandFailedEvent>(e =>
    {
        // Log a failed dependency event on every CommandFailedEvent 
        TelemetryLogger.SendDependencyTelemetry(DependencyTypeName, e.ConnectionId.ServerId.EndPoint.ToString().Split('/', ':')[1], e.CommandName,
            $"{e.CommandName}\n{e.ToString()}", DateTimeHelper.GetUnspecifiedTimeNow().Subtract(e.Duration), e.Duration, false);
    });
};

Thanks!

1

There are 1 best solutions below

0
On

You'd likely need a decorator class like CacheClientWithPrefix.cs to decorate all ICacheClient provider to add a prefix to all ICacheClient APIs, e.g:

container.Register(c => 
    c.Resolve<IRedisClientsManager>().GetCacheClient().WithTelemetry());

Where it intercepts each API call to also call the Telemetry APIs.