forked from TIGERB/easy-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
166 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,181 +1,29 @@ | ||
# 一探究竟新一代可观测标准OpenTelemetry | ||
|
||
指标 Promethues | ||
## 什么是可观测 | ||
|
||
``` | ||
go get github.com/prometheus/client_golang/prometheus/promhttp | ||
``` | ||
观察系统状态 | ||
基于系统状态定位问题 | ||
|
||
```Go | ||
package main | ||
指标 | ||
日志 | ||
追踪 | ||
|
||
import ( | ||
"net/http" | ||
## 什么是`OpenTelemetry` | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
) | ||
指标使用`promethues`采集,`grafana`看板展示 | ||
|
||
func main() { | ||
// 对外提供/metrics接口 | ||
http.Handle("/metrics", promhttp.Handler()) | ||
http.ListenAndServe(":2112", nil) | ||
} | ||
``` | ||
追踪使用基于`OpenTracing` | ||
|
||
``` | ||
启动Go服务,curl请求接口: | ||
|
||
curl http://localhost:2112/metrics | ||
``` | ||
## 可观测之指标 | ||
|
||
``` | ||
获取到监控指标数据如下: | ||
### 基于原生`promethues`sdk的指标采集演示 | ||
|
||
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. | ||
# TYPE go_gc_duration_seconds summary | ||
go_gc_duration_seconds{quantile="0"} 2.8697e-05 | ||
go_gc_duration_seconds{quantile="0.25"} 3.8094e-05 | ||
go_gc_duration_seconds{quantile="0.5"} 0.000125819 | ||
go_gc_duration_seconds{quantile="0.75"} 0.000190862 | ||
go_gc_duration_seconds{quantile="1"} 0.0098972 | ||
go_gc_duration_seconds_sum 0.025042382 | ||
go_gc_duration_seconds_count 45 | ||
......略 | ||
``` | ||
### 基于`OpenTelemetry`sdk的`promethues`指标采集演示 | ||
|
||
追踪 OpenTracing | ||
## 可观测之追踪 | ||
|
||
```Go | ||
``` | ||
### 基于`OpenTracing`sdk的`jaeger`追踪演示 | ||
|
||
|
||
OpenTelemetry 指标/追踪 | ||
|
||
```Go | ||
package main | ||
|
||
import ( | ||
// 导入net/http包 | ||
"context" | ||
"http-demo/demov1" | ||
"net/http" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
|
||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
"google.golang.org/grpc" | ||
|
||
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/exporters/jaeger" | ||
"go.opentelemetry.io/otel/exporters/prometheus" | ||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/propagation" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
tracesdk "go.opentelemetry.io/otel/sdk/trace" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.17.0" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
metricsdk "go.opentelemetry.io/otel/sdk/metric" | ||
) | ||
|
||
var tracer *tracesdk.TracerProvider | ||
|
||
var meter metric.Meter | ||
|
||
func init() { | ||
// 初始化追踪tracer | ||
// https://github.com/open-telemetry/opentelemetry-go/blob/main/example/jaeger/main.go | ||
// Create the Jaeger exporter | ||
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint("http://jaeger-demo:14268/api/traces"))) | ||
if err != nil { | ||
panic(err) | ||
return | ||
} | ||
tracer = tracesdk.NewTracerProvider( | ||
tracesdk.WithBatcher(exp), | ||
tracesdk.WithResource(resource.NewWithAttributes( | ||
semconv.SchemaURL, | ||
semconv.ServiceNameKey.String("http-demo"), | ||
)), | ||
) | ||
|
||
// 初始化指标meter | ||
mexp, err := prometheus.New() | ||
if err != nil { | ||
panic(err) | ||
} | ||
meter = metricsdk.NewMeterProvider(metricsdk.WithReader(mexp)).Meter("http-demo") | ||
} | ||
|
||
// 常见框架集成opentelemetry SDK | ||
// https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/instrumentation | ||
|
||
func main() { | ||
otel.SetTracerProvider(tracer) | ||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
defer tracer.Shutdown(ctx) | ||
|
||
// 集成指标 | ||
// https://github.com/open-telemetry/opentelemetry-go/blob/main/example/prometheus/main.go | ||
// 创建一个接口访问计数器 | ||
urlCouter, _ := meter.Int64Counter("api_query_couter", metric.WithDescription("QPS")) | ||
|
||
// /v1/demo接口 业务逻辑handler | ||
demoHandler := func(w http.ResponseWriter, r *http.Request) { | ||
// 记录接口 QPS | ||
opt := metric.WithAttributes(attribute.Key("service_name").String("http-demo"), attribute.Key("url").String("/v1/demo")) | ||
urlCouter.Add(context.Background(), 1, opt) // 计数 | ||
|
||
// 调用demo grpc接口 | ||
name, err := demoGrpcReq() | ||
if err != nil { | ||
w.Write([]byte(err.Error())) | ||
} | ||
// 写入响应内容 | ||
w.Write([]byte(name)) | ||
} | ||
|
||
// 集成指标 | ||
// https://github.com/open-telemetry/opentelemetry-go/blob/main/example/prometheus/main.go | ||
go (func() { | ||
httpmux := http.NewServeMux() | ||
httpmux.Handle("/metrics", promhttp.Handler()) | ||
http.ListenAndServe(":6061", httpmux) | ||
})() | ||
|
||
// 集成链路追踪 | ||
// https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/instrumentation/net/http/otelhttp/example/server/server.go | ||
otelHandler := otelhttp.NewHandler(http.HandlerFunc(demoHandler), "otelhttp demo test") | ||
http.Handle("/v1/demo", otelHandler) | ||
|
||
// 启动一个http服务并监听6060端口 这里第二个参数可以指定handler | ||
http.ListenAndServe(":6060", nil) | ||
} | ||
|
||
func demoGrpcReq() (string, error) { | ||
conn, err := grpc.Dial("grpc-demo:1010", | ||
grpc.WithInsecure(), | ||
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()), | ||
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()), | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
// 泄露 | ||
defer conn.Close() | ||
|
||
client := demov1.NewGreeterClient(conn) | ||
resp, err := client.SayHello(context.TODO(), &demov1.HelloRequest{ | ||
Name: "http request", | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
return resp.GetMessage(), nil | ||
} | ||
|
||
|
||
``` | ||
### 基于`OpenTelemetry`sdk的`jaeger`追踪演示 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ perf report -n --stdio | |
``` | ||
|
||
<p> | ||
<img src="http://cdn.tigerb.cn/20220121152729.png" width="100%"> | ||
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20220121152729.png" width="100%"> | ||
</p> | ||
|
||
除此之外,查看实时top函数 | ||
|
@@ -71,7 +71,7 @@ perf top -p 6 | |
``` | ||
|
||
<p> | ||
<img src="http://cdn.tigerb.cn/20220121152934.png" width="100%"> | ||
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20220121152934.png" width="100%"> | ||
</p> | ||
|
||
### 生成火焰图 | ||
|
@@ -103,7 +103,7 @@ git clone [email protected]:brendangregg/FlameGraph.git && cd ./FlameGraph | |
``` | ||
|
||
<p> | ||
<img src="http://cdn.tigerb.cn/20220122220117.png" width="100%"> | ||
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20220122220117.png" width="100%"> | ||
</p> | ||
## Go服务内存泄露排查 | ||
|
||
|
Oops, something went wrong.