opentelemetry

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2021 License: Apache-2.0 Imports: 24 Imported by: 3

README

Go Agent for OpenTelemetry

Go Agent provides a set of complementary features for OpenTelemetry instrumentation

Package net/hyperhttp

HTTP server

The server instrumentation relies on the http.Handler component of the server declarations.

import (
    "net/http"

    "github.com/gorilla/mux"
    "github.com/hypertrace/goagent/instrumentation/opentelemetry/net/hyperhttp"
    "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

func main() {
    // ...

    r := mux.NewRouter()
    r.Handle("/foo/{bar}", otelhttp.NewHandler(
        hyperhttp.WrapHandler(fooHandler),
        "/foo/{bar}",
    ))

    // ...
}
HTTP client

The client instrumentation relies on the http.Transport component of the HTTP client in Go.

import (
    "net/http"
    "github.com/hypertrace/goagent/instrumentation/opentelemetry/net/hyperhttp"
    "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

// ...

client := http.Client{
    Transport: otelhttp.NewTransport(
        hyperhttp.WrapDefaultTransport(),
        ...
    ),
}

req, _ := http.NewRequest("GET", "http://example.com", nil)

res, err := client.Do(req)

// ...
Running HTTP examples

In terminal 1 run the client:

go run ./net/http/examples/client/main.go

In terminal 2 run the server:

go run ./net/http/examples/server/main.go

Package google.golang.org/hypergrpc

GRPC server

The server instrumentation relies on the grpc.UnaryServerInterceptor component of the server declarations.


server := grpc.NewServer(
    grpc.UnaryInterceptor(
        hypergrpc.WrapUnaryServerInterceptor(
            otelgrpc.UnaryServerInterceptor(myTracer),
        ),
    ),
)
GRPC client

The client instrumentation relies on the http.Transport component of the HTTP client in Go.

import (
    // ...

    hypergrpc "github.com/hypertrace/goagent/instrumentation/opentelemetry/google.golang.org/hypergrpc"
    "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
    "google.golang.org/grpc"
)

func main() {
    // ...
    conn, err := grpc.Dial(
        address,
        grpc.WithInsecure(),
        grpc.WithBlock(),
        grpc.WithUnaryInterceptor(
            hypergrpc.WrapUnaryClientInterceptor(
                otelgrpc.UnaryClientInterceptor(myTracer),
            ),
        ),
    )
    if err != nil {
        log.Fatalf("could not dial: %v", err)
    }
    defer conn.Close()

    client := pb.NewCustomClient(conn)

    // ...
}
Running GRPC examples

In terminal 1 run the client:

go run ./google.golang.org/hypergrpc/examples/client/main.go

In terminal 2 run the server:

go run ./google.golang.org/hypergrpc/examples/server/main.go

Documentation

Index

Examples

Constants

View Source
const TracerDomain = "org.hypertrace.goagent"

TracerDomain represents the tracer name for the Hypertrace instrumentation

Variables

View Source
var StartSpan = startSpan(otel.GetTracerProvider)

Functions

func Init

func Init(cfg *config.AgentConfig) func()

Init initializes opentelemetry tracing and returns a shutdown function to flush data immediately on a termination signal.

Example
cfg := config.Load()
cfg.ServiceName = config.String("my_example_svc")
cfg.DataCapture.HttpHeaders.Request = config.Bool(true)
cfg.Reporting.Endpoint = config.String("http://api.traceable.ai:9411/api/v2/spans")

shutdown := Init(cfg)
defer shutdown()
Output:

func RegisterService

func RegisterService(serviceName string, resourceAttributes map[string]string) (sdk.StartSpan, error)

RegisterService creates tracerprovider for a new service and returns a func which can be used to create spans

Example
cfg := config.Load()
cfg.ServiceName = config.String("my_example_svc")
cfg.DataCapture.HttpHeaders.Request = config.Bool(true)
cfg.Reporting.Endpoint = config.String("http://api.traceable.ai:9411/api/v2/spans")
cfg.Reporting.TraceReporterType = config.TraceReporterType_ZIPKIN

shutdown := Init(cfg)

_, err := RegisterService("custom_service", map[string]string{"test1": "val1"})
if err != nil {
	log.Fatalf("Error while initializing service: %v", err)
}

defer shutdown()
Output:

func SpanFromContext

func SpanFromContext(ctx context.Context) sdk.Span

Types

type Span

type Span struct {
	trace.Span
}

func (*Span) IsNoop

func (s *Span) IsNoop() bool

func (*Span) SetAttribute

func (s *Span) SetAttribute(key string, value interface{})

func (*Span) SetError

func (s *Span) SetError(err error)

Directories

Path Synopsis
database
github.com
google.golang.org
net

Jump to

Keyboard shortcuts

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