Cause
I am currently using Halal applicationinsights (hereinafter referred to as AI) to do program-level monitoring. (AI related documents: https://azure.microsoft.com/zh-cn/services/application-insights/)
In fact, everything is fine, but our job system based on Hangfire can not be monitored by AI, because AI it monitoring principle is based on HttpModule to monitor the request, Hangfire, by polling for storage (such as SQL or queues), implements job processing.
That is, Hangfire theoretically does not have any requests for the Hangfire site itself, it resembles a while (true) dead loop constantly polling storage to get the job task executed.
The Hangfire series can be consulted on the official instructions http://docs.hangfire.io/en/latest/
Although Hangfire own a dashboard can monitor the tasks performed by Hangfire, as follows
But a lot of other sites I all in the portal in Azure at a glance, you a hangfire want me to run to your own home dashboard look after all, but also easy to forget, resulting in Hangfire site perennial in the forgotten corner.
Recently, a Hangfire site has been accelerated to unify the Hangfire monitoring into AI after an event that has not been up (down) for some external reason.
Search
First go to GitHub to find out if there is no relevant solution, honestly think that the third-party based on AI extension is still quite a lot, in GitHub search AI related or can search for several pages (https://github.com/topics/ Application-insights), but did not find what I needed.
Then Google found someone in the Hangfire forum to ask me similar questions, but there is no solution (https://discuss.hangfire.io/t/ integrating-application-insights-appinsights-into-hangfire/3009).
Since I can't find it, I'll just roll up my sleeves and do it.
Open Dry
First we review the AI default principle of self-monitoring:
The previous article said it had a httpmodule that would be in the request to create a requesttelemetry,
And a operationid is created within the thread context, and then all other data (such as an exception/http request/sql request) that the request is scoped to will be associated with that ID.
This ID will even be appended to your HTTP header when you send an HTTP request, and then the site receiving the HTTP request will have two more associations (call link associations) based on the ID in the header if the AI is used.
First to solve the basic knowledge of AI needs
We're going to be in the hangfire. Ai monitoring, in fact, the focus is how to get the ID in the request can be passed to the Hangfire, and then in the scope of Hangfire an operation to keep the ID consistent (concatenation of all operations).
The point is clear, that is, how to solve the problem of this ID.
How does the AI generate or acquire this ID, and after the AI version 2.4 has introduced a dependency on the System.Diagnostics.DiagnosticSource package.
All ID associations It is based on the activity provided by this package relationship serve now, details can refer to http://apmtips.com/blog/2018/01/23/diagnosticssource-design-principles/
It is also highly recommended to use the activity class if you need to set up an ID associated system in your system.
Other people achieve more than their own science, I also used asynclocal to do before, but the follow-up has been replaced by activity,
The current activity instance can be obtained through activity.current, and then the Start method can be started by using the new activity.
The knowledge required for this AI is ready.
Then solve the basic knowledge required by Hangfire
From a hangfire point of view, it can only provide 2 functional support.
When the task is queued, the ID of the current request operation is placed in the data, because I want to associate the Hangfire operation with the request.
At the time of the task execution, when you get the task data, you need to be able to remove the ID, then start the ID through activity and then stop the activity and release it after the task is executed.
(The picture is "a bit" ugly, but that's probably what it means)
In order to solve this problem find the next Hangfire global filter related data find it has Iserverfilter and iclientfilter these 2 things:
Iserverfilter: A filter that is processed by the server, which is the filter to be processed by Hangfire servers when executing a job, you can set the ID of the AI to the context of the Hangfire job at this location.
Iclientfilter: The filter that is processed by the client is the filter to be processed by the Hangfire client when a job is queued, and the ID in the request can be assigned to job Data at this location.
How to solve every time when the client can automatically plug an id into it?
My solution is to define a jobdtobase, all the data of the Hangfire task to inherit from this class, inside an ID, and then through the Iclientfilter in each queue will be the current Activity.Current.Id thrown in
(You need to consider the case of acitivity.current null)
And how do you automatically start an activity and monitor it on the server side?
I was thinking about getting the job parameters through the iserverfilter through the performingcontext and then using the IS To judge if it is jobdtobase, take its ID and start the activity and create the requesttelemetry.
The point is that requesttelemetry and activity need to be freed after the execution of the server side, so the threadstatic static variable is used to keep the reference.
Effect
Effect how this problem temporarily I can only hehe, because also just toss out has not put into line run.
But judging from the test environment, I can really catch my hangfire request and show it as request in AI:
You can also catch dependencies associated with "Request":
So at least the test situation should be to achieve the goal, and so on after the line to look at the final concrete effect.
Hangfire using Applicationinsigts Monitoring