logreplay

package module
v0.0.0-...-5c29987 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 19 Imported by: 0

README

Log Replay Plugin

For interface refactoring or modification, sometimes it is desired to validate using live requests.

One way is to integrate with a traffic replay platform for traffic replay, but it involves data storage and cost settlement issues.

The gateway provides a plugin for instant traffic forwarding, forwarding a certain proportion of live requests to the test interface.

Use Cases:

  • Interface refactoring or modification, using live traffic for validation
  • Online debugging, forwarding live traffic

Technical Solution

Traffic replay needs to be done after the main request is completed. The first thought is to implement it in the gateway forwarding logic.

However, following the principle of not invading core logic with edge functions, it will definitely not be implemented in the gateway forwarding logic (as it would be costly to implement). So we use local forwarding to implement traffic replay functionality at a low cost.

By default, it will be forwarded to the {original interface}_replay interface. Adding the _replay suffix distinguishes it from the original interface and facilitates management, indicating that it is a temporary interface.

img_1.png origin image

Usage

Import the Plugin in the main.go file of the gateway project
  • Add import
import (
    _ "trpc.group/trpc-go/trpc-gateway/plugin/logreplay"
)
  • tRPC framework configuration file, enable the logreplay interceptor.

Note: Be sure to register it in server.service.filter, not in server.filter.

global:                             # Global configuration
server:                             # Server configuration
  filter:                          # Interceptor list for all service processing functions
  service:                         # Business services provided by the service, can have multiple
    - name: trpc.inews.trpc.gateway      # Route name of the service
      filter:
        - logreplay # Gateway plugin registered in the service filter, so that it can be dynamically loaded in router.yaml
plugins:                            # Plugin configuration
  log:                                            # Log configuration
  gateway:                          # Plugin type is gateway
    logreplay:                      # Log replay plugin
Configure the replay forwarding interface

After the original interface

In the gateway routing configuration file router.yaml, configure the replay forwarding interface and the logreplay plugin.
router: # Routing configuration
  - method: /v1/user/info
    id: "xxxxxx"
    target_service:
      - service: trpc.user.service1
    plugins:
      - name: logreplay # Route-level plugin
  - method: /v1/user/info_replay # Configuration for replay forwarding interface
    id: "xxxxxx"
    target_service:
      - service: trpc.user.service2
    plugins:
client: # Upstream service configuration, consistent with trpc protocol
  - name: trpc.user.service
    plugins:
      - name: logreplay # Service-level configuration
        props:
          scale: # Replay ratio, e.g., 1% should be filled as 1
          # Whether to pass through the original response body.
          # POST request:
          #    Content-Type: application/json, then an origin_rsp_body field of type string will be added to the original JSON request body
          #    Content-Type: application/x-www-form-urlencoded, then a URL-encoded origin_rsp_body field will be added to the original query request body
          #    Other Content-Type: the original request will be appended to the query parameters, adding an origin_rsp_body field with the value of the URL-encoded response body
          # Other requests (GET, etc.):
          #    The original request will be appended to the query parameters, adding an origin_rsp_body field with the value of the URL-encoded response body
          pass_through_response: true
          # Timeout for replay requests, default is 500 milliseconds
          timeout: 500
plugins:
  - name: logreplay # Global configuration
The plugin can also be used through the gateway console.

img.png

Documentation

Overview

Package logreplay Log Replay Plugin

Index

Constants

This section is empty.

Variables

View Source
var DefaultConnPoolSize = 1000

DefaultConnPoolSize default connection pool size

View Source
var New = func() (*LogReplay, error) {
	l := &LogReplay{
		ConnPool: fhttp.NewConnPool(DefaultConnPoolSize),
	}
	return l, nil
}

New creates a new log replay

Functions

This section is empty.

Types

type LogReplay

type LogReplay struct {
	ConnPool fhttp.Pool
}

LogReplay traffic replay

func (*LogReplay) CopyRequest

func (lr *LogReplay) CopyRequest(ctx context.Context, fctx *fasthttp.RequestCtx,
	opts *Options) (*fasthttp.Request, error)

CopyRequest copies the request

func (*LogReplay) Replay

func (lr *LogReplay) Replay(ctx context.Context, newReq *fasthttp.Request, opts *Options) error

Replay replays the request

func (*LogReplay) ServerFilter

func (lr *LogReplay) ServerFilter(ctx context.Context, req interface{},
	handler filter.ServerHandleFunc) (interface{}, error)

ServerFilter server interceptor

type Options

type Options struct {
	// Traffic replay ratio, in percentage. For example, 0.01 represents 1 in 10,000.
	Scale float64 `yaml:"scale" json:"scale"`
	// Whether to pass through the original response body. If true, the original response body will be passed through.
	// For POST requests:
	// Content-Type: application/json, the original response body will be added as a string field "origin_rsp_body"
	// in the original JSON request body.
	// Content-Type: application/x-www-form-urlencoded, the original response body will be added as a URL-encoded
	// field "origin_rsp_body" in the original query request body.
	// For other requests (GET, etc.), the original request will be appended to the query as a URL-encoded
	// field "origin_rsp_body".
	PassThroughResponse bool `yaml:"pass_through_response"`
	// Timeout in milliseconds; default is 500 milliseconds.
	Timeout int `yaml:"timeout"`
}

Options plugin configuration

type Plugin

type Plugin struct{}

Plugin plugin definition

func (*Plugin) CheckConfig

func (p *Plugin) CheckConfig(_ string, decoder plugin.Decoder) error

CheckConfig validates the plugin configuration and returns a parsed configuration object with the correct type. Used in the ServerFilter method for parsing.

func (*Plugin) Setup

func (p *Plugin) Setup(string, plugin.Decoder) error

Setup plugin initialization

func (*Plugin) Type

func (p *Plugin) Type() string

Type get plugin type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL