腾讯mini项目-【指标监控服务重构】2023-07-20

今日已办

Trace

otelzap :opentelemetry-go-extra/otelzap at main · uptrace/opentelemetry-go-extra (github.com)

sh 复制代码
go get github.com/uptrace/opentelemetry-go-extra/otelzap

声明 Tracer ,一个应用创建一个 Tracer (需要从venus中来传来的trace_id构造

go 复制代码
var consumerTracer = otel.Tracer("profile-consumer",
		trace.WithInstrumentationAttributes(attribute.String("profile.work", "consumer")))

处理异常,日志记录 & Span 记录错误 & 设置状态

go 复制代码
func handlerErr(span trace.Span, ctx context.Context, msg string, err error) {
    // otelzap 集成zap
	otelzap.L().Ctx(ctx).Error(msg, zap.Error(err))
	span.RecordError(err)
	span.SetStatus(codes.Error, msg)
}

在每一个Handler中使用同一个 Tracer 创建Span

go 复制代码
func (profileCtx *ProfileContext) UnpackKafkaMessage(ctx context.Context) (needBreak bool, tpsStatus string, contextErr error) {
	var span trace.Span
    // 根Span,类型为内部
	profileCtx.Ctx, span = consumerTracer.Start(profileCtx.Ctx, "UnpackKafkaMessage",
		trace.WithNewRoot(), trace.WithSpanKind(trace.SpanKindInternal))
	defer span.End()
	if contextErr = json.Unmarshal(profileCtx.msg.Value, &profileCtx.Event); contextErr != nil {
		profileCtx.Status = state.StatusUnmarshalError
		handlerErr(span, profileCtx.Ctx, "unmarshal error", contextErr)
		return
	}
    // 设置属性
	if span.IsRecording() {
		span.SetAttributes(
			attribute.String("event.id", profileCtx.Event.ID),
		)
	}
	log.Logger.Info("[UnpackKafkaItem] unpack kafka item success", zap.Any("event", profileCtx.Event))
	return
}

Metric

根据组员 张锐添 提供的可用性方案和代码实现

  1. 设置metric PeriodicReader的上报间隔
  2. 创建新的 MeterProvider
  3. 使用 Int64ObservableGauge 在每个测量收集周期,异步记录一次 StatusAlive的 瞬时值
go 复制代码
func InitAvailabilityObserver() func() {

	ctx := context.Background()

	res, err := createResource(ctx)
	handleErr(err, "failed to create resource")

	otelAgentAddr := config.Profile.GetString("otelclient.otelExporterOtlpEndpoint")
	metricExp := createMetricExp(err, ctx, otelAgentAddr)
	handleErr(err, "Failed to create the otelclient metric exporter")

	mp := sdkmetric.NewMeterProvider(
		sdkmetric.WithResource(res),
		sdkmetric.WithReader(
			sdkmetric.NewPeriodicReader(
				metricExp,
				sdkmetric.WithInterval(10*time.Second), // 10s上报一次
				sdkmetric.WithTimeout(2*time.Second),   // 设置超时
			),
		),
	)

	const (
		StatusAlive = iota // 可用状态
	)

	_, err = mp.Meter("profile-availability-meter").Int64ObservableGauge(
		"profile/alive_observer",
		// 每个测量值收集周期,异步记录一次瞬时 int64 测量值
		metric.WithInt64Callback(func(ctx context.Context, observer metric.Int64Observer) error {
			observer.Observe(
				StatusAlive,
				// metric.WithAttributes(attribute.String("", "")),
			)
			return nil
		}),
	)
	handleErr(err, "Failed to create the ObservableGauge")

	log.Logger.Info("init availabilityObserver success")

	return func() {
		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		defer cancel()
		if err := mp.Shutdown(ctx); err != nil {
			otel.Handle(err)
		}
	}
}

在 SigNoz Web 的 Dashboards 配置 ClickHouse Query

sql 复制代码
SELECT
    toStartOfMinute(toTimeZone(toDateTime(timestamp_ms/1000), 'Asia/Shanghai')) AS minute,
    count(*)/6 AS result
FROM signoz_metrics.samples_v2
WHERE metric_name='profile_alive_observer' AND timestamp_ms >= 1000*toUnixTimestamp(toStartOfMinute(toTimeZone(now(), 'Asia/Shanghai') - INTERVAL 1 DAY))
GROUP BY minute
ORDER BY minute ASC

发现问题

请求 Venus 的 Post 接口,Profile 消费了两次,由于上报的json数据的data字段是数组(包含了多条数据)

使用 Int64Counter 记录消费总次数时(同步且递增),在服务重新启动后,测量值会重新从0开始

traefik 反向代理

yaml 复制代码
http:
  routers:
    frontend-router:
      rule: "PathPrefix(`/frontend`)"
      entrypoints:
        - web-entrypoint
      service: frontend-service
  services:
    frontend-service:
      loadBalancer:
        servers:
          - url: http://profile-frontend:3301

组会记录

  1. SRE报表- SLI SLO SLA (指标、具体范围或者限制, 协议)--- 具体以什么形式,可视化出来
  2. 服务(进程)的CPU、内存利用率等指标,shirou/gopsutil: psutil for golang (github.com)
  3. 重启服务,Counter计数器的值会从零开始计算
  4. traefik反向代理 :指定一个端口,前缀为'/'的请求都路由到frontend服务
  5. Log上报目标 - [郑兆隆]
  6. Grafana配置Ck的仪表盘
  7. Trace 收集到的 Span 的延时、错误率、吞吐量需要用指标 metric 来上报吗
  8. Profile的ToDo

明日待办

  1. 组长会议汇报项目进度和问题
相关推荐
郭京京4 小时前
goweb内置的 net/http 包
后端·go
dylan_QAQ4 小时前
Java转Go全过程06-工程管理
java·后端·go
中电金信6 小时前
中电金信:AI重构测试体系·智能化时代的软件工程新范式
人工智能·重构·软件工程
lypzcgf6 小时前
Coze源码分析-资源库-删除插件-后端源码-错误处理与总结
人工智能·后端·go·coze·coze源码分析·ai应用平台·agent平台
lypzcgf8 小时前
Coze源码分析-资源库-删除插件-后端源码-应用和领域服务层
后端·go·coze·coze插件·coze源码分析·智能体平台·ai应用平台
程序员爱钓鱼10 小时前
Go语言实战案例 — 工具开发篇:编写高可用日志收集脚本
后端·mongodb·go
今天头发还在吗12 小时前
【Go】:mac 环境下GoFrame安装开发工具 gf-cli——gf_darwin_arm64
macos·golang·go·gf-cli
通信小呆呆14 小时前
5G/6G时代的智能超表面:如何重构无线传播环境?
5g·重构·信息与通信·信号处理·超表面
程序员爱钓鱼20 小时前
Go语言实战案例-开发一个Markdown转HTML工具
前端·后端·go
一朵筋斗云1 天前
golang底层原理剖析
go