private SpanContext CreateChildSpanContext() { var traceId = new TraceId(RandomGenerator.RandomId(), RandomGenerator.RandomId()); var parentId = new SpanId(0); var spanId = new SpanId(traceId.Low); var baggage = new Dictionary <string, string>(); var flags = ContextFlags.None; var(isSampled, samplerTags) = _sampler.IsSampled(traceId, _operationName); if (isSampled) { foreach (var samplingTag in samplerTags) { _tags[samplingTag.Key] = samplingTag.Value; } flags |= ContextFlags.Sampled; _metrics.TraceStartedSampled.Inc(1); } else { _metrics.TraceStartedNotSampled.Inc(1); } var spanContext = new SpanContext(traceId, spanId, parentId, baggage, flags); return(spanContext); }
public void Tracer_BuildSpan_ShouldPassItselfAndOperationNameToSpanBuilder() { _mockSampler.IsSampled(Arg.Any <TraceId>(), Arg.Any <string>()).Returns((false, new Dictionary <string, object>())); var span = (ILetsTraceSpan)_builtTracer.BuildSpan(_operationName).Start(); Assert.Equal(_operationName, span.OperationName); Assert.Equal(_builtTracer, span.Tracer); }
public void IsSampled_ShouldCallSampler() { var op = "op"; var traceId = new TraceId(452); _mockSampler.IsSampled(Arg.Is <TraceId>(tid => tid == traceId), Arg.Is <string>(on => on == op)) .Returns((true, new Dictionary <string, object>())); var isSampled = _testingSampler.IsSampled(traceId, op); Assert.True(isSampled.Sampled); _mockSampler.Received(1).IsSampled(Arg.Any <TraceId>(), Arg.Any <string>()); }
public (bool Sampled, Dictionary <string, object> Tags) IsSampled(TraceId id, string operation) { var operationKey = operation.ToLower(); if (_samplers.TryGetValue(operationKey, out var sampler)) { return(sampler.IsSampled(id, operation)); } if (_samplers.Count >= _maxOperations) { return(_defaultSampler.IsSampled(id, operation)); } var newSampler = (IGuaranteedThroughputProbabilisticSampler)_factory.NewGuaranteedThroughputProbabilisticSampler(_samplingRate, _lowerBound); _samplers[operationKey] = newSampler; return(newSampler.IsSampled(id, operation)); }
/// <summary> /// Use the <see cref="ISampler"/> to make a sampling decision. /// </summary> /// <returns> /// The current <see cref="SpanContext"/>. /// </returns> public SpanContext Sample(ISampler sampler) { Sampled = sampler.IsSampled(this); return(this); }