## 5 Trace Sampling Based on Type, Duration, and Errors
### The Sampling of Erroneous Traces  
In specific frameworks, sampling should preserve other important properties besides duration and type. One such property is the normality/abnormality of a trace, which  
Figure 12: The sampling rates that correspond to different values of \(\alpha\) and \(\beta\). The colors correspond to the different ranges of sampling rates for a better visualization. characterizes microservices' performance and could be used for troubleshooting and root-cause analysis. In the described approach, we cannot guarantee a sufficient number of important erroneous traces in the sampled dataset, as there was no requirement to preserve them. Now, we will try to address that requirement by discussing various approaches. The first approach is the natural modification of the previous one, ensuring the existence of enough erroneous traces in each trace type after the sampling. More precisely, we verify the existence of erroneous traces in each trace type. If they exist, we divide the corresponding type into two types containing only normal or erroneous traces. Then, we can apply the approach that has already been considered (sampling by type, duration, or hybrid) to the same dataset but with renewed trace types. Dividing erroneous traces into additional types according to the corresponding error codes is also possible. Assume that trace type \"A\" has erroneous traces containing error codes \"400-Bad Request\" and \"401-unauthorized\". Then, we can divide it into three new trace types: \"A-we\" (without errors), \"A-400\" (with 400 code), and \"A-401\" (with 401 code). It is also possible to divide erroneous traces into types by their error codes independent of the types. For example, collect all traces with \"400\" error codes together in a single type, independent of their original types. Let us show how this approach works. Assume two trace types; for example, 300 traces of type \"A\" and 130 of type \"B\", and we must store only 30% of the traces. The sampling, based only on the trace types, provides the value \(\alpha=0.619\). It means storing 60 traces of type \"A\" and 69 of type \"B\". Figure 13 illustrates those numbers. Now, let us also consider erroneous traces. Assume type \"A\" has 200 normal and 100 erroneous traces. Thus, types \"A\" and \"A\" contain 200 and 100 traces, respectively. Then, assume type \"B\" has 60 normal and 70 erroneous traces. Thus, \"B\" and \"B\" contain 60 and 70 traces, respectively. Now, instead of two types, there are four. The type-based sampling provides the value \(\alpha=0.283\), which leads to sampling rates 0.2, 0.43, 0.34, and 0.4, respectively. Figure 14 shows the distributions before and after sampling. As a result, from 260 normal and 170 erroneous traces, the approach sampled 66 and 63, respectively (see the right figure of Figure 14). Now, we can guarantee that the final sampled set also contains erroneous traces across different types. It is possible to apply the hybrid approach that will also consider the duration of the erroneous traces. Figure 13: The sampling of two trace types without counting the errors. The second approach tries to control the percentage of erroneous traces better in the sampled dataset. Let \(0<h<1\) be the final required sampling rate. Assume that \(h_{e}\) and \(h_{n}\) are the sampling rates of erroneous and normal traces, respectively:  
\[N_{n}^{*}=h_{n}N_{n}, \tag{27}\]  
and  
\[N_{e}^{*}=h_{e}N_{e}, \tag{28}\]  
where \(N_{n}\) and \(N_{e}\) are the number of normal and erroneous traces before sampling, respectively, and \(N_{n}^{*}\) and \(N_{e}*\) after sampling. Can we also put some requirements on \(h_{e}\)? We have  
\[h_{n}\frac{N_{n}^{*}}{N_{n}}+h_{e}\frac{N_{e}^{*}}{N_{e}}=h, \tag{29}\]  
and  
\[h_{n}=\frac{N}{N_{n}}\bigg{(}h-h_{e}\frac{N_{e}}{N}\bigg{)}. \tag{30}\]  
If the requirements on \(h\) and \(h_{e}\) lead to the value \(0<h_{n}<1\), then the requirements can be accomplished. Otherwise, if \(h_{n}\) turns out to be a negative number, that means it is impossible, and we must ask to change the requirements.