Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added README file for instabeego package #487

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions instrumentation/instabeego/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Instana instrumentation for Beego framework
=============================================

This module contains middleware to instrument HTTP services written with [`https://github.com/beego/beego`](https://github.com/beego/beego).

[![PkgGoDev](https://pkg.go.dev/badge/github.com/instana/go-sensor/instrumentation/instabeego)][godoc]

Installation
------------

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

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

Usage
-----

## Web server instrumentation

```go
// 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][serverInstrumentationExample]

## HTTP client instrumentation

```go
// 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][clientInstrumentationExample]




[godoc]: https://pkg.go.dev/github.com/instana/go-sensor/instrumentation/instabeego
[serverInstrumentationExample]: https://pkg.go.dev/github.com/instana/go-sensor/instrumentation/instabeego#example-package-ServerInstrumentation
[clientInstrumentationExample]: https://pkg.go.dev/github.com/instana/go-sensor/instrumentation/instabeego#example-package-HttpClientInstrumentation
9 changes: 6 additions & 3 deletions instrumentation/instabeego/example_http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import (

// This example shows how to instrument beego httplib module (HTTP client) with Instana tracing
func Example_httpClientInstrumentation() {
sensor := instana.NewSensor("my-http-client")
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 := sensor.Tracer().StartSpan("client-call")
sp := t.StartSpan("client-call")
sp.SetTag(string(ext.SpanKind), "entry")

defer sp.Finish()
Expand All @@ -32,7 +35,7 @@ func Example_httpClientInstrumentation() {
ctx := instana.ContextWithSpan(context.Background(), sp)

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

_, err := req.Response()
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions instrumentation/instabeego/example_web_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ func (u *UserController) GetUserById() {

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

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