Skip to content

xiajingge/observability

Repository files navigation

observability

📦 安装 (Installation)

使用 go get 安装最新版本:

go get -u github.com/xiajingge/observability

🚀 快速开始 (Quick Start)

基础用法

注意本示例中的collector的地址需要根据自己的实际来进行更改。

func main() {
	// Application entry point
	ctx := context.Background()
	cfg := observability.NewObservabilityConfig("test-demo", "v0.0.1", "localhost:4317")
	provider, err := observability.NewOTelProvider(ctx, cfg)
	if err != nil {
		panic(err)
	}

	tracer := provider.Tracer("otel-test-demo-tracer")
	meter := provider.Meter("otel-test-demo-meter")
	requestCounter, err := meter.Int64Counter("request_count", metric.WithDescription("Number of requests"))
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		requestCounter.Add(r.Context(), 1)
		ctx := r.Context()
		ctx, span := tracer.Start(ctx, "handleRequest")
		defer span.End()

		span.SetAttributes(attribute.String("method", r.Method))
		span.SetAttributes(attribute.String("path", r.URL.Path))

		// Simulate some work
		time.Sleep(100 * time.Millisecond)

		w.Write([]byte("Hello, OpenTelemetry!"))
	})

	log.Println("Server starting on :8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

项目架构

frame 下游的导出组件可参考本项目的collector.yaml进行编写。

About

一个封装好的可观测性(metric、trace)的工具

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages