trace

package module
v1.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 23 Imported by: 132

README

OpenTelemetry Google Cloud Trace Exporter

Docs Apache License

OpenTelemetry Google Cloud Trace Exporter allows the user to send collected traces and spans to Google Cloud.

Google Cloud Trace is a distributed tracing backend system. It helps developers to gather timing data needed to troubleshoot latency problems in microservice & monolithic architectures. It manages both the collection and lookup of gathered trace data.

This exporter package assumes your application is already instrumented with the OpenTelemetry SDK. Once you get ready to export OpenTelemetry data, you can add this exporter to your application.

Setup

Google Cloud Trace is a managed service provided by Google Cloud Platform. The end-to-end set up guide with OpenTelemetry is available on the official GCP docs, so this document goes through the exporter set up.

Usage

Once you import the trace exporter package, create and install a new export pipeline, then you can start tracing. If you are running in a GCP environment, the exporter will automatically authenticate using the environment's service account. If not, you will need to follow the instruction in Authentication.

package main

import (
	"context"
	"log"

	texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"

	"go.opentelemetry.io/otel"
	sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func main() {
	exporter, err := texporter.New()
	if err != nil {
		log.Fatalf("unable to set up tracing: %v", err)
	}
	tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
	defer tp.Shutdown(context.Background())

	otel.SetTracerProvider(tp)

	tracer := tp.Tracer("example.com/trace")
	ctx := context.TODO()
	ctx, span := tracer.Start(ctx, "foo")
	defer span.End()

	// Do some work.
}

Authentication

The Google Cloud Trace exporter depends upon google.FindDefaultCredentials, so the service account is automatically detected by default, but also the custom credential file (so called service_account_key.json) can be detected with specific conditions. Quoting from the document of google.FindDefaultCredentials:

  • A JSON file whose path is specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable.
  • A JSON file in a location known to the gcloud command-line tool. On Windows, this is %APPDATA%/gcloud/application_default_credentials.json. On other systems, $HOME/.config/gcloud/application_default_credentials.json.

When running code locally, you may need to specify a Google Project ID in addition to GOOGLE_APPLICATION_CREDENTIALS. This is best done using an environment variable (e.g. GOOGLE_CLOUD_PROJECT) and the WithProjectID method, e.g.:

projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
exporter, err := texporter.New(texporter.WithProjectID(projectID))
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version added in v0.10.0

func Version() string

Version is the current release version of the OpenTelemetry Operations Trace Exporter in use.

func WithAttributeMapping added in v1.4.0

func WithAttributeMapping(mapping AttributeMapping) func(o *options)

WithAttributeMapping configures how to map OpenTelemetry span attributes to google cloud trace span attributes. By default, it maps to attributes that are used prominently in the trace UI.

func WithContext

func WithContext(ctx context.Context) func(o *options)

WithContext sets the context that trace exporter and metric exporter relies on.

func WithDestinationProjectQuota added in v1.11.0

func WithDestinationProjectQuota() func(o *options)

WithDestinationProjectQuota enables per-request usage of the destination project's quota. For example, when setting the gcp.project.id resource attribute.

func WithErrorHandler added in v1.0.0

func WithErrorHandler(handler otel.ErrorHandler) func(o *options)

WithErrorHandler sets the hook to be called when there is an error occurred on uploading the span data to Stackdriver. If no custom hook is set, errors are logged.

func WithProjectID

func WithProjectID(projectID string) func(o *options)

WithProjectID sets Google Cloud Platform project as projectID. Without using this option, it automatically detects the project ID from the default credential detection process. Please find the detailed order of the default credentail detection proecess on the doc: https://godoc.org/golang.org/x/oauth2/google#FindDefaultCredentials

func WithTimeout

func WithTimeout(t time.Duration) func(o *options)

WithTimeout sets the timeout for trace exporter and metric exporter If unset, it defaults to a 12 second timeout.

func WithTraceClientOptions

func WithTraceClientOptions(opts []option.ClientOption) func(o *options)

WithTraceClientOptions sets additionial client options for tracing.

Types

type AttributeMapping added in v1.4.0

type AttributeMapping func(attribute.Key) attribute.Key

AttributeMapping determines how to map from OpenTelemetry span attribute keys to cloud trace attribute keys.

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

Exporter is a trace exporter that uploads data to Stackdriver.

TODO(yoshifumi): add a metrics exporter once the spec definition process and the sampler implementation are done.

func New added in v1.0.0

func New(opts ...Option) (*Exporter, error)

New creates a new Exporter thats implements trace.Exporter.

func (*Exporter) ExportSpans

func (e *Exporter) ExportSpans(ctx context.Context, spanData []sdktrace.ReadOnlySpan) error

ExportSpans exports a ReadOnlySpan to Stackdriver Trace.

func (*Exporter) Shutdown added in v0.12.0

func (e *Exporter) Shutdown(ctx context.Context) error

Shutdown waits for exported data to be uploaded.

For our purposes it closed down the client.

type Option

type Option func(*options)

Option is function type that is passed to the exporter initialization function.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL