Home

Vostok.Context – library for spreading the context across the asynchronous control flow. It also supported spreading the context across the network.

Spreading the context

When the code is executed, asynchronous control flow is built. Sometimes we want to spread some context at the tree. For example, we want to have to access information about a user or tracing by default.

Consider the following situation: A user had come in our service. We know something about him. For example, we know his id. Control flow looks like this:

Handle() ....Method1() ........Method2() ............ – ................MethodN()

We want to have to access information about the user at every level of asynchronous control flow. But at the same time, we wouldn't need to throw this data explicitly. For example, we want to say: "UserId is xxx now" in Handle() and get UserId in MethodN() :

Handle() <------- Context.Put ("UserId", xxx) ....Method1() ........Method2() ............ – ................MethodN() <------- Context.Get("UserId")

If the code is running in one thread, we can use thread local storage. Just put local for current thread data in this storage. But what if the code is asynchronous and we still want general context?

Microsoft created a solution – AsyncLocal.

Why do you need Vostok.Context?

Context is more user-friendly wrapper above the AsyncLocal. The main value of Vostok-Context is integration with other Vostok's libraries. For example, if you use Vostok.Context + ClusterClient , you can automatically spread the context across the network. (Not just in a current asynchronous stream.)

By way of illustration: A user has come in our service, and we know his id. But calls go through another service unlike the example above. It looks like this:

Service1 Handle() ....Method1() ........Method2() ............ – ................MethodN() | | HTTP request | \/ Service2 Handle() ....Method1() ........Method2() ............ – ................MethodM()

We want to have access to information on the user at every level of asynchronous control flow in the Service2.

You can spread the context across the network with Vostok.Context + ClusterClient. The function of the Context is serialization and deserialization of disseminated data. Use model, connecting ClusterClient and Context – Vostok.Clusterclient.Context to send context over the network.

Interfaces

There are two different interfaces in Context:

When do you need Vostok.Context?

The main prime example is trace. If you use the Context, you can add prefix to operation like at this example:

Last updated