graphql-engine/server/src-lib/Hasura/Tracing.hs
kodiakhq[bot] bb8f6a76cc PLAT-438: improve traces from OpenTelemetry
[PLAT-438](https://hasurahq.atlassian.net/browse/PLAT-438)

After this change the top level trace is is annotated with `request_id` and `parameterized_query_hash`. It looks like this,  in jeager:

![image](https://user-images.githubusercontent.com/210815/233206107-73682a85-3306-4d1d-8a6c-148890dc1a38.png)

 bulk queries should output a separate `parameterized_query_hash` for each query span

...after the last two commits, things look like this (remove pointless spans, more attributes):

![image](https://user-images.githubusercontent.com/210815/233476532-47521f35-2cdf-4f7d-af13-39c7ce15c9ec.png)

[PLAT-438]: https://hasurahq.atlassian.net/browse/PLAT-438?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8824
GitOrigin-RevId: 0fd105c879161587d41b729b9bac968e92efae95
2023-04-26 16:21:29 +00:00

84 lines
3.0 KiB
Haskell

module Hasura.Tracing (module Tracing) where
import Hasura.Tracing.Class as Tracing
import Hasura.Tracing.Context as Tracing
import Hasura.Tracing.Monad as Tracing
import Hasura.Tracing.Reporter as Tracing
import Hasura.Tracing.Sampling as Tracing
import Hasura.Tracing.TraceId as Tracing
import Hasura.Tracing.Utils as Tracing
{- Note [Tracing]
\## Usage
The Tracing library allows us to trace arbitrary pieces of our code, providing
that the current monad implements 'MonadTrace'.
newTrace "request" do
userInfo <- newSpan "authentication" retrieveUserInfo
parsedQuery <- newSpan "parsing" $ parseQuery q
result <- newSpan "execution" $ runQuery parsedQuery userInfo
pure result
\## Trace and span
Each _trace_ is distinct, and is composed of one or more _spans_. Spans are
organized as a tree: the root span covers the entire trace, and each sub span
keeps track of its parent.
We report each span individually, and to each of them we associate a
'TraceContext', that contains:
- a trace id, common to all the spans of that trace
- a unique span id, generated randomly
- the span id of the parent span, if any
- whether that trace was sampled (see "Sampling").
All of this can be retrieved for the current span with 'currentContext'.
Starting a new trace masks the previous one; in the following example, "span2"
is associated to "trace2" and "span1" is associated to "trace1"; the two trees
are distinct:
newTrace "trace1" $
newSpan "span1" $
newTrace "trace2" $
newSpan "span2"
Lastly, a span that is started outside of a root trace is, for now, silently
ignored, as it has no trace id to attach to. This is a design decision we may
revisit.
\## Metadata
Metadata can be attached to the current span with 'attachMetadata', as a list
of pair of text key and text values.
\## Reporters
'TraceT' is the de-facto implementation of 'MonadTrace'; but, in practice, it
only does half the job: once a span finishes, 'TraceT' delegates the job of
actually reporting / exporting all relevant information to a 'Reporter'. Said
reporter must be provided to 'runTraceT', and is a wrapper around a function in
IO that processes the span.
In practice, 'TraceT' is only a reader that keeps track of the reporter, the
default sampling policy, and the current trace.
\## Sampling
To run 'TraceT', you must also provide a 'SamplingPolicy': an IO action that,
when evaluated, will decide whether an arbitrary trace should be reporter or
not. This decision is only made once per trace: every span within a trace will
use the same result: they're either all reporter, or none of them are.
When starting a trace, the default sampling policy can be overriden. You can for
instance run 'TraceT' with an action that, by default, only reports one out of
every ten traces, but use 'newTraceWithPolicy sampleAlways' when sending
critical requests to your authentication service.
Note that sampling and reporting are distinct: using 'sampleAlways' simply
guarantees that the 'Reporter' you provided will be called.
-}