github.com/v2fly/tools@v0.100.0/internal/event/export/tag.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package export 6 7 import ( 8 "context" 9 10 "github.com/v2fly/tools/internal/event" 11 "github.com/v2fly/tools/internal/event/core" 12 "github.com/v2fly/tools/internal/event/label" 13 ) 14 15 // Labels builds an exporter that manipulates the context using the event. 16 // If the event is type IsLabel or IsStartSpan then it returns a context updated 17 // with label values from the event. 18 // For all other event types the event labels will be updated with values from the 19 // context if they are missing. 20 func Labels(output event.Exporter) event.Exporter { 21 return func(ctx context.Context, ev core.Event, lm label.Map) context.Context { 22 stored, _ := ctx.Value(labelContextKey).(label.Map) 23 if event.IsLabel(ev) || event.IsStart(ev) { 24 // update the label map stored in the context 25 fromEvent := label.Map(ev) 26 if stored == nil { 27 stored = fromEvent 28 } else { 29 stored = label.MergeMaps(fromEvent, stored) 30 } 31 ctx = context.WithValue(ctx, labelContextKey, stored) 32 } 33 // add the stored label context to the label map 34 lm = label.MergeMaps(lm, stored) 35 return output(ctx, ev, lm) 36 } 37 }