|
1 | 1 | // Copyright The OpenTelemetry Authors
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -package tailsamplingprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor" |
| 4 | +package telemetry // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/telemetry" |
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "context" |
| 8 | + |
7 | 9 | "go.opencensus.io/stats"
|
8 | 10 | "go.opencensus.io/stats/view"
|
9 | 11 | "go.opencensus.io/tag"
|
10 | 12 | "go.opentelemetry.io/collector/config/configtelemetry"
|
11 | 13 | "go.opentelemetry.io/collector/processor/processorhelper"
|
12 | 14 |
|
13 | 15 | "github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/metadata"
|
| 16 | + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/sampling" |
14 | 17 | )
|
15 | 18 |
|
16 | 19 | // Variables related to metrics specific to tail sampling.
|
|
19 | 22 | tagSampledKey, _ = tag.NewKey("sampled")
|
20 | 23 | tagSourceFormat, _ = tag.NewKey("source_format")
|
21 | 24 |
|
| 25 | + tagMutatorSampled = []tag.Mutator{tag.Upsert(tagSampledKey, "true")} |
| 26 | + tagMutatorNotSampled = []tag.Mutator{tag.Upsert(tagSampledKey, "false")} |
| 27 | + |
22 | 28 | statDecisionLatencyMicroSec = stats.Int64("sampling_decision_latency", "Latency (in microseconds) of a given sampling policy", "µs")
|
23 | 29 | statOverallDecisionLatencyUs = stats.Int64("sampling_decision_timer_latency", "Latency (in microseconds) of each run of the sampling decision timer", "µs")
|
24 | 30 |
|
|
36 | 42 | statTracesOnMemoryGauge = stats.Int64("sampling_traces_on_memory", "Tracks the number of traces current on memory", stats.UnitDimensionless)
|
37 | 43 | )
|
38 | 44 |
|
| 45 | +func contextForPolicyOC(ctx context.Context, configName, format string) (context.Context, error) { |
| 46 | + return tag.New(ctx, tag.Upsert(tagPolicyKey, configName), tag.Upsert(tagSourceFormat, format)) |
| 47 | +} |
| 48 | + |
| 49 | +func recordFinalDecisionOC(ctx context.Context, latencyMicroSec, droppedTooEarly, evaluationErrors, tracesOnMemory int64, decision sampling.Decision) { |
| 50 | + stats.Record(ctx, |
| 51 | + statOverallDecisionLatencyUs.M(latencyMicroSec), |
| 52 | + statDroppedTooEarlyCount.M(droppedTooEarly), |
| 53 | + statPolicyEvaluationErrorCount.M(evaluationErrors), |
| 54 | + statTracesOnMemoryGauge.M(tracesOnMemory), |
| 55 | + ) |
| 56 | + |
| 57 | + var mutators []tag.Mutator |
| 58 | + switch decision { |
| 59 | + case sampling.Sampled: |
| 60 | + mutators = tagMutatorSampled |
| 61 | + case sampling.NotSampled: |
| 62 | + mutators = tagMutatorNotSampled |
| 63 | + } |
| 64 | + |
| 65 | + _ = stats.RecordWithTags( |
| 66 | + ctx, |
| 67 | + mutators, |
| 68 | + statCountGlobalTracesSampled.M(int64(1)), |
| 69 | + ) |
| 70 | +} |
| 71 | + |
| 72 | +func recordPolicyLatencyOC(ctx context.Context, latencyMicroSec int64) { |
| 73 | + stats.Record(ctx, |
| 74 | + statDecisionLatencyMicroSec.M(latencyMicroSec), |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +func recordPolicyDecisionOC(ctx context.Context, sampled bool, numSpans int64) { |
| 79 | + var mutators []tag.Mutator |
| 80 | + if sampled { |
| 81 | + mutators = tagMutatorSampled |
| 82 | + } else { |
| 83 | + mutators = tagMutatorNotSampled |
| 84 | + } |
| 85 | + |
| 86 | + _ = stats.RecordWithTags( |
| 87 | + ctx, |
| 88 | + mutators, |
| 89 | + statCountTracesSampled.M(int64(1)), |
| 90 | + ) |
| 91 | + if isMetricStatCountSpansSampledEnabled() { |
| 92 | + _ = stats.RecordWithTags( |
| 93 | + ctx, |
| 94 | + mutators, |
| 95 | + statCountSpansSampled.M(numSpans), |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +func recordNewTraceIDsOC(ctx context.Context, count int64) { |
| 102 | + stats.Record(ctx, statNewTraceIDReceivedCount.M(count)) |
| 103 | +} |
| 104 | + |
| 105 | +func recordLateSpanOC(ctx context.Context, ageSec int64) { |
| 106 | + stats.Record(ctx, statLateSpanArrivalAfterDecision.M(ageSec)) |
| 107 | +} |
| 108 | + |
| 109 | +func recordTraceRemovalAgeOC(ctx context.Context, ageSec int64) { |
| 110 | + stats.Record(ctx, statTraceRemovalAgeSec.M(ageSec)) |
| 111 | +} |
| 112 | + |
39 | 113 | // samplingProcessorMetricViews return the metrics views according to given telemetry level.
|
40 | 114 | func samplingProcessorMetricViews(level configtelemetry.Level) []*view.View {
|
41 | 115 | if level == configtelemetry.LevelNone {
|
|
0 commit comments