2019-04-20 19:53:34 -04:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
// Copyright 2018 Microsoft Corporation
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-03-25 15:15:46 -04:00
|
|
|
// Tracer represents an HTTP tracing facility.
|
|
|
|
type Tracer interface {
|
|
|
|
NewTransport(base *http.Transport) http.RoundTripper
|
|
|
|
StartSpan(ctx context.Context, name string) context.Context
|
|
|
|
EndSpan(ctx context.Context, httpStatusCode int, err error)
|
2019-04-20 19:53:34 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:15:46 -04:00
|
|
|
var (
|
|
|
|
tracer Tracer
|
|
|
|
)
|
2019-04-20 19:53:34 -04:00
|
|
|
|
2020-03-25 15:15:46 -04:00
|
|
|
// Register will register the provided Tracer. Pass nil to unregister a Tracer.
|
|
|
|
func Register(t Tracer) {
|
|
|
|
tracer = t
|
2019-04-20 19:53:34 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:15:46 -04:00
|
|
|
// IsEnabled returns true if a Tracer has been registered.
|
2019-04-20 19:53:34 -04:00
|
|
|
func IsEnabled() bool {
|
2020-03-25 15:15:46 -04:00
|
|
|
return tracer != nil
|
2019-04-20 19:53:34 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:15:46 -04:00
|
|
|
// NewTransport creates a new instrumenting http.RoundTripper for the
|
|
|
|
// registered Tracer. If no Tracer has been registered it returns nil.
|
|
|
|
func NewTransport(base *http.Transport) http.RoundTripper {
|
|
|
|
if tracer != nil {
|
|
|
|
return tracer.NewTransport(base)
|
2019-04-20 19:53:34 -04:00
|
|
|
}
|
2020-03-25 15:15:46 -04:00
|
|
|
return nil
|
2019-04-20 19:53:34 -04:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:15:46 -04:00
|
|
|
// StartSpan starts a trace span with the specified name, associating it with the
|
|
|
|
// provided context. Has no effect if a Tracer has not been registered.
|
2019-04-20 19:53:34 -04:00
|
|
|
func StartSpan(ctx context.Context, name string) context.Context {
|
2020-03-25 15:15:46 -04:00
|
|
|
if tracer != nil {
|
|
|
|
return tracer.StartSpan(ctx, name)
|
|
|
|
}
|
2019-04-20 19:53:34 -04:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-03-25 15:15:46 -04:00
|
|
|
// EndSpan ends a previously started span stored in the context.
|
|
|
|
// Has no effect if a Tracer has not been registered.
|
2019-04-20 19:53:34 -04:00
|
|
|
func EndSpan(ctx context.Context, httpStatusCode int, err error) {
|
2020-03-25 15:15:46 -04:00
|
|
|
if tracer != nil {
|
|
|
|
tracer.EndSpan(ctx, httpStatusCode, err)
|
2019-04-20 19:53:34 -04:00
|
|
|
}
|
|
|
|
}
|