Crate relay_sampling

source ·
Expand description

Sampling logic for performing sampling decisions of incoming events.

In order to allow Sentry to offer performance at scale, Relay extracts key metrics from all transactions, but only forwards a random sample of raw transaction payloads to the upstream. What exact percentage is sampled is determined by dynamic sampling rules, and depends on the project, the environment, the transaction name, etc.

In order to determine the sample rate, Relay uses a SamplingConfig which contains a set of SamplingRules that are matched against incoming data.

Types of Sampling

There are two main types of dynamic sampling:

  1. Trace sampling ensures that either all transactions of a trace are sampled or none. Rules have access to information in the DynamicSamplingContext.
  2. Transaction sampling does not guarantee complete traces and instead applies to individual transactions. Rules have access to the full data in transaction events.

Components

The sampling system implemented in Relay is composed of the following components:

  • DynamicSamplingContext: a container for information associated with the trace.
  • SamplingRule: a rule that is matched against incoming data. It specifies a condition that acts as predicate on the incoming payload.
  • SamplingMatch: the result of the matching of one or more rules.

How It Works

  • The incoming data is received by Relay.
  • Relay resolves the SamplingConfig of the project to which the incoming data belongs. Additionally, it tries to resolve the configuration of the project that started the trace, the so-called “root project”.
  • The two SamplingConfigs are merged together and dynamic sampling evaluates a result. The algorithm goes over each rule and compute either a factor or sample rate based on the value of the rule.
  • The SamplingMatch is finally returned containing the final sample_rate and some additional data that will be used in relay_server to perform the sampling decision.

Determinism

The concept of determinism is a core concept for dynamic sampling. Deterministic sampling decisions allow to:

  • Run dynamic sampling repeatedly over a chain of Relays (e.g., we don’t want to drop an event that was retained by a previous Relay and vice-versa).
  • Run dynamic sampling independently on transactions of the same trace (e.g., we want to be able to sample all the transactions of the same trace, even though some exceptions apply).

In order to perform deterministic sampling, Relay uses teh trace ID or event ID as the seed for the random number generator.

Examples

SamplingConfig

{
  "version": 2,
  "rules": [
    {
      "condition": {
        "op": "and",
        "inner": [
          {
            "op": "glob",
            "name": "releases",
            "value": [
              "1.1.1",
              "1.1.2"
            ]
          }
        ]
      },
      "samplingValue": {
        "type": "sampleRate",
        "value": 0.7
      },
      "type": "transaction",
      "id": 2
    },
    {
      "condition": {
        "op": "and",
        "inner": [
          {
            "op": "eq",
            "name": "transaction",
            "value": "/foo"
          }
        ]
      },
      "samplingValue": {
        "type": "factor",
        "value": 5.0
      },
      "type": "trace",
      "id": 1,
      "timeRange": {
        "start": "2022-10-10T00:00:00.000000Z",
        "end": "2022-10-20T00:00:00.000000Z"
      }
    }
  ],
  "mode": "total"
}

DynamicSamplingContext

{
  "trace_id": "00000000-0000-0000-0000-000000000000",
  "public_key": "abd0f232775f45feab79864e580d160b",
  "release": "1.0",
  "environment": "dev",
  "transaction": "/foo",
  "sample_rate": "0.5",
  "user_id": "some-id",
  "user_segment": "all",
  "user": null
}

Re-exports

Modules

  • Dynamic sampling rule configuration.
  • Contextual information stored on traces.
  • Evaluation of dynamic sampling rules.