blob: 559d757d5c6563bfa85f539bbc3b54d75f8e2e45 [file] [log] [blame]
// Copyright 2023 The LUCI Authors.
//
// 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.
// Package tracing contains helper for reporting OpenTelemetry tracing spans.
package tracing
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
var tracer = otel.Tracer("go.chromium.org/luci/cv")
// Start opens a tracing span.
//
// Finish it with End, usually like this:
//
// ctx, span := tracing.Start(ctx, "..."")
// defer func() { tracing.End(span, err) }()
func Start(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span) {
return tracer.Start(ctx, name, trace.WithAttributes(attrs...))
}
// End records the error (if any) and closes the span.
func End(span trace.Span, err error, attrs ...attribute.KeyValue) {
span.SetAttributes(attrs...)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
span.End()
}