github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/aggregator/client/client.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package client
    22  
    23  import (
    24  	"errors"
    25  	"fmt"
    26  
    27  	"github.com/m3db/m3/src/metrics/metadata"
    28  	"github.com/m3db/m3/src/metrics/metric/aggregated"
    29  	"github.com/m3db/m3/src/metrics/metric/unaggregated"
    30  	"github.com/m3db/m3/src/metrics/policy"
    31  )
    32  
    33  var (
    34  	errClientIsInitializedOrClosed   = errors.New("client is already initialized or closed")
    35  	errClientIsUninitializedOrClosed = errors.New("client is uninitialized or closed")
    36  )
    37  
    38  // Client is a client capable of writing different types of metrics to the aggregation clients.
    39  type Client interface {
    40  	// Init initializes the client.
    41  	Init() error
    42  
    43  	// WriteUntimedCounter writes untimed counter metrics.
    44  	WriteUntimedCounter(
    45  		counter unaggregated.Counter,
    46  		metadatas metadata.StagedMetadatas,
    47  	) error
    48  
    49  	// WriteUntimedBatchTimer writes untimed batch timer metrics.
    50  	WriteUntimedBatchTimer(
    51  		batchTimer unaggregated.BatchTimer,
    52  		metadatas metadata.StagedMetadatas,
    53  	) error
    54  
    55  	// WriteUntimedGauge writes untimed gauge metrics.
    56  	WriteUntimedGauge(
    57  		gauge unaggregated.Gauge,
    58  		metadatas metadata.StagedMetadatas,
    59  	) error
    60  
    61  	// WriteTimed writes timed metrics.
    62  	WriteTimed(
    63  		metric aggregated.Metric,
    64  		metadata metadata.TimedMetadata,
    65  	) error
    66  
    67  	// WritePassthrough writes passthrough metrics.
    68  	WritePassthrough(
    69  		metric aggregated.Metric,
    70  		storagePolicy policy.StoragePolicy,
    71  	) error
    72  
    73  	// WriteTimedWithStagedMetadatas writes timed metrics with staged metadatas.
    74  	WriteTimedWithStagedMetadatas(
    75  		metric aggregated.Metric,
    76  		metadatas metadata.StagedMetadatas,
    77  	) error
    78  
    79  	// Flush flushes any remaining data buffered by the client.
    80  	Flush() error
    81  
    82  	// Close closes the client.
    83  	Close() error
    84  }
    85  
    86  // AdminClient is an administrative client capable of performing regular client operations
    87  // as well as high-privilege operations such as internal communcations among aggregation
    88  // servers that regular client is not permissioned to do.
    89  type AdminClient interface {
    90  	Client
    91  
    92  	// WriteForwarded writes forwarded metrics.
    93  	WriteForwarded(
    94  		metric aggregated.ForwardedMetric,
    95  		metadata metadata.ForwardMetadata,
    96  	) error
    97  }
    98  
    99  // NewClient creates a new client.
   100  func NewClient(opts Options) (Client, error) {
   101  	if err := opts.Validate(); err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	clientType := opts.AggregatorClientType()
   106  	switch clientType {
   107  	case M3MsgAggregatorClient:
   108  		return NewM3MsgClient(opts)
   109  	case LegacyAggregatorClient:
   110  		fallthrough // LegacyAggregatorClient is an alias
   111  	case TCPAggregatorClient:
   112  		return NewTCPClient(opts)
   113  	}
   114  	return nil, fmt.Errorf("unrecognized client type: %v", clientType)
   115  }