Skip to content

Commit

Permalink
tips: replace img domain
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed May 28, 2023
1 parent 06f8536 commit b569f6e
Show file tree
Hide file tree
Showing 20 changed files with 166 additions and 318 deletions.
182 changes: 15 additions & 167 deletions docker/go-otel/opentelemetry.md
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`追踪演示
34 changes: 17 additions & 17 deletions docker/grafana-promethues/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
整体的简易架构如下:

<p align="center">
<img src="http://cdn.tigerb.cn/20210531132351.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210531132351.png" style="width:100%">
</p>

- Grafana:作为UI,提供了丰富的监控面板。
Expand Down Expand Up @@ -183,11 +183,11 @@ prometheus http://localhost:9090
举个例子,比如通过如下的操作,我们就可以看见我们Go服务的`Goroutines`监控。

<p align="center">
<img src="http://cdn.tigerb.cn/20210529155608.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529155608.png" style="width:100%">
</p>

<p align="center">
<img src="http://cdn.tigerb.cn/20210529160638.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160638.png" style="width:100%">
</p>

但是呢,Grafana提供了更丰富的监控面板,接着我们来搭建一个简单的Go服务监控。
Expand All @@ -206,71 +206,71 @@ admin

进入首页:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529160749.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160749.png" style="width:100%">
</p>

添加数据源:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529160803.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160803.png" style="width:100%">
</p>

<p align="center">
<img src="http://cdn.tigerb.cn/20210529160812.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160812.png" style="width:100%">
</p>

选择数据源为Prometheus:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529160823.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160823.png" style="width:100%">
</p>

填写Prometheus服务的地址:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529160842.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160842.png" style="width:100%">
</p>

点击添加:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529160856.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160856.png" style="width:100%">
</p>

切换到Dashboards面板,选择导入一个面板:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529160917.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160917.png" style="width:100%">
</p>

接着我们就可以看见一个已经存在的面板了,这个面板是Prometheus自身监控的面板。
<p align="center">
<img src="http://cdn.tigerb.cn/20210529160925.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529160925.png" style="width:100%">
</p>

接着,我们来创建一个我们自己Go服务的面板,首先创建一个Go服务的目录(保证隔离和可读性,相当于namespace):
<p align="center">
<img src="http://cdn.tigerb.cn/20210529163049.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529163049.png" style="width:100%">
</p>

创建一个Go服务的面板:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529163058.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529163058.png" style="width:100%">
</p>

创建一个指标视图:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529163105.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529163105.png" style="width:100%">
</p>

选择视图类型为折线统计图,选择数据指标为`go_gotoutines`:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529163114.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529163114.png" style="width:100%">
</p>

只展示我们的Go样例服务数据,这里采用的是 语法:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529163122.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529163122.png" style="width:100%">
</p>

以此类推,我们就创建了一些列监控视图数据,比如goroutine数量、线程数量、heap内存数据、stack内存数据、mcache数据、mspan数据、GC数据等等,如下:
<p align="center">
<img src="http://cdn.tigerb.cn/20210529163131.png" style="width:100%">
<img src="http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20210529163131.png" style="width:100%">
</p>


Expand Down
2 changes: 1 addition & 1 deletion go/framework/beego-code-read/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,5 @@ beego.Router()

> 备注:go导入包相当于入栈过程,先import后执行init
![http://cdn.tigerb.cn/beego_2.png](http://cdn.tigerb.cn/beego_2.png)
![http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/beego_2.png](http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/beego_2.png)

2 changes: 1 addition & 1 deletion go/framework/gin-code-read/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# iris源码阅读

![](http://cdn.tigerb.cn/20190704211526.png)
![](http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20190704211526.png)

```go
package main
Expand Down
2 changes: 1 addition & 1 deletion go/framework/iris-code-read/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# iris源码阅读

![](http://cdn.tigerb.cn/20190704211456.png)
![](http://blog-1251019962.cos.ap-beijing.myqcloud.com/qiniu_img_2022/20190704211456.png)

```
package main
Expand Down
6 changes: 3 additions & 3 deletions go/go-learn/experience/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -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函数
Expand All @@ -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>

### 生成火焰图
Expand Down Expand Up @@ -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服务内存泄露排查

Expand Down
Loading

0 comments on commit b569f6e

Please sign in to comment.