github.com/matrixorigin/matrixone@v1.2.0/pkg/proxy/counter_set.go (about)

     1  // Copyright 2021 - 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/util/metric/stats"
    19  	"go.uber.org/zap"
    20  )
    21  
    22  // counterLogExporter is the log exporter of proxy counter set.
    23  type counterLogExporter struct {
    24  	counter *counterSet
    25  }
    26  
    27  var _ stats.LogExporter = (*counterLogExporter)(nil)
    28  
    29  // newCounterLogExporter creates a new log exporter.
    30  func newCounterLogExporter(counter *counterSet) stats.LogExporter {
    31  	return &counterLogExporter{
    32  		counter: counter,
    33  	}
    34  }
    35  
    36  // Export implements the stats.LogExporter interface.
    37  func (e *counterLogExporter) Export() []zap.Field {
    38  	var fields []zap.Field
    39  	fields = append(fields, zap.Int64("accepted connections",
    40  		e.counter.connAccepted.Load()))
    41  	fields = append(fields, zap.Int64("total connections",
    42  		e.counter.connTotal.Load()))
    43  	fields = append(fields, zap.Int64("client disconnect",
    44  		e.counter.clientDisconnect.Load()))
    45  	fields = append(fields, zap.Int64("server disconnect",
    46  		e.counter.serverDisconnect.Load()))
    47  	fields = append(fields, zap.Int64("refused connections",
    48  		e.counter.connRefused.Load()))
    49  	fields = append(fields, zap.Int64("auth failed",
    50  		e.counter.authFailed.Load()))
    51  	fields = append(fields, zap.Int64("connection migration success",
    52  		e.counter.connMigrationSuccess.Load()))
    53  	fields = append(fields, zap.Int64("connection migration requested",
    54  		e.counter.connMigrationRequested.Load()))
    55  	fields = append(fields, zap.Int64("connection migration cannot start",
    56  		e.counter.connMigrationCannotStart.Load()))
    57  	return fields
    58  }
    59  
    60  // counterSet contains all items that need to be tracked in proxy.
    61  type counterSet struct {
    62  	connAccepted             stats.Counter
    63  	connTotal                stats.Counter
    64  	clientDisconnect         stats.Counter
    65  	serverDisconnect         stats.Counter
    66  	connRefused              stats.Counter
    67  	authFailed               stats.Counter
    68  	connMigrationSuccess     stats.Counter
    69  	connMigrationRequested   stats.Counter
    70  	connMigrationCannotStart stats.Counter
    71  }
    72  
    73  // newCounterSet creates a new counterSet.
    74  func newCounterSet() *counterSet {
    75  	return &counterSet{}
    76  }
    77  
    78  // updateWithError updates the counterSet according to the error.
    79  func (s *counterSet) updateWithErr(err error) {
    80  	if err == nil {
    81  		return
    82  	}
    83  	switch getErrorCode(err) {
    84  	case codeAuthFailed:
    85  		s.authFailed.Add(1)
    86  	case codeClientDisconnect:
    87  		s.clientDisconnect.Add(1)
    88  	case codeServerDisconnect:
    89  		s.serverDisconnect.Add(1)
    90  	}
    91  }