Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/telemetry/sampling_span_processor.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/telemetry/sampling_span_processor.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway@83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/telemetry/sampling_span_processor.go
-
curl -L -o sampling_span_processor.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/telemetry/sampling_span_processor.go
2.85 kB
| package telemetry | |
| import ( | |
| "context" | |
| "crypto/sha256" | |
| "time" | |
| "go.opentelemetry.io/otel/attribute" | |
| "go.opentelemetry.io/otel/codes" | |
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | |
| "go.opentelemetry.io/otel/trace" | |
| ) | |
| const ( | |
| mustSampleAttribute attribute.Key = "httpsms.must_sample" | |
| slowDatabaseSpanThreshold = 200 * time.Millisecond | |
| ) | |
| type samplingSpanProcessor struct { | |
| sdktrace.SpanProcessor | |
| } | |
| // NewSamplingSpanProcessor exports warning/error spans, database spans over 200ms, | |
| // and 50% of other spans. Truncated spans are retained conservatively. | |
| // The provider must record all spans so the decision can be made after they end. | |
| func NewSamplingSpanProcessor(exporter sdktrace.SpanExporter) sdktrace.SpanProcessor { | |
| return &samplingSpanProcessor{ | |
| SpanProcessor: sdktrace.NewBatchSpanProcessor(exporter), | |
| } | |
| } | |
| func (processor *samplingSpanProcessor) OnEnd(span sdktrace.ReadOnlySpan) { | |
| if isSampledTrace(span.SpanContext().TraceID()) || shouldRetainSpan(span) { | |
| processor.SpanProcessor.OnEnd(span) | |
| } | |
| } | |
| // SampledTraceExemplarFilter links metrics only to the stable trace-ID sample. | |
| func SampledTraceExemplarFilter(ctx context.Context) bool { | |
| spanContext := trace.SpanContextFromContext(ctx) | |
| return spanContext.IsValid() && spanContext.IsSampled() && isSampledTrace(spanContext.TraceID()) | |
| } | |
| func isSampledTrace(traceID trace.TraceID) bool { | |
| // UUID version/variant bits must not bias the trace-ID decision. | |
| hash := sha256.Sum256(traceID[:]) | |
| return hash[0] < 128 | |
| } | |
| func shouldRetainSpan(span sdktrace.ReadOnlySpan) bool { | |
| // Drop counters survive buffer limits, unlike severity attributes and events. | |
| return span.DroppedEvents() > 0 || | |
| span.DroppedAttributes() > 0 || | |
| span.Status().Code == codes.Error || | |
| hasRetentionFlag(span.Attributes()) || | |
| hasExceptionalEvents(span.Events()) || | |
| hasTruncatedLinks(span) || | |
| isSlowDatabaseSpan(span) | |
| } | |
| func hasTruncatedLinks(span sdktrace.ReadOnlySpan) bool { | |
| if span.DroppedLinks() > 0 { | |
| return true | |
| } | |
| for _, link := range span.Links() { | |
| if link.DroppedAttributeCount > 0 { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| func isSlowDatabaseSpan(span sdktrace.ReadOnlySpan) bool { | |
| if span.EndTime().Sub(span.StartTime()) <= slowDatabaseSpanThreshold { | |
| return false | |
| } | |
| for _, value := range span.Attributes() { | |
| switch value.Key { | |
| case "db.system.name", "db.system": | |
| if value.Value.AsString() != "" { | |
| return true | |
| } | |
| } | |
| } | |
| return false | |
| } | |
| func hasExceptionalEvents(events []sdktrace.Event) bool { | |
| for _, event := range events { | |
| if event.Name == "exception" || event.DroppedAttributeCount > 0 { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| func hasRetentionFlag(attributes []attribute.KeyValue) bool { | |
| for _, value := range attributes { | |
| if value.Key == mustSampleAttribute && value.Value.AsBool() { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |