golang 集成sentry:http.Client

http.Client 是 Go 标准库 HTTP 客户端实现, sentry-go也没有这个组件,所以需要自己实现。 我们只需要对 http.Transport 进行包装即可, 完整代码如下

go 复制代码
package main

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"net/http"
	"time"

	"github.com/getsentry/sentry-go"
)

type tracingTransport struct {
	http.RoundTripper
}

func NewTracingTransport(roundTripper http.RoundTripper) *tracingTransport {
	return &tracingTransport{RoundTripper: roundTripper}
}

func (t *tracingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	operationName := fmt.Sprintf("HTTP %s %s", req.Method, req.URL.String())
	span := sentry.StartSpan(req.Context(), operationName)
	defer span.Finish()

	span.SetTag("url", req.URL.String())
	if span.Data == nil {
		span.Data = make(map[string]interface{})
	}
	// reading body from the request body and fill it again
	var body []byte
	var err error
	if req.Body != nil {
		body, err = io.ReadAll(req.Body)
		if err != nil {
			return nil, err
		}
	}
	// Be careful with including sensitive information in the span,
	// request body and response may have private user data, which we wouldn't want to expose,
	// authorization header also is a good example of sensitive data.
	span.Data["body"] = string(body)

	req.Body = io.NopCloser(bytes.NewBuffer(body))
	// adding sentry header for distributed tracing
	req.Header.Add("sentry-trace", span.TraceID.String())

	response, err := t.RoundTripper.RoundTrip(req)

	span.Data["http_code"] = response.StatusCode
	// could additionally add the response to the span data

	return response, err
}

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Debug:              true,
		Dsn:                "https://a5eac4fa3396cbfac8fb4baa6a9c03a3@o4504291071688704.ingest.sentry.io/4506715873804288",
		AttachStacktrace:   true,
		EnableTracing:      true,
		SampleRate:         1.0,
		TracesSampleRate:   1.0,
		ProfilesSampleRate: 1.0,
	})
	if err != nil {
		log.Fatalf("sentry.Init: %s", err)
	}
	defer sentry.Flush(2 * time.Second)

	client := &http.Client{
		Transport: NewTracingTransport(http.DefaultTransport),
	}

	res, err := client.Get("http://httpbin.org/get")
	if err != nil {
		log.Fatalf("client Get: %s", err)
	}
	defer res.Body.Close()

	body, err := io.ReadAll(res.Body)
	if err != nil {
		log.Fatalf("io.ReadAll: %s", err)
	}
	fmt.Println(string(body))
}

参考:

https://anymindgroup.com/news/tech-blog/15724/

相关推荐
青灯文案117 分钟前
前端 HTTP 请求由 Nginx 反向代理和 API 网关到后端服务的流程
前端·nginx·http
Narutolxy39 分钟前
深入探讨 Go 中的高级表单验证与翻译:Gin 与 Validator 的实践之道20241223
开发语言·golang·gin
Hello.Reader1 小时前
全面解析 Golang Gin 框架
开发语言·golang·gin
小林熬夜学编程2 小时前
【Linux网络编程】第十四弹---构建功能丰富的HTTP服务器:从状态码处理到服务函数扩展
linux·运维·服务器·c语言·网络·c++·http
njnu@liyong12 小时前
图解HTTP-HTTP报文
网络协议·计算机网络·http
ZachOn1y13 小时前
计算机网络:应用层 —— 应用层概述
计算机网络·http·https·应用层·dns
hkNaruto1 天前
【P2P】【Go】采用go语言实现udp hole punching 打洞 传输速度测试 ping测试
golang·udp·p2p
入 梦皆星河1 天前
go中常用的处理json的库
golang
Graceful_scenery1 天前
https双向认证
服务器·网络·网络协议·http·https
海绵波波1071 天前
Gin-vue-admin(2):项目初始化
vue.js·golang·gin