github.com/cloudwego/kitex@v0.9.0/pkg/diagnosis/interface.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package diagnosis provide support to register probe func that can get some infos to do diagnosis.
    18  package diagnosis
    19  
    20  // ProbeName is the name of probe.
    21  type ProbeName string
    22  
    23  // ProbeFunc is used to get probe data, it is usually a data dump func.
    24  type ProbeFunc func() interface{}
    25  
    26  // Service is the interface for debug service.
    27  type Service interface {
    28  	// RegisterProbeFunc is used to register ProbeFunc with probe name
    29  	// ProbeFunc is usually a dump func that to dump info to do problem diagnosis,
    30  	// eg: CBSuite.Dump(), s.RegisterProbeFunc(CircuitInfoKey, cbs.Dump)
    31  	RegisterProbeFunc(ProbeName, ProbeFunc)
    32  }
    33  
    34  // RegisterProbeFunc is a wrap function to execute Service.RegisterProbeFunc.
    35  func RegisterProbeFunc(svc Service, name ProbeName, pf ProbeFunc) {
    36  	if svc != nil {
    37  		svc.RegisterProbeFunc(name, pf)
    38  	}
    39  }
    40  
    41  // Keys below are probe info that has been registered by default.
    42  // If you want to register other info, please use RegisterProbeFunc(ProbeName, ProbeFunc) to do that.
    43  const (
    44  	// Common
    45  	ChangeEventsKey    ProbeName = "events"
    46  	ServiceInfosKey    ProbeName = "service_infos"
    47  	FallbackServiceKey ProbeName = "fallback_service"
    48  	OptionsKey         ProbeName = "options"
    49  
    50  	// Client
    51  	DestServiceKey ProbeName = "dest_service"
    52  	ConnPoolKey    ProbeName = "conn_pool"
    53  	RetryPolicyKey ProbeName = "retry_policy"
    54  )
    55  
    56  // WrapAsProbeFunc is to wrap probe data as ProbeFunc, the data is some infos that you want to diagnosis, like config info.
    57  func WrapAsProbeFunc(data interface{}) ProbeFunc {
    58  	return func() interface{} {
    59  		return data
    60  	}
    61  }
    62  
    63  // NoopService is an empty implementation of Service.
    64  // If you need diagnosis feature, specify Service through the option WithDiagnosisService of client and server.
    65  var NoopService Service = &noopService{}
    66  
    67  type noopService struct{}
    68  
    69  func (n noopService) RegisterProbeFunc(name ProbeName, probeFunc ProbeFunc) {
    70  	// RegisterProbeFunc of NoopService do nothing.
    71  }