cls

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: Apache-2.0, MIT Imports: 10 Imported by: 0

README

English | 中文

tRPC-Go CLS Remote Logging Plugin

Go Reference Go Report Card LICENSE Releases Docs Tests Coverage

This plugin encapsulates the Tencent Cloud CLS SDK and provides a tRPC-Go logging plugin to quickly integrate your tRPC-Go service with the CLS logging system.

Complete Configuration

plugins:
  log: # Logging configuration, supports multiple logs, can log using log.Get("xxx").Debug
    default: # Default log configuration, multiple outputs are supported for each log
      - writer: cls # CLS remote log output
        level: debug # Log level for remote logging
        remote_config: # Remote log configuration
          topic_id: b0179d73-8932-4a96-a1df-xxxxxx # CLS log topic ID
          host: ap-guangzhou.cls.tencentyun.com # CLS log reporting domain
          secret_id: AKIDRefNpzzYcOf7HFsj8Kxxxxxxx # Tencent Cloud secret_id
          secret_key: jK4ZJIMEuV3IHy49zYq2yxxxxxxx # Tencent Cloud secret_key
          total_size_ln_bytes: 104857600 # [Optional, 100 * 1024 * 1024] Maximum log size that the instance can cache
          max_send_worker_count: 50 # [Optional, 50] Maximum number of "goroutines" for concurrency
          max_block_sec: 0 # [Optional, 0] Maximum blocking time on the send method, default is 0 (non-blocking)
          max_batch_size: 5242880 # [Optional, 5 * 1024 * 1024] When the log size cached in the Batch is greater than or equal to MaxBatchSize, the batch will be sent
          max_batch_count: 4096 # [Optional, 4096] When the number of logs cached in the Batch is greater than or equal to MaxBatchCount, the batch will be sent
          linger_ms: 2000 # [Optional, 2 * 1000] The time from batch creation to being able to send
          retries: 10 # [Optional, 10] Number of retries for a batch that failed to send for the first time
          max_reserved_attempts: 11 # [Optional, 11] Each attempt to send a batch corresponds to an attempt, and this parameter controls the number of attempts returned to the user
          base_retry_backoff_ms: 100 # [Optional, 100] Backoff time for the first retry
          max_retry_backoff_ms: 50000 # [Optional, 50 * 1000] Maximum backoff time for retries
          source: 127.0.0.1 # [Optional] Default to using trpc global local_ip, service listening IP
          field_map: # [Optional, not set by default] Custom field mapping for reporting
            Level: log_level # Map the Level field to log_level and report it
            field1: test_field # Map the field1 field to test_field and report it

Get started

1. Apply for a CLS Log Topic
2. Configure trpc_go.yaml according to the complete configuration mentioned above.
3. Develop Your Code
  • First, import this plugin:
import _ "trpc.group/trpc-go/trpc-log-cls"
  • Log your messages:
log.WithFields("key1", "value1").Info("message1")
log.Warn("warning message1")
  • If you need to remap the fields to be reported, you can do so using the field_map configuration:
field_map: # [Optional, not set by default] Custom field mapping for reporting
  Msg: log_content # Map the Msg field to log_content and report it
  Caller: file_line # Map the Caller field to file_line and report it
  Level: log_level # Map the Level field to log_level and report it
  Time: ts # Map the Time field to ts and report it
  key1: test_field # Map the key1 field to test_field and report it
  ...

If you need more complex field remapping or filtering, you can implement it by overriding cls.GetReportCLSField. For example, to standardize the fields reported to remote logging in lowercase with underscores:

import (
	...
    cls "trpc.group/trpc-go/trpc-log-cls"
	...
)

func init() {
	cls.GetReportCLSField = func(sourceField string, _ *cls.Config) (string, bool) {
		var output []rune
		for i, r := range sourceField {
			if i == 0 {
				output = append(output, unicode.ToLower(r))
				continue
			}

			if unicode.IsUpper(r) {
				output = append(output, '_')
			}
			output = append(output, unicode.ToLower(r))
		}

		return string(output), true
	}
}
4. View Remote Logs

Documentation

Overview

Package cls is tencent cloud cls log plugin for trpc-go

Index

Constants

This section is empty.

Variables

View Source
var GetReportCLSField = func(sourceField string, cfg *Config) (reportField string, needReport bool) {
	if cfg == nil || len(cfg.FieldMap) == 0 {
		return sourceField, true
	}

	reportField, exist := cfg.FieldMap[sourceField]
	if !exist {
		reportField = sourceField
	}

	return reportField, true
}

