Skip to content

Commit

Permalink
feat: add docs (README, comment in examples)
Browse files Browse the repository at this point in the history
  • Loading branch information
anhvietnguyennva authored Aug 31, 2023
2 parents 72f2e9f + f8fada1 commit 8d5a61b
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 26 deletions.
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Kyber Trace GO Lib

## Overview
This is a library that makes it easier to integrate with Kyber's self-hosted open tracing

## Add this lib to your project
- Step 1:
```
$ export GOPRIVATE=github.com/KyberNetwork/kyber-trace-go
```
- Step 2: Add file `tools/tools.go` with content:
```
package tools
import (
_ "github.com/KyberNetwork/kyber-trace-go/tools"
)
```
- Step 3:
```
$ go mod tidy
$ go mod vendor
```

## Update to latest version
```
$ go get -u github.com/KyberNetwork/kyber-trace-go
$ go mod vendor
```

## Docker build
Please adjust your `RUN go mod download` statement in your Dockerfile to `RUN GO111MODULE=on GOPRIVATE=github.com/KyberNetwork/kybers-trace-error go mod download`

## How to use

### Init global `TracerProvider`
In order to push spans and traces to Kyber's self-hosted agent, you have to initialize a TracerProvider. When you add the following statement into your code: `import _ "github.com/KyberNetwork/kyber-trace-go/pkg/tracer"`, `kyber-trace-go` will initialize a new TraceProvider, set it to global TracerProvider of `otel` package. Whenever you want to get the Tracer to start (or get) a span, just use the function `tracer.Tracer()` (refer the example at https://github.com/KyberNetwork/kyber-trace-go/blob/main/example/tracer.go for more details).
### Init global `MeterProvider`
In order to push customized metrics to Kyber's self-hosted agent, you have to initialize a MeterProvider. When you add the following statement into your code: `import _ "github.com/KyberNetwork/kyber-trace-go/pkg/metric"`, `kyber-trace-go` will initialize a new MeterProvider, set it to global MeterProvider of `otel` package. Whenever you want to get the Meter to push customized metrics, just use the function `metric.Meter()` (refer the example at https://github.com/KyberNetwork/kyber-trace-go/blob/main/example/meter.go for more details).
### Configurations
When initializing the global TracerProvider and global MeterProvider, `kyber-trace-go` loads configurations from the following environment variables:
- OTEL_ENABLED: `kyber-trace-go` only initializes global `TracerProvider` and global `MeterProvider` if `OTEL_ENABLED = true`
- OTEL_AGENT_HOST: The host of the agent where traces, spans, customized metrics will be sent to. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be injected via helm chart, and you don't need to set it in your `values.yaml` in kyber-application. Otherwise, you have to set it yourself.
- OTEL_METRIC_AGENT_GRPC_PORT: The gRPC port of the agent where customized metrics will be sent to. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be injected via helm chart, and you don't need to set it in your `values.yaml` in kyber-application. Otherwise, you have to set it yourself.
- OTEL_METRIC_AGENT_HTTP_PORT: The HTTP port of the agent where customized metrics will be sent to. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be injected via helm chart, and you don't need to set it in your `values.yaml` in kyber-application. Otherwise, you have to set it yourself.
- OTEL_TRACE_AGENT_GRPC_PORT: The gRPC port of the agent where traces, spans will be sent to. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be injected via helm chart, and you don't need to set it in your `values.yaml` in kyber-application. Otherwise, you have to set it yourself.
- OTEL_TRACE_AGENT_HTTP_PORT: The HTTP port of the agent where traces, spans will be sent to. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be injected via helm chart, and you don't need to set it in your `values.yaml` in kyber-application. Otherwise, you have to set it yourself.
- OTEL_INSECURE: Disables client transport security for HTTP/gRPC connection when connecting to agent. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be set to `true` automatically.
- OTEL_PROTOCOL: Specify which protocol will be used to connect to the agent. Enum: `grpc`, `http`. The default value is `grpc`. Only add this environment variable to your `value.yaml` in kyber-application when you want to use `http`.
- OTEL_SERVICE_NAME: Name of your service which can be used in your query in grafana jaeger. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be injected via helm chart, and you don't need to set it in your `values.yaml` in `kyber-application`. Otherwise, you have to set it yourself.
- OTEL_SERVICE_VERSION: The current version of your service. If you are using helm chart with dependency `base-service` version `0.5.15` or later, this environment variable will be injected via helm chart, and you don't need to set it in your `values.yaml` in `kyber-application`. Otherwise, you have to set it to your current image tag.
- OTEL_TRACE_SAMPLE_RATE: The default value is `0.5`. If you want your all traces and spans will be recorded, set `OTEL_TRACE_SAMPLE_RATE = 1`

