blob: a9471859187ba09c579a6a6e5d40f2d722cf0fb9 [file] [log] [blame]
Rohan Agrawalc32d9932020-06-15 11:01:47 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// File contains utility functions to support Open Tracing in conjunction with
18// Enhanced Logging based on context propagation
19
20package log
21
22import (
23 "context"
24 "errors"
25 "github.com/opentracing/opentracing-go"
26 jtracing "github.com/uber/jaeger-client-go"
27 jcfg "github.com/uber/jaeger-client-go/config"
Rohan Agrawalc32d9932020-06-15 11:01:47 +000028 "io"
Girish Kumaradc3ba12020-06-15 14:22:55 +000029 "io/ioutil"
Rohan Agrawalc32d9932020-06-15 11:01:47 +000030 "os"
Girish Kumaradc3ba12020-06-15 14:22:55 +000031 "strings"
Rohan Agrawalc32d9932020-06-15 11:01:47 +000032)
33
Girish Kumaradc3ba12020-06-15 14:22:55 +000034const (
35 RootSpanNameKey = "op-name"
36)
37
38// Flag indicating whether to extract Log Fields from Span embedded in the received Context
39var extractLogFieldsFromContext bool = true
40
41// Flag indicating whether to process Span related operations; to save CPU cycles when disabled
42var processSpanOperations bool = true
43
44// Jaeger complaint Logger instance to redirect logs to Default Logger
45type traceLogger struct {
46 logger *clogger
47}
48
49func (tl traceLogger) Error(msg string) {
50 tl.logger.Error(context.Background(), msg)
51}
52
53func (tl traceLogger) Infof(msg string, args ...interface{}) {
54 // Tracing logs should be performed only at Debug Verbosity
55 tl.logger.Debugf(context.Background(), msg, args...)
56}
57
Rohan Agrawalc32d9932020-06-15 11:01:47 +000058// This method will start the Tracing for a component using Component name injected from the Chart
59// The close() method on returned Closer instance should be called in defer mode to gracefully
60// terminate tracing on component shutdown
Girish Kumaradc3ba12020-06-15 14:22:55 +000061func InitTracingAndLogCorrelation(tracePublishEnabled bool, traceAgentAddress string, logCorrelationEnabled bool) (io.Closer, error) {
62 if !tracePublishEnabled && !logCorrelationEnabled {
63 defaultLogger.Info(context.Background(), "Skipping Global Tracer initialization as both Trace publish and Log correlation are configured as disabled")
64 extractLogFieldsFromContext = false
65 processSpanOperations = false
66 return ioutil.NopCloser(strings.NewReader("")), nil
67 }
68
69 if !logCorrelationEnabled {
70 defaultLogger.Info(context.Background(), "Disabling Log Fields extraction from context as configured")
71 extractLogFieldsFromContext = false
72 }
73
Rohan Agrawalc32d9932020-06-15 11:01:47 +000074 componentName := os.Getenv("COMPONENT_NAME")
75 if componentName == "" {
76 return nil, errors.New("Unable to retrieve PoD Component Name from Runtime env")
77 }
78
79 // Use basic configuration to start with; will extend later to support dynamic config updates
80 cfg := jcfg.Configuration{}
81
Girish Kumaradc3ba12020-06-15 14:22:55 +000082 var err error
83 var jReporterConfig jcfg.ReporterConfig
84 var jReporterCfgOption jtracing.Reporter
Rohan Agrawalc32d9932020-06-15 11:01:47 +000085
Girish Kumaradc3ba12020-06-15 14:22:55 +000086 // Attempt Trace Agent Address only if Trace Publishing is enabled; else directly use Loopback IP
87 if tracePublishEnabled {
88 jReporterConfig = jcfg.ReporterConfig{LocalAgentHostPort: traceAgentAddress, LogSpans: true}
89 jReporterCfgOption, err = jReporterConfig.NewReporter(componentName, jtracing.NewNullMetrics(), traceLogger{logger: defaultLogger})
90
91 if err != nil {
92 defaultLogger.Errorw(context.Background(), "Unable to create Reporter with given Trace Agent address",
93 Fields{"error": err, "address": traceAgentAddress})
94 // The Reporter initialization may fail due to Invalid Agent address or non-existent Agent (DNS lookup failure).
95 // It is essential for Tracer Instance to still start for correct Span propagation needed for log correlation.
96 // Thus, falback to use loopback IP for Reporter initialization before throwing back any error
97 tracePublishEnabled = false
98 }
99 }
100
101 if !tracePublishEnabled {
102 jReporterConfig.LocalAgentHostPort = "127.0.0.1:6831"
103 jReporterCfgOption, err = jReporterConfig.NewReporter(componentName, jtracing.NewNullMetrics(), traceLogger{logger: defaultLogger})
104 if err != nil {
105 return nil, errors.New("Failed to initialize Jaeger Tracing due to Reporter creation error : " + err.Error())
106 }
107 }
108
109 // To start with, we are using Constant Sampling type
110 samplerParam := 0 // 0: Do not publish span, 1: Publish
111 if tracePublishEnabled {
112 samplerParam = 1
113 }
114 jSamplerConfig := jcfg.SamplerConfig{Type: "const", Param: float64(samplerParam)}
115 jSamplerCfgOption, err := jSamplerConfig.NewSampler(componentName, jtracing.NewNullMetrics())
116 if err != nil {
117 return nil, errors.New("Unable to create Sampler : " + err.Error())
118 }
119
120 return cfg.InitGlobalTracer(componentName, jcfg.Reporter(jReporterCfgOption), jcfg.Sampler(jSamplerCfgOption))
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000121}
122
123// Extracts details of Execution Context as log fields from the Tracing Span injected into the
124// context instance. Following log fields are extracted:
125// 1. Operation Name : key as 'op-name' and value as Span operation name
126// 2. Operation Id : key as 'op-id' and value as 64 bit Span Id in hex digits string
127//
128// Additionally, any tags present in Span are also extracted to use as log fields e.g. device-id.
129//
130// If no Span is found associated with context, blank slice is returned without any log fields
131func ExtractContextAttributes(ctx context.Context) []interface{} {
Girish Kumaradc3ba12020-06-15 14:22:55 +0000132 if !extractLogFieldsFromContext {
133 return make([]interface{}, 0)
134 }
135
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000136 attrMap := make(map[string]interface{})
137
138 if ctx != nil {
139 if span := opentracing.SpanFromContext(ctx); span != nil {
140 if jspan, ok := span.(*jtracing.Span); ok {
Girish Kumaradc3ba12020-06-15 14:22:55 +0000141 // Add Log fields for operation identified by Root Level Span (Trace)
142 opId := jspan.SpanContext().TraceID().String()
143 opName := jspan.BaggageItem(RootSpanNameKey)
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000144
Girish Kumaradc3ba12020-06-15 14:22:55 +0000145 taskId := jspan.SpanContext().SpanID().String()
146 taskName := jspan.OperationName()
147
148 if opName == "" {
149 span.SetBaggageItem(RootSpanNameKey, taskName)
150 opName = taskName
151 }
152
153 attrMap["op-id"] = opId
154 attrMap["op-name"] = opName
155
156 // Add Log fields for task identified by Current Span, if it is different
157 // than operation
158 if taskId != opId {
159 attrMap["task-id"] = taskId
160 attrMap["task-name"] = taskName
161 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000162
163 for k, v := range jspan.Tags() {
Girish Kumaradc3ba12020-06-15 14:22:55 +0000164 // Ignore the special tags added by Jaeger, middleware (sampler.type, span.*) present in the span
165 if strings.HasPrefix(k, "sampler.") || strings.HasPrefix(k, "span.") || k == "component" {
166 continue
167 }
168
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000169 attrMap[k] = v
170 }
171 }
172 }
173 }
174
175 return serializeMap(attrMap)
176}
177
Girish Kumaradc3ba12020-06-15 14:22:55 +0000178// Method to inject additional log fields into Span e.g. device-id
179func EnrichSpan(ctx context.Context, keyAndValues ...Fields) {
180 span := opentracing.SpanFromContext(ctx)
181 if span != nil {
182 for _, field := range keyAndValues {
183 for k, v := range field {
184 span.SetTag(k, v)
185 }
186 }
187 }
188}
189
190// Method to inject Error into the Span in event of any operation failure
191func MarkSpanError(ctx context.Context, err error) {
192 span := opentracing.SpanFromContext(ctx)
193 span.SetTag("error", true)
194 span.SetTag("err", err)
195}
196
197// Creates a Child Span from Parent Span embedded in passed context. Should be used before starting a new major
198// operation in Synchronous or Asynchronous mode (go routine), such as following:
199// 1. Start of all implemented External API methods unless using a interceptor for auto-injection of Span (Server side impl)
200// 2. Just before calling an Third-Party lib which is invoking a External API (etcd, kafka)
201// 3. In start of a Go Routine responsible for performing a major task involving significant duration
202// 4. Any method which is suspected to be time consuming...
203func CreateChildSpan(ctx context.Context, taskName string, keyAndValues ...Fields) (opentracing.Span, context.Context) {
204 if !processSpanOperations {
205 return opentracing.NoopTracer{}.StartSpan(taskName), ctx
206 }
207
208 parentSpan := opentracing.SpanFromContext(ctx)
209 childSpan, newCtx := opentracing.StartSpanFromContext(ctx, taskName)
210
211 if parentSpan == nil || parentSpan.BaggageItem(RootSpanNameKey) == "" {
212 childSpan.SetBaggageItem(RootSpanNameKey, taskName)
213 }
214
215 EnrichSpan(newCtx, keyAndValues...)
216 return childSpan, newCtx
217}
218
219// Creates a Async Child Span with Follows-From relationship from Parent Span embedded in passed context.
220// Should be used only in scenarios when
221// a) There is dis-continuation in execution and thus result of Child span does not affect the Parent flow at all
222// b) The execution of Child Span is guaranteed to start after the completion of Parent Span
223// In case of any confusion, use CreateChildSpan method
224// Some situations where this method would be suitable includes Kafka Async RPC call, Propagation of Event across
225// a channel etc.
226func CreateAsyncSpan(ctx context.Context, taskName string, keyAndValues ...Fields) (opentracing.Span, context.Context) {
227 if !processSpanOperations {
228 return opentracing.NoopTracer{}.StartSpan(taskName), ctx
229 }
230
231 var asyncSpan opentracing.Span
232 var newCtx context.Context
233
234 parentSpan := opentracing.SpanFromContext(ctx)
235
236 // We should always be creating Aysnc span from a Valid parent span. If not, create a Child span instead
237 if parentSpan == nil {
238 defaultLogger.Warn(context.Background(), "Async span must be created with a Valid parent span only")
239 asyncSpan, newCtx = opentracing.StartSpanFromContext(ctx, taskName)
240 } else {
241 // Use Background context as the base for Follows-from case; else new span is getting both Child and FollowsFrom relationship
242 asyncSpan, newCtx = opentracing.StartSpanFromContext(context.Background(), taskName, opentracing.FollowsFrom(parentSpan.Context()))
243 }
244
245 if parentSpan == nil || parentSpan.BaggageItem(RootSpanNameKey) == "" {
246 asyncSpan.SetBaggageItem(RootSpanNameKey, taskName)
247 }
248
249 EnrichSpan(newCtx, keyAndValues...)
250 return asyncSpan, newCtx
251}
252
253// Extracts the span from Source context and injects into the supplied Target context.
254// This should be used in situations wherein we are calling a time-sensitive operation (etcd update) and hence
255// had a context.Background() used earlier to avoid any cancellation/timeout of operation by passed context.
256// This will allow propagation of span with a different base context (and not the original context)
257func WithSpanFromContext(targetCtx, sourceCtx context.Context) context.Context {
258 span := opentracing.SpanFromContext(sourceCtx)
259 return opentracing.ContextWithSpan(targetCtx, span)
260}
261
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000262// Utility method to convert log Fields into array of interfaces expected by zap logger methods
263func serializeMap(fields Fields) []interface{} {
264 data := make([]interface{}, len(fields)*2)
265 i := 0
266 for k, v := range fields {
267 data[i] = k
268 data[i+1] = v
269 i = i + 2
270 }
271 return data
272}