Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Makonike committed Aug 17, 2023
1 parent d80014f commit 05d6a0e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,106 @@
# jaeger-middleware
An extensively configurable Go web middleware that integrates Jaeger for distributed tracing.

## Usage

### Installation

```bash
go get github.com/makonike/jaeger-middleware
```

### Example

#### Server

```go
func main() {
tp, _ := middleware.TracerProvider("http://localhost:14268/api/traces", false)
otel.SetTracerProvider(tp)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Cleanly shutdown and flush telemetry when the application exits.
defer func(ctx context.Context) {
// Do not make the application hang when it is shutdown.
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
defer cancel()
if err := tp.Shutdown(ctx); err != nil {
fmt.Println("failed to shutdown TracerProvider: ", err)
}
}(ctx)

server := grpc.NewServer(
grpc.UnaryInterceptor(middleware.NewJaegerServerMiddleware().UnaryInterceptor),
grpc.StreamInterceptor(middleware.NewJaegerServerMiddleware().StreamInterceptor),
)
proto.RegisterTestServiceServer(server, new(test.UserService))
list, err := net.Listen("tcp", ":50055")
if err != nil {
log.Fatal(err)
}
err = server.Serve(list)
if err != nil {
log.Fatal(err)
}
}
```

#### Client
```go
func main() {
tp, _ := middleware.TracerProvider("http://localhost:14268/api/traces", false)
otel.SetTracerProvider(tp)
var addr string
addr = ":50055"
ctx := context.WithValue(context.Background(), "trace-id", "cdde169b504ec847521a2cf1d1ffa9f9")
req := &proto.GetReq{
Name: "xxx",
}
conn, err := grpc.Dial(addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(middleware.NewJaegerClientMiddleware().UnaryClientInterceptor),
grpc.WithStreamInterceptor(middleware.NewJaegerClientMiddleware().StreamClientInterceptor),
)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := proto.NewTestServiceClient(conn)
resp, err := client.Get(ctx, req)
assert.Nil(t, err)
}
```

## Configuration

At present, configuration exclusively relies on manipulation of environmental variables within the operating system. Hence, it is advisable to operate a distinct application within an individual container. This approach facilitates the accommodation of multiple applications on a single host, each employing distinct customized configurations.

- CLUSTER_NAME, default default_cluster
- NAMESPACE, default default_namespace
- DEPLOYMENT, default default_deployment
- SERVICE_NAME, default default_hostname
- ENVIRONMENT, default default_pod
- TRACE_HEADER, default trace-id

The identifying key for the trace_id propagated across the chain of communication.

- MAX_BOX_SIZE, default 10240

If the size of the input body or the returned error message exceeds the MAX_BOX_SIZE, it will not be logged.

- SERVER_ENABLED, default true

If configured as 'false,' the server middleware will remain inactive.

- CLIENT_ENABLED=dev, default true

If configured as 'false,' the client middleware will remain inactive.

## Effect

![img.png](/img/img.png)

## Reference

TODO
Binary file added img/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions middleware/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func DefaultOptions() Options {
if os.Getenv("ENVIRONMENT") != "" {
o.meta.Environment = os.Getenv("ENVIRONMENT")
}
if os.Getenv("TRACE_HEADER") != "" {
o.meta.Environment = os.Getenv("TRACE_HEADER")
}

o.maxBodySize = 10240
o.serverEnabled = true
Expand Down
4 changes: 2 additions & 2 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestMiddlewareClient(t *testing.T) {
var addr string
addr = ":50055"
ctx := context.WithValue(context.Background(), "trace-id", "cdde169b504ec847521a2cf1d1ffa9f9")
req1 := &proto.GetReq{
req := &proto.GetReq{
Name: "www3",
}
conn, err := grpc.Dial(addr,
Expand All @@ -67,7 +67,7 @@ func TestMiddlewareClient(t *testing.T) {
}
defer conn.Close()
client := proto.NewTestServiceClient(conn)
resp, err := client.Get(ctx, req1)
resp, err := client.Get(ctx, req)
assert.Nil(t, err)
assert.Equal(t, "www", resp.GetName())
}
Expand Down

0 comments on commit 05d6a0e

Please sign in to comment.