GetReportCLSField is used to override the logging fields reported to CLS. For example: log.WithFields("field1", "value1").Info("message1"). In this case, the field reported to CLS is "field1," and you can override it using the GetReportCLSField function. The default implementation supports field mapping through framework configuration, as detailed in the README.md. Parameter sourceField represents the original field, such as "field1" in the example above, cfg is the configuration information for CLS framework. The returned reportField is the field after remapping for reporting to CLS. If needReport is false, it means that this field will be ignored and not reported to CLS.

Functions

This section is empty.

Types

type Callback

type Callback struct {
}

Callback provides callback function.

func (*Callback) Fail

func (callback *Callback) Fail(result *clssdk.Result)

Fail is the failure callback function.

func (*Callback) Success

func (callback *Callback) Success(result *clssdk.Result)

Success is the success callback function.

type Config

type Config struct {
	// TopicID is the log reporting topic ID.
	TopicID string `yaml:"topic_id"`
	// Host is the log reporting host.
	Host string `yaml:"host"`
	// SecretID is the log reporting secret ID used when calling Tencent Cloud APIs.
	SecretID string `yaml:"secret_id"`
	// SecretKey is the log reporting secret key used when calling Tencent Cloud APIs.
	SecretKey string `yaml:"secret_key"`
	// TotalSizeLnBytes is the maximum log size limit that an instance can cache, default is 100MB.
	TotalSizeLnBytes int64 `yaml:"total_size_ln_bytes"`
	// MaxSendWorkerCount is the maximum number of goroutines that the client can use concurrently, default is 50.
	MaxSendWorkerCount int64 `yaml:"max_send_worker_count"`
	// MaxBlockSec is the maximum blocking time on the send method if available buffer space is insufficient, default is non-blocking.
	MaxBlockSec int `yaml:"max_block_sec"`
	// MaxBatchSize is the size at which a batch of cached logs will be sent when greater than or equal to MaxBatchSize, default is 512KB, with a maximum of 5MB.
	MaxBatchSize int64 `yaml:"max_batch_size"`
	// MaxBatchCount is the number of logs in a batch that triggers sending when greater than or equal to MaxBatchCount, default is 4096, with a maximum of 40960.
	MaxBatchCount int `yaml:"max_batch_count"`
	// LingerMs is the time a batch stays in an available state before being sent, default is 2 seconds, with a minimum of 100 milliseconds.
	LingerMs int64 `yaml:"linger_ms"`
	// Retries is the number of retries allowed if a batch fails to send on the first attempt, default is 10 times.
	Retries int `yaml:"retries"`
	// MaxReservedAttempts is the number of attempts retained for each batch, each send attempt corresponds to an attempt, this parameter controls the number of attempts returned to the user, default is to keep only the latest 11 attempt records.
	MaxReservedAttempts int `yaml:"max_reserved_attempts"`
	// BaseRetryBackoffMs is the initial backoff time for the first retry, default is 100 milliseconds. The client uses an exponential backoff algorithm, where the planned wait time for the Nth retry is baseRetryBackoffMs * 2^(N-1).
	BaseRetryBackoffMs int64 `yaml:"base_retry_backoff_ms"`
	// MaxRetryBackoffMs is the maximum backoff time for retries, default is 50 seconds.
	MaxRetryBackoffMs int64 `yaml:"max_retry_backoff_ms"`
	// FieldMap is the mapping of log reporting fields.
	FieldMap map[string]string `yaml:"field_map"`
	// Source is the log source, typically the machine's IP address.
	Source string `yaml:"source"`
}

Config is the configuration for the cls log plugin.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is cls logger.

func (*Logger) Write

func (l *Logger) Write(p []byte) (n int, err error)

Write trpc写日志接口实现

type LoggerPlugin

type LoggerPlugin struct {
}

LoggerPlugin is CLS logger plugin.

func (*LoggerPlugin) Setup

func (lp *LoggerPlugin) Setup(name string, configDec plugin.Decoder) error

Setup setups the plugin.

func (*LoggerPlugin) SetupCls

func (lp *LoggerPlugin) SetupCls(conf *log.OutputConfig) (*Logger, error)

SetupCls setups cls logger.

func (*LoggerPlugin) Type

func (lp *LoggerPlugin) Type() string

Type returns the type of logger plugin.

Jump to

Keyboard shortcuts

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