### For gin framework
If your application is using gin framework, you can use [this package](https://pkg.go.dev/go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin) to trace requests to your application automatically. You can see the example at https://github.com/KyberNetwork/kyber-trace-go/blob/main/example/gin.go

### For GORM
OpenTelemetry GORM is designed is easy to use and provides a simple API for instrumenting GORM applications, making it possible for developers to quickly add observability to their applications without having to write a lot of code. To instrument GORM, you need to install the plugin provided by otelgorm:
```
import (
"github.com/uptrace/opentelemetry-go-extra/otelgorm"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic(err)
}
if err := db.Use(otelgorm.NewPlugin()); err != nil {
panic(err)
}
```
And then use db.WithContext(ctx) to propagate the active span via context:
```
var num int
if err := db.WithContext(ctx).Raw("SELECT 42").Scan(&num).Error; err != nil {
panic(err)
}
```
47 changes: 47 additions & 0 deletions example/gin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package example

import (
"net/http"
"os"

_ "github.com/KyberNetwork/kyber-trace-go/tools" // this is important
"github.com/gin-gonic/gin"
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
)

func GinFramework() {
// Please port forward collector from develop environment to local then set environment variables before run this example:
// kubectl -n observability port-forward daemonset/opentelemetry-collector-agent 4317:4317 4315:4315
// export OTEL_ENABLED=true
// export OTEL_AGENT_HOST=127.0.0.1
// export OTEL_SERVICE_NAME=your_service_name
// export OTEL_SERVICE_VERSION=0.1.0
// export OTEL_TRACE_SAMPLE_RATE=1
// export OTEL_TRACE_AGENT_GRPC_PORT=4317
// export OTEL_METRIC_AGENT_GRPC_PORT=4315
// export OTEL_INSECURE=true

// When you deploy your service using helm chart with base-service from version 0.5.15, the following variables will be injected directly via helm chart:
// OTEL_AGENT_HOST, OTEL_SERVICE_NAME, OTEL_SERVICE_VERSION, OTEL_TRACE_AGENT_GRPC_PORT, OTEL_METRIC_AGENT_GRPC_PORT, OTEL_INSECURE
// You just need to set OTEL_ENABLED, OTEL_TRACE_SAMPLE_RATE.

server := newServer()
err := server.Run()
if err != nil {
panic(err)
}
}

func newServer() *gin.Engine {
server := gin.New()
if os.Getenv("OTEL_ENABLED") == "true" {
server.Use(otelgin.Middleware(
os.Getenv("OTEL_SERVICE_NAME"),
))
}
rg := server.Group("/api/v1")
rg.GET("/greeting", func(c *gin.Context) {
c.AbortWithStatusJSON(http.StatusOK, "Hello World")
})
return server
}
17 changes: 16 additions & 1 deletion example/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@ import (
)

func PushMetric() {
counter, err := metric.Meter().Int64Counter("example_count_edge_4")
// Please port forward collector from develop environment to local then set environment variables before run this example:
// kubectl -n observability port-forward daemonset/opentelemetry-collector-agent 4317:4317 4315:4315
// export OTEL_ENABLED=true
// export OTEL_AGENT_HOST=127.0.0.1
// export OTEL_SERVICE_NAME=your_service_name
// export OTEL_SERVICE_VERSION=0.1.0
// export OTEL_TRACE_SAMPLE_RATE=1
// export OTEL_TRACE_AGENT_GRPC_PORT=4317
// export OTEL_METRIC_AGENT_GRPC_PORT=4315
// export OTEL_INSECURE=true

// When you deploy your service using helm chart with base-service from version 0.5.15, the following variables will be injected directly via helm chart:
// OTEL_AGENT_HOST, OTEL_SERVICE_NAME, OTEL_SERVICE_VERSION, OTEL_TRACE_AGENT_GRPC_PORT, OTEL_METRIC_AGENT_GRPC_PORT, OTEL_INSECURE
// You just need to set OTEL_ENABLED, OTEL_TRACE_SAMPLE_RATE.

counter, err := metric.Meter().Int64Counter("example_count_metric")
if err != nil {
panic(err)
}
Expand Down
17 changes: 16 additions & 1 deletion example/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ import (
)

func Tracing() {
// Please port forward collector from develop environment to local then set environment variables before run this example:
// kubectl -n observability port-forward daemonset/opentelemetry-collector-agent 4317:4317 4315:4315
// export OTEL_ENABLED=true
// export OTEL_AGENT_HOST=127.0.0.1
// export OTEL_SERVICE_NAME=your_service_name
// export OTEL_SERVICE_VERSION=0.1.0
// export OTEL_TRACE_SAMPLE_RATE=1
// export OTEL_TRACE_AGENT_GRPC_PORT=4317
// export OTEL_METRIC_AGENT_GRPC_PORT=4315
// export OTEL_INSECURE=true

// When you deploy your service using helm chart with base-service from version 0.5.15, the following variables will be injected directly via helm chart:
// OTEL_AGENT_HOST, OTEL_SERVICE_NAME, OTEL_SERVICE_VERSION, OTEL_TRACE_AGENT_GRPC_PORT, OTEL_METRIC_AGENT_GRPC_PORT, OTEL_INSECURE
// You just need to set OTEL_ENABLED, OTEL_TRACE_SAMPLE_RATE.

ctx := context.Background()

parentSpanCtx, parentSpan := tracer.Tracer().Start(ctx, "parent span")
Expand All @@ -22,5 +37,5 @@ func Tracing() {

childSpan.End()
parentSpan.End()
time.Sleep(5 * time.Second) // wait to ensure parentSpan was pushed
time.Sleep(10 * time.Second) // wait to ensure parentSpan was pushed
}
35 changes: 28 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,61 @@ module github.com/KyberNetwork/kyber-trace-go
go 1.21.0

require (
github.com/gin-gonic/gin v1.9.1
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.43.0
go.opentelemetry.io/otel v1.17.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.39.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v0.39.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.16.0
go.opentelemetry.io/otel/metric v1.16.0
go.opentelemetry.io/otel/metric v1.17.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/sdk/metric v0.39.0
go.opentelemetry.io/otel/trace v1.16.0
go.opentelemetry.io/otel/trace v1.17.0
google.golang.org/grpc v1.57.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 8d5a61b

Please sign in to comment.