restic/vendor/github.com/Azure/go-autorest/tracing/tracing.go

68 lines
1.9 KiB
Go
Raw Normal View History

2019-01-27 20:07:57 +00: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"
)
// 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-01-27 20:07:57 +00:00
}
var (
tracer Tracer
)
2019-01-27 20:07:57 +00:00
// Register will register the provided Tracer. Pass nil to unregister a Tracer.
func Register(t Tracer) {
tracer = t
2019-04-24 10:32:52 +00:00
}
// IsEnabled returns true if a Tracer has been registered.
2019-01-27 20:07:57 +00:00
func IsEnabled() bool {
return tracer != nil
2019-01-27 20:07:57 +00: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-01-27 20:07:57 +00:00
}
return nil
2019-01-27 20:07:57 +00: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-01-27 20:07:57 +00:00
func StartSpan(ctx context.Context, name string) context.Context {
if tracer != nil {
return tracer.StartSpan(ctx, name)
}
2019-01-27 20:07:57 +00:00
return ctx
}
// EndSpan ends a previously started span stored in the context.
// Has no effect if a Tracer has not been registered.
2019-01-27 20:07:57 +00:00
func EndSpan(ctx context.Context, httpStatusCode int, err error) {
if tracer != nil {
tracer.EndSpan(ctx, httpStatusCode, err)
2019-01-27 20:07:57 +00:00
}
}