opencensus

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

README

Go Agent for OpenCensus

Go Agent provides a set of complementary features for OpenCensus instrumentation

Package net/http

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/opencensus/net/hyperhttp"
	ochttp "go.opencensus.io/plugin/ochttp"
)

func main() {
    // ...

    r := mux.NewRouter()
    r.Handle("/foo/{bar}", &ochttp.Handler{
        Handler: hyperhttp.WrapHandler(http.HandlerFunc(fooHandler)),
    })

    // ...
}
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/net/hyperhttp"
    "go.opencensus.io/plugin/ochttp"
)

// ...

client := http.Client{
    Transport: &ochttp.Transport{
        Base: hyperhttp.WrapTransport(http.DefaultTransport),
    },
}

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/hyperhttp/examples/client/main.go

In terminal 2 run the server:

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

Package google.golang.org/grpc

GRPC server

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

import (
    // ...

    "github.com/hypertrace/goagent/instrumentation/opencensus/google.golang.org/hypergrpc"
    "go.opencensus.io/plugin/ocgrpc"
    "google.golang.org/grpc"
)


server := grpc.NewServer(
    grpc.UnaryInterceptor(
        grpc.StatsHandler(hypergrpc.WrapServerHandler(&ocgrpc.ServerHandler{})),
    ),
)
GRPC client

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

import (
    // ...

    "github.com/hypertrace/goagent/instrumentation/google.golang.org/hypergrpc"
    "go.opencensus.io/plugin/ocgrpc"
    "google.golang.org/grpc"
)

func main() {
    // ...
    conn, err := grpc.Dial(
        address,
        grpc.WithInsecure(),
        grpc.WithBlock(),
        grpc.WithStatsHandler(hypergrpc.WrapClientHandler(&ocgrpc.ClientHandler{})),
    )
    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

This section is empty.

Variables

This section is empty.

Functions

func Init

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

Init initializes opencensus 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("https://api.traceable.ai:9411/api/v2/spans")

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

func NewHttpOperationMetricsHandler added in v0.12.0

func NewHttpOperationMetricsHandler() sdk.HttpOperationMetricsHandler

func SpanFromContext

func SpanFromContext(ctx context.Context) sdk.Span

func StartSpan

func StartSpan(ctx context.Context, name string, opts *sdk.SpanOptions) (context.Context, sdk.Span, func())

Types

type Span

type Span struct {
	*trace.Span
}

func (*Span) AddEvent added in v0.6.0

func (s *Span) AddEvent(name string, _ time.Time, attributes map[string]interface{})

There is no method matching adding span events in oc.

func (*Span) GetAttributes added in v0.6.0

func (s *Span) GetAttributes() sdk.AttributeList

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)

func (*Span) SetStatus added in v0.5.0

func (s *Span) SetStatus(code sdk.Code, message string)

Borrowed from https://github.com/open-telemetry/opentelemetry-go/blob/d18c13/bridge/opencensus/internal/span.go#L75

Directories

Path Synopsis
database
google.golang.org
net

Jump to

Keyboard shortcuts

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