Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package tracectx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
// BoundState discards trailing vendor entries as whole members to fit the job envelope.
|
||||
func BoundState(state string) string {
|
||||
const limit = 512
|
||||
if len(state) <= limit {
|
||||
return state
|
||||
}
|
||||
cut := strings.LastIndexByte(state[:limit+1], ',')
|
||||
if cut < 0 {
|
||||
return ""
|
||||
}
|
||||
return state[:cut]
|
||||
}
|
||||
|
||||
func Fields(ctx context.Context) (string, string) {
|
||||
c := propagation.MapCarrier{}
|
||||
propagation.TraceContext{}.Inject(ctx, c)
|
||||
return c.Get("traceparent"), BoundState(c.Get("tracestate"))
|
||||
}
|
||||
|
||||
func Restore(ctx context.Context, parent, state string) context.Context {
|
||||
return propagation.TraceContext{}.Extract(ctx, propagation.MapCarrier{"traceparent": parent, "tracestate": BoundState(state)})
|
||||
}
|
||||
Reference in New Issue
Block a user