instabeego

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 6 Imported by: 2

README

Instana instrumentation for Beego framework

This module contains middleware to instrument HTTP services written with https://github.com/beego/beego.

PkgGoDev

Installation

To add the module to your go.mod file run the following command in your project directory:

$ go get github.com/instana/go-sensor/instrumentation/instabeego

Usage

Web server instrumentation

// create a sensor
t := instana.InitCollector(&instana.Options{
    Service:           "beego-server",
    EnableAutoProfile: true,
})

// instrument the web server
instabeego.InstrumentWebServer(t)

// define API 
beego.Get("/foo", func(ctx *beecontext.Context) {/* ... */})

// Run beego application
beego.Run() 
// ...

Full example

HTTP client instrumentation

// create a sensor
t := instana.InitCollector(&instana.Options{
    Service:           "my-http-client",
    EnableAutoProfile: true,
})

// get the parent span and inject into the request context
ctx := instana.ContextWithSpan(context.Background(), /* parent span */)

// create a new http request using beego
req := httplib.NewBeegoRequestWithCtx(ctx, "https://www.instana.com", http.MethodGet)

// instrument the client request
instabeego.InstrumentRequest(sensor, req)

// execute the client request and get the response
_, err := req.Response()
// ...

Full example

Documentation

Overview

Package instabeego provides Instana instrumentation for the beego framework.

Example (HttpClientInstrumentation)

This example shows how to instrument beego httplib module (HTTP client) with Instana tracing

package main

import (
	"context"
	"log"
	"net/http"

	"github.com/beego/beego/v2/client/httplib"
	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instabeego"
	"github.com/opentracing/opentracing-go/ext"
)

func main() {
	t := instana.InitCollector(&instana.Options{
		Service:           "my-http-client",
		EnableAutoProfile: true,
	})

	// Every call should start with an entry span (https://docs.instana.io/quick_start/custom_tracing/#always-start-new-traces-with-entry-spans)
	// Normally this would be your HTTP/GRPC/message queue request span, but here we need to create it explicitly, since an HTTP client call is
	// an exit span. And all exit spans must have a parent entry span.
	sp := t.StartSpan("client-call")
	sp.SetTag(string(ext.SpanKind), "entry")

	defer sp.Finish()

	// Inject the parent span into request context
	ctx := instana.ContextWithSpan(context.Background(), sp)

	req := httplib.NewBeegoRequestWithCtx(ctx, "https://www.instana.com", http.MethodGet)
	instabeego.InstrumentRequest(t, req)

	_, err := req.Response()
	if err != nil {
		log.Fatalf("failed to GET https://www.instana.com: %s", err)
	}
}
Output:

Example (ServerInstrumentation)

This example shows how to instrument a beego web server.

// (c) Copyright IBM Corp. 2023

//go:build go1.18
// +build go1.18

package main

import (
	beego "github.com/beego/beego/v2/server/web"
	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instabeego"
)

// UserController represents the router for user APIs in the given example
type UserController struct {
	beego.Controller
}

// GetUserById is a sample handler function for the example
func (u *UserController) GetUserById() {
	u.Ctx.WriteString("GetUserById")
}

// This example shows how to instrument a beego web server.
func main() {
	t := instana.InitCollector(&instana.Options{
		Service:           "beego-server",
		EnableAutoProfile: true,
	})
	// This will add instana.TracingHandlerFunc() function as middleware for every API.
	instabeego.InstrumentWebServer(t)

	beego.CtrlGet("api/user/:id", (*UserController).GetUserById)
	beego.Run()
}
Output:

Index

Examples

Constants

View Source
const Version = "0.7.1"

Version is the instrumentation module semantic version

Variables

This section is empty.

Functions

func InstrumentRequest

func InstrumentRequest(sensor instana.TracerLogger, req *httplib.BeegoHTTPRequest)

InstrumentRequest wrap the original BeegoHTTPRequest transport with instana.RoundTripper().

func InstrumentWebServer

func InstrumentWebServer(sensor instana.TracerLogger)

InstrumentWebServer wraps beego's handlers execution. Adds tracing context and handles entry span. It should be added as a first Middleware to the beego, before defining handlers.

Types

This section is empty.

Jump to

Keyboard shortcuts

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