context

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package context 提供每次 RAL 请求的上下文对象,主要用来输出日志。

Index

Constants

View Source
const (
	ErrnoHTTPEmptyBody               = "700"
	ErrnoHTTPAwaitingHeadersExceeded = "701"
	ErrnoHTTPIOTimeout               = "702"
	ErrnoUnKnown                     = "999"
)

Variables

View Source
var Err2ErrorHandlers = []Err2ErrorHandler{

	func(protocol string, errMsg string) (errno string, dealSucc bool) {
		if protocol != "http" {
			return
		}

		if strings.HasSuffix(errMsg, "with Body length 0") {
			return ErrnoHTTPEmptyBody, true
		}

		return
	},

	func(protocol string, errMsg string) (errno string, dealSucc bool) {
		if protocol != "http" {
			return
		}

		if strings.HasSuffix(errMsg, "net/http: request canceled (Client.Timeout exceeded while awaiting headers)") {
			return ErrnoHTTPAwaitingHeadersExceeded, true
		}

		return
	},

	func(protocol string, errMsg string) (errno string, dealSucc bool) {
		if protocol != "http" {
			return
		}

		if strings.HasSuffix(errMsg, "i/o timeout") {
			return ErrnoHTTPEmptyBody, true
		}

		return
	},
}

Err2ErrorHandlers 错误转换处理者

Functions

This section is empty.

Types

type Context

type Context struct {
	ReqContext RequestContext

	Caller      string
	ServiceName string
	ReqLen      int64
	RspLen      int64
	Method      string
	TraceID     interface{}
	Protocol    string
	BalanceName string

	PackStatis *StatisItem

	MaxTry int
	// contains filtered or unexported fields
}

Context 用作日志记录

func NewContext

func NewContext() (ctx *Context)

NewContext 创建一个context

func (*Context) CurRecord

func (ctx *Context) CurRecord() *InvokeRecord

CurRecord 当前的访问记录

func (*Context) NextRecord

func (ctx *Context) NextRecord()

NextRecord 将访问记录往后移一位

func (*Context) TimeStatisStart

func (ctx *Context) TimeStatisStart(topic string)

TimeStatisStart 开始一个统计项

func (*Context) TimeStatisStop

func (ctx *Context) TimeStatisStop(topic string)

TimeStatisStop 停止一个统计项

type Err2ErrorHandler

type Err2ErrorHandler func(protocol string, errMsg string) (errno string, dealSucc bool)

Err2ErrorHandler 错误转换为错误码 protocol 请求协议 当前有 http, nshead, pbrpc, mysql, redis

type InvokeRecord

type InvokeRecord struct {
	// RspCode 请求的响应码
	// http 代表 http status code,200 为正常,700+是自定义的错误码,表示发送请求时发生了error
	// nshead 等有自己的规则,不统一描述
	RspCode int

	// Path 请求的路径
	// http 相对path, 形如: /foo/bar
	Path string

	// IPPort ip和端口号
	IPPort string

	// Host 域名,可能和IPPort 一致
	Host string

	// 一次请求最多一条错误日志
	Error error
	// contains filtered or unexported fields
}

InvokeRecord 访问日志,因为重试可能有多条

func (*InvokeRecord) GetTimePoint

func (invokeRecord *InvokeRecord) GetTimePoint(topic string) string

GetTimePoint 得到一个时间点 毫秒

func (*InvokeRecord) GetTimeStatis

func (invokeRecord *InvokeRecord) GetTimeStatis(topic string) string

GetTimeStatis 获取一个统计项

func (*InvokeRecord) RecordTimePoint

func (invokeRecord *InvokeRecord) RecordTimePoint(topic string)

RecordTimePoint 打下一个时间点

type RequestContext

type RequestContext interface {
	GetTraceId() string
	GetClientIP() string
}

RequestContext Web请求的上下文

type StatisItem

type StatisItem struct {
	StartPoint time.Time
	StopPoint  time.Time
}

StatisItem 时间统计项

func (*StatisItem) GetSpan

func (si *StatisItem) GetSpan() string

GetSpan 得到耗时

Jump to

Keyboard shortcuts

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