traceutils.go 6.8 KB
Newer Older
T
tanggen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or  modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package common

import (
	"context"
	"errors"
	"fmt"
	"strconv"
	"time"

	mon "github.com/finogeeks/ligase/skunkworks/monitor/go-client/monitor"
	"github.com/opentracing/opentracing-go"
	"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
)

type ContextMsg struct {
	Ctx context.Context
	Msg interface{}
}

var (
	// cs2sr is the latency of transporting
	cs2sr = mon.GetInstance().NewLabeledHistogram("cs_sr_latency_ms",
		[]string{"protocol", "topic", "annotation"}, nil)
	// sob2cs is the latency from business beginning to current module ending
	sob2cs = mon.GetInstance().NewLabeledHistogram("sob_cs_latency_ms",
		[]string{"protocol", "topic", "annotation"}, nil)
	// sob2cs is the latency from module beginning to current module ending
	som2cs = mon.GetInstance().NewLabeledHistogram("som_cs_latency_ms",
		[]string{"protocol", "topic", "annotation"}, nil)
	// cs2cs is the latency from last module ending to current module ending,
	// cs2cs can be calculated by (cs2sr + som2cs), so not exported.
)

const DeleteMark = "__D__"

func markBaggageItemDeleted(span opentracing.Span, items []string) {
	for _, item := range items {
		span.SetBaggageItem(item, DeleteMark)
	}
}

func removeBaggageItemDeleted(carrier *opentracing.HTTPHeadersCarrier) {
	for k, v := range *carrier {
		if len(v) == 1 && v[0] == DeleteMark {
			delete(*carrier, k)
		}
	}
}

const SpanKey = "span"

func InjectSpanToHeader(span opentracing.Span) map[string]string {
	carrier := opentracing.HTTPHeadersCarrier(make(map[string][]string))
	tracer := opentracing.GlobalTracer()
	if err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier); err != nil {
		return nil
	}
	carrierStr, err := json.Marshal(carrier)
	if err != nil {
		return nil
	}
	return map[string]string{SpanKey: string(carrierStr)}
}

func InjectSpanToHeaderForSending(span opentracing.Span) map[string]string {
	now := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
	span.SetBaggageItem("cs", now)
	if len(span.BaggageItem("sob")) == 0 {
		span.SetBaggageItem("sob", now)
	}
	markBaggageItemDeleted(span, []string{"cr", "sr", "ss", "som"}) // only "cs" and "sob" reserved
	carrier := opentracing.HTTPHeadersCarrier(make(map[string][]string))
	tracer := opentracing.GlobalTracer()
	if err := tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier); err != nil {
		return nil
	}
	removeBaggageItemDeleted(&carrier)
	carrierStr, err := json.Marshal(carrier)
	if err != nil {
		return nil
	}
	return map[string]string{SpanKey: string(carrierStr)}
}

// export metric sob, som before sending, argument protocal can choose from: kafka|nats|http
func ExportMetricsBeforeSending(span opentracing.Span, metricName string, protocol string) {
	now := time.Now().UnixNano() / 1e6
	if sobStr := span.BaggageItem("sob"); len(sobStr) != 0 {
		sob, err := strconv.ParseInt(sobStr, 10, 0)
		if err == nil {
			sob2cs.WithLabelValues(protocol, metricName, "sob2cs").Observe(float64(now) - float64(sob))
		}
	}
	if somStr := span.BaggageItem("som"); len(somStr) != 0 {
		som, err := strconv.ParseInt(somStr, 10, 0)
		if err == nil {
			som2cs.WithLabelValues(protocol, metricName, "som2cs").Observe(float64(now) - float64(som))
		}
	}
}

func extractSpanFromMsg(msg interface{}) (opentracing.SpanContext, error) {
	switch e := msg.(type) {
	case *kafka.Message:
		for _, header := range e.Headers {
			if header.Key == SpanKey {
				var carrier opentracing.HTTPHeadersCarrier
				err := json.Unmarshal(header.Value, &carrier)
				if err != nil {
					return nil, err
				}
				tracer := opentracing.GlobalTracer()
				// extract span from header
				clientContext, err := tracer.Extract(opentracing.HTTPHeaders, carrier)
				if err != nil {
					return nil, err
				} else {
					return clientContext, nil
				}
			}
		}
	}
	return nil, errors.New("no span")
}

func StartSpanFromMsgAfterReceived(metricName string, msg interface{}) opentracing.Span {
	now := time.Now().UnixNano() / 1e6
	nowStr := fmt.Sprintf("%d", now)
	clientContext, err := extractSpanFromMsg(msg)
	var span opentracing.Span
	if err != nil {
		span = opentracing.StartSpan(metricName)
	} else {
		span = opentracing.StartSpan(metricName, opentracing.FollowsFrom(clientContext))
	}
	if len(span.BaggageItem("sob")) == 0 {
		span.SetBaggageItem("sob", nowStr)
	}
	span.SetBaggageItem("som", nowStr)

	if csStr := span.BaggageItem("cs"); len(csStr) != 0 {
		cs, err := strconv.Atoi(csStr)
		if err == nil {
			cs2sr.WithLabelValues("kafka", metricName, "cs2sr").Observe(float64(now) - float64(cs))
		}
	}
	return span
}

// store a span into context.Context, so it can be passed to other place
func ContextWithSpan(ctx context.Context, span opentracing.Span) context.Context {
	return opentracing.ContextWithSpan(ctx, span)
}

// restore a span from context.Context, so it can be the parent of another child or follow span
func SpanFromContext(ctx context.Context) opentracing.Span {
	return opentracing.SpanFromContext(ctx)
}

// create a follow span from the span stored in context.Context
func StartSobSomSpan(ctx context.Context, operationName string) (opentracing.Span, context.Context) {
	nowStr := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
	span, ctx := StartSpanFromContext(ctx, operationName)
	if len(span.BaggageItem("sob")) == 0 {
		span.SetBaggageItem("sob", nowStr)
	}
	if len(span.BaggageItem("som")) == 0 {
		span.SetBaggageItem("som", nowStr)
	}
	return span, ContextWithSpan(ctx, span)
}

// create a follow span from the span stored in context.Context
func StartSomSpan(ctx context.Context, operationName string) (opentracing.Span, context.Context) {
	nowStr := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)
	span, ctx := StartSpanFromContext(ctx, operationName)
	if len(span.BaggageItem("som")) == 0 {
		span.SetBaggageItem("som", nowStr)
	}
	return span, ContextWithSpan(ctx, span)
}

// create a follow span from the span stored in context.Context
func StartSpanFromContext(ctx context.Context, operationName string,
	opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
	var span opentracing.Span
	if parentSpan := SpanFromContext(ctx); parentSpan != nil {
		opts = append(opts, opentracing.FollowsFrom(parentSpan.Context()))
		span = opentracing.StartSpan(operationName, opts...)
	} else {
		span = opentracing.StartSpan(operationName, opts...)
	}
	return span, ContextWithSpan(ctx, span)
}