<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Go - 實(shí)現(xiàn)項(xiàng)目內(nèi)鏈路追蹤(二)

          共 2822字,需瀏覽 6分鐘

           ·

          2021-02-11 09:17

          上篇文章 Go - 實(shí)現(xiàn)項(xiàng)目內(nèi)鏈路追蹤 分享了,通過 鏈路 ID 可以將 請求信息響應(yīng)信息、調(diào)用第三方接口的信息、調(diào)試信息、執(zhí)行的 SQL 信息、執(zhí)行的 Redis 信息 串起來,記錄的具體參數(shù)在文件中都有介紹。

          這篇文章在上面的基礎(chǔ)上,新增 2 個功能點(diǎn)

          1. 新增將 調(diào)用 gRPC 接口信息 記錄到 Trace 中;
          2. 新增對記錄的敏感信息進(jìn)行脫敏處理;

          調(diào)用 gRPC 接口信息

          記錄參數(shù)

          Object,結(jié)構(gòu)如下:

          type?Grpc?struct?{
          ?Timestamp???string?????????????????`json:"timestamp"`?????????????//?時間,格式:2006-01-02 15:04:05
          ?Addr????????string?????????????????`json:"addr"`??????????????????//?地址
          ?Method??????string?????????????????`json:"method"`????????????????//?操作方法
          ?Meta????????metadata.MD????????????`json:"meta"`??????????????????//?Mate?信息
          ?Request?????map[string]interface{}?`json:"request"`???????????????//?請求信息
          ?Response????map[string]interface{}?`json:"response"`??????????????//?返回信息
          ?CostSeconds?float64????????????????`json:"cost_seconds"`??????????//?執(zhí)行時間(單位秒)
          ?Code????????string?????????????????`json:"err_code,omitempty"`????//?錯誤碼
          ?Message?????string?????????????????`json:"err_message,omitempty"`?//?錯誤信息
          }

          如何收集參數(shù)

          封裝了一個 grpclient 包:

          • 支持設(shè)置 DialTimeout;
          • 支持設(shè)置 UnaryInterceptor;
          • 支持設(shè)置 KeepaliveParams;
          • 支持設(shè)置 TransportCredentials;

          主要是在攔截器 Interceptor 中進(jìn)行收集。

          示例代碼

          實(shí)例化 gRPC client

          //?TODO?需從配置文件中獲取
          target?:=?"127.0.0.1:9988"
          secret?:=?"abcdef"

          clientInterceptor?:=?NewClientInterceptor(func(message?[]byte)?(authorization?string,?err?error)?{
          ?return?GenerateSign(secret,?message)
          })

          conn,?err?:=?grpclient.New(target,
          ?grpclient.WithKeepAlive(keepAlive),
          ?grpclient.WithDialTimeout(time.Second*5),
          ?grpclient.WithUnaryInterceptor(clientInterceptor.UnaryInterceptor),
          )

          return?&clientConn{
          ?conn:?conn,
          },?err

          調(diào)用具體方法

          //?核心:傳遞 core.Context 給 Interceptor 使用
          client?:=?hello.NewHelloClient(d.grpconn.Conn())
          client.SayHello(grpc.ContextWithValueAndTimeout(c,?time.Second*3),?&hello.HelloRequest{Name:?"Hello?World"})

          敏感信息脫敏

          敏感信息脫敏又稱為動態(tài)數(shù)據(jù)掩碼(Dynamic Data Masking,簡稱為DDM)能夠防止把敏感數(shù)據(jù)暴露給未經(jīng)授權(quán)的用戶。

          根據(jù)項(xiàng)目要求可以約定一些規(guī)范,例如:

          類型要求示例說明
          手機(jī)號前 3 后 4132****7986定長 11 位數(shù)字
          郵箱地址前 1 后 1l**[email protected]僅對 @ 之前的郵箱名稱進(jìn)行掩碼
          姓名隱姓*鴻章將姓氏隱藏
          密碼不輸出******
          銀行卡卡號前 6 后 4622888******5676銀行卡卡號最多 19 位數(shù)字
          身份證號前 1 后 11******7定長 18 位

          如何實(shí)現(xiàn)

          我現(xiàn)在的實(shí)現(xiàn)方案是:自定義 MarshalJSON(),歡迎大佬們提出更好的方案。

          示例代碼

          //?定義?Mobile?類型
          type?Mobile?string

          //?自定義?MarshalJSON()
          func?(m?Mobile)?MarshalJSON()?([]byte,?error)?{
          ?if?len(m)?!=?11?{
          ??return?[]byte(`"`?+?m?+?`"`),?nil
          ?}

          ?v?:=?fmt.Sprintf("%s****%s",?m[:3],?m[len(m)-4:])
          ?return?[]byte(`"`?+?v?+?`"`),?nil
          }

          測試

          type?message?struct?{
          ?Mobile????ddm.Mobile???`json:"mobile"`
          }

          msg?:=?new(message)
          msg.Mobile?=?ddm.Mobile("13288889999")

          marshal,?_?:=?json.Marshal(msg)
          fmt.Println(string(marshal))

          //?輸出:{"mobile":"132****9999"}

          小結(jié)

          本篇文章新增了 2 個實(shí)用的功能點(diǎn),大家趕緊使用起來吧。關(guān)于 敏感信息脫敏 期待各位大佬不吝賜教,提出更好的解決方案,謝謝!

          以上代碼都在 go-gin-api 項(xiàng)目中,地址:https://github.com/xinliangnote/go-gin-api


          推薦閱讀


          福利

          我為大家整理了一份從入門到進(jìn)階的Go學(xué)習(xí)資料禮包,包含學(xué)習(xí)建議:入門看什么,進(jìn)階看什么。關(guān)注公眾號 「polarisxu」,回復(fù)?ebook?獲??;還可以回復(fù)「進(jìn)群」,和數(shù)萬 Gopher 交流學(xué)習(xí)。

          瀏覽 47
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  人妻夜夜爽天天爽三区麻豆AV网站 | 伊人大香蕉99网 | 成人电影天天干 | 色婷婷香蕉在线一区二区 | 大香伊人中文字幕精品 |