In the previous article, Go – to achieve link tracking sharing within the project. Through the link ID, request information, response information, call third-party interface information, debugging information, SQL information executed, Redis information executed can be strung together. The specific parameters recorded are introduced in the file.

Based on the above, this article adds two new function points:

  1. New willInformation about the gRPC interfacerecordedTrace;
  2. Added desensitization of recorded sensitive information;

Information about the gRPC interface

Record parameters

Object, the structure is as follows:

Type Grpc struct {Timestamp string 'json' :" Timestamp "' 2006-01-02 15:04:05 Addr String 'json:" Addr "' // address Method string 'json:" Method "' // Meta metadata.MD 'json:" Meta" Request map[string]interface{} 'json:" Request "' // Request information Response Map [string]interface{} 'json:" Response "' // CostSeconds float64 'JSON :"cost_seconds"' // Execution time in seconds Code string 'json:"err_code, omitEmpty "' // Error Code Message String 'json:"err_message,omitempty"' // Error message}Copy the code

How to Collect parameters

Encapsulates a GrpClient package:

  • Allows you to setDialTimeout;
  • Allows you to setUnaryInterceptor;
  • Allows you to setKeepaliveParams;
  • Allows you to setTransportCredentials;

The main collection is in the Interceptor.

The sample code

Example Instantiate gRPC Client

// The TODO file is available. 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, }, errCopy the code

Calling concrete methods

/ / core: Pass core.context to the Interceptor using client := hello.newhelloclient (d.rpconn.conn ()) client.SayHello(grpc.ContextWithValueAndTimeout(c, time.Second*3), &hello.HelloRequest{Name: "Hello World"})Copy the code

Desensitization of sensitive information

Sensitive information desensitization, also known as Dynamic Data Masking, or DDM for short, can prevent sensitive Data from being exposed to unauthorized users.

Some specifications can be agreed upon according to project requirements, for example:

type requirements The sample instructions
Mobile phone no. After the first 3 4 132 * * * * 7986 The fixed length is 11 digits
Email address 1 after 1 before l**[email protected] Mask only mailbox names before @
The name Hidden surname * hong chapter Hide the last name
password No output * * * * * *
Bank card Number 4 after the first six 622888 * * * * * * 5676 The bank card number has a maximum of 19 digits
Id number 1 after 1 before 1 * * * * * * 7 Fixed length of 18

How to implement

My current implementation scheme is: custom MarshalJSON(), welcome you to come up with a better scheme.

The sample code

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 }Copy the code

test

type message struct { Mobile ddm.Mobile `json:"mobile"` } msg := new(message) msg.Mobile = ddm.Mobile("13288889999") Marshal(MSG) FMT.Println(string(marshal)) {"mobile":"132****9999"}Copy the code

summary

This article has two new useful feature points, so get started. We look forward to your advice on desensitization of sensitive information and better solutions. Thank you!

The above code is in the Go-gin-API project at github.com/xinliangnot…