github.com/grafana/pyroscope@v1.18.0/pkg/distributor/writepath/write_path.go (about)

     1  package writepath
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  	"time"
     8  )
     9  
    10  // WritePath controls the write path.
    11  type WritePath string
    12  
    13  const (
    14  	// IngesterPath specifies old write path the requests are sent to ingester.
    15  	IngesterPath WritePath = "ingester"
    16  	// SegmentWriterPath specifies the new write path: distributor sends
    17  	// the request to segment writers before profile split, using the new
    18  	// distribution algorithm and the segment-writer ring.
    19  	SegmentWriterPath = "segment-writer"
    20  	// CombinedPath specifies that each request should be sent to both write
    21  	// paths. For each request we decide on how a failure is handled:
    22  	//  * If the request is sent to ingester (regardless of anything),
    23  	//    the response is returned to the client immediately after the old
    24  	//    write path returns. Failure of the new write path should be logged
    25  	//    and counted in metrics but NOT returned to the client.
    26  	//  * If the request is sent to segment-writer exclusively: the response
    27  	//    returns to the client only when the new write path returns.
    28  	//    Failure of the new write is returned to the client.
    29  	//    Failure of the old write path is NOT returned to the client.
    30  	CombinedPath = "combined"
    31  )
    32  
    33  var ErrInvalidWritePath = errors.New("invalid write path")
    34  
    35  var paths = []WritePath{
    36  	IngesterPath,
    37  	SegmentWriterPath,
    38  	CombinedPath,
    39  }
    40  
    41  const validWritePathOptionsString = "valid options: ingester, segment-writer, combined"
    42  
    43  func (m *WritePath) Set(text string) error {
    44  	x := WritePath(text)
    45  	for _, name := range paths {
    46  		if x == name {
    47  			*m = x
    48  			return nil
    49  		}
    50  	}
    51  	return fmt.Errorf("%w: %s; %s", ErrInvalidWritePath, x, validWritePathOptionsString)
    52  }
    53  
    54  func (m *WritePath) String() string { return string(*m) }
    55  
    56  type Compression string
    57  
    58  const (
    59  	CompressionNone Compression = "none"
    60  	CompressionGzip Compression = "gzip"
    61  )
    62  
    63  var ErrInvalidCompression = errors.New("invalid write path compression")
    64  
    65  var compressions = []Compression{
    66  	CompressionNone,
    67  	CompressionGzip,
    68  }
    69  
    70  const validCompressionOptionsString = "valid compression options: none, gzip"
    71  
    72  func (m *Compression) Set(text string) error {
    73  	x := Compression(text)
    74  	for _, name := range compressions {
    75  		if x == name {
    76  			*m = x
    77  			return nil
    78  		}
    79  	}
    80  	return fmt.Errorf("%w: %s; %s", ErrInvalidCompression, x, validCompressionOptionsString)
    81  }
    82  
    83  func (m *Compression) String() string { return string(*m) }
    84  
    85  type Config struct {
    86  	WritePath            WritePath     `yaml:"write_path" json:"write_path" doc:"hidden"`
    87  	IngesterWeight       float64       `yaml:"write_path_ingester_weight" json:"write_path_ingester_weight" doc:"hidden"`
    88  	SegmentWriterWeight  float64       `yaml:"write_path_segment_writer_weight" json:"write_path_segment_writer_weight" doc:"hidden"`
    89  	SegmentWriterTimeout time.Duration `yaml:"write_path_segment_writer_timeout" json:"write_path_segment_writer_timeout" doc:"hidden"`
    90  	Compression          Compression   `yaml:"write_path_compression" json:"write_path_compression" doc:"hidden"`
    91  	AsyncIngest          bool          `yaml:"async_ingest" json:"async_ingest" doc:"hidden"`
    92  }
    93  
    94  func (o *Config) RegisterFlags(f *flag.FlagSet) {
    95  	o.WritePath = IngesterPath
    96  	o.Compression = CompressionNone
    97  	f.Var(&o.WritePath, "write-path", "Controls the write path route; "+validWritePathOptionsString+".")
    98  	f.Float64Var(&o.IngesterWeight, "write-path.ingester-weight", 1,
    99  		"Specifies the fraction [0:1] that should be send to ingester in combined mode. 0 means no traffics is sent to ingester. 1 means 100% of requests are sent to ingester.")
   100  	f.Float64Var(&o.SegmentWriterWeight, "write-path.segment-writer-weight", 0,
   101  		"Specifies the fraction [0:1] that should be send to segment-writer in combined mode. 0 means no traffics is sent to segment-writer. 1 means 100% of requests are sent to segment-writer.")
   102  	f.DurationVar(&o.SegmentWriterTimeout, "write-path.segment-writer-timeout", 5*time.Second, "Timeout for segment writer requests.")
   103  	f.Var(&o.Compression, "write-path.compression", "Compression algorithm to use for segment writer requests; "+validCompressionOptionsString+".")
   104  	f.BoolVar(&o.AsyncIngest, "async-ingest", false, "If true, the write path will not wait for the segment-writer to finish processing the request. Writes to ingester always synchronous.")
   105  }