github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/models/types.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 models
    22  
    23  import (
    24  	"bytes"
    25  	"regexp"
    26  )
    27  
    28  // Separators for tags.
    29  const (
    30  	graphiteSep  = byte('.')
    31  	sep          = byte(',')
    32  	finish       = byte('!')
    33  	eq           = byte('=')
    34  	leftBracket  = byte('{')
    35  	rightBracket = byte('}')
    36  )
    37  
    38  // IDSchemeType determines the scheme for generating
    39  // series IDs based on their tags.
    40  type IDSchemeType uint16
    41  
    42  const (
    43  	// TypeDefault is an invalid scheme that indicates that the default scheme
    44  	// for the tag options version option should be used.
    45  	TypeDefault IDSchemeType = iota
    46  	// TypeQuoted describes a scheme where IDs are generated by appending
    47  	// tag names with explicitly quoted and escaped tag values. Tag names are
    48  	// also escaped if they contain invalid characters. This is equivalent to
    49  	// the Prometheus ID style.
    50  	// {t1:v1},{t2:v2} -> {t1="v1",t2="v2"}
    51  	// {t1:v1,t2:v2}   -> {t1="v1,t2:v2"}
    52  	// {"t1":"v1"}     -> {\"t1\""="\"v1\""}
    53  	TypeQuoted
    54  	// TypePrependMeta describes a scheme where IDs are generated by prepending
    55  	// the length of each tag at the start of the ID
    56  	// {t1:v1},{t2:v2} -> 2,2,2,2!t1v1t2v2
    57  	// {t1:v1,t2:v2}   -> 2,8!t1v1,t2:v2
    58  	// {"t1":"v1"}     -> 4,4!"t1""v1"
    59  	TypePrependMeta
    60  	// TypeGraphite describes a scheme where IDs are generated to match graphite
    61  	// representation of the tags. This scheme should only be used on the graphite
    62  	// ingestion path, as it ignores tag names and is very prone to collisions if
    63  	// used on non-graphite data.
    64  	// {__g0__:v1},{__g1__:v2} -> v1.v2
    65  	//
    66  	// NB: when TypeGraphite is specified, tags are ordered numerically rather
    67  	// than lexically.
    68  	//
    69  	// NB 2: while the graphite scheme is valid, it is not available to choose as
    70  	// a general ID scheme; instead, it is set on any metric coming through the
    71  	// graphite ingestion path.
    72  	TypeGraphite
    73  )
    74  
    75  // TagOptions describes additional options for tags.
    76  type TagOptions interface {
    77  	// Validate validates these tag options.
    78  	Validate() error
    79  
    80  	// SetMetricName sets the name for the `metric name` tag.
    81  	SetMetricName(value []byte) TagOptions
    82  
    83  	// MetricName gets the name for the `metric name` tag.
    84  	MetricName() []byte
    85  
    86  	// SetBucketName sets the name for the `bucket label` tag.
    87  	SetBucketName(value []byte) TagOptions
    88  
    89  	// BucketName gets the name for the `bucket label` tag.
    90  	BucketName() []byte
    91  
    92  	// SetIDSchemeType sets the ID generation scheme type.
    93  	SetIDSchemeType(value IDSchemeType) TagOptions
    94  
    95  	// IDSchemeType gets the ID generation scheme type.
    96  	IDSchemeType() IDSchemeType
    97  
    98  	// SetFilters sets tag filters.
    99  	SetFilters(value Filters) TagOptions
   100  
   101  	// Filters gets the tag filters.
   102  	Filters() Filters
   103  
   104  	// SetAllowTagNameDuplicates sets the value to allow duplicate tags to appear.
   105  	SetAllowTagNameDuplicates(value bool) TagOptions
   106  
   107  	// AllowTagNameDuplicates returns the value to allow duplicate tags to appear.
   108  	AllowTagNameDuplicates() bool
   109  
   110  	// SetAllowTagValueEmpty sets the value to allow empty tag values to appear.
   111  	SetAllowTagValueEmpty(value bool) TagOptions
   112  
   113  	// AllowTagValueEmpty returns the value to allow empty tag values to appear.
   114  	AllowTagValueEmpty() bool
   115  
   116  	// SetMaxTagLiteralLength sets the maximum length of a tag Name/Value.
   117  	SetMaxTagLiteralLength(value uint16) TagOptions
   118  
   119  	// MaxTagLiteralLength returns the maximum length of a tag Name/Value.
   120  	MaxTagLiteralLength() uint16
   121  
   122  	// Equals determines if two tag options are equivalent.
   123  	Equals(other TagOptions) bool
   124  }
   125  
   126  // Tags represents a set of tags with options.
   127  type Tags struct {
   128  	Opts     TagOptions
   129  	Tags     []Tag
   130  	hashedID uint64
   131  	id       []byte
   132  }
   133  
   134  // Tag is a key/value metric tag pair.
   135  type Tag struct {
   136  	Name  []byte
   137  	Value []byte
   138  }
   139  
   140  // Equal determiens whether two tags are equal to each other.
   141  func (t Tag) Equal(other Tag) bool {
   142  	return bytes.Equal(t.Name, other.Name) && bytes.Equal(t.Value, other.Value)
   143  }
   144  
   145  // MatchType is an enum for label matching types.
   146  type MatchType int
   147  
   148  // Possible MatchTypes.
   149  const (
   150  	MatchEqual MatchType = iota
   151  	MatchNotEqual
   152  	MatchRegexp
   153  	MatchNotRegexp
   154  	MatchField
   155  	MatchNotField
   156  	MatchAll
   157  )
   158  
   159  // Matcher models the matching of a label.
   160  // NB: when serialized to JSON, name and value will be in base64.
   161  type Matcher struct {
   162  	Type  MatchType `json:"type"`
   163  	Name  []byte    `json:"name"`
   164  	Value []byte    `json:"value"`
   165  
   166  	re *regexp.Regexp
   167  }
   168  
   169  // Matchers is a list of individual matchers.
   170  type Matchers []Matcher
   171  
   172  // Metric is the individual metric that gets returned from the search endpoint.
   173  type Metric struct {
   174  	ID   []byte
   175  	Tags Tags
   176  }
   177  
   178  // Metrics is a list of individual metrics.
   179  type Metrics []Metric
   180  
   181  // Filters is a set of tag filters.
   182  type Filters []Filter
   183  
   184  // Filter is a regex tag filter.
   185  type Filter struct {
   186  	// Name is the name of the series.
   187  	Name []byte
   188  	// Values are a set of filter values. If this is unset, all series containing
   189  	// the tag name are filtered.
   190  	Values [][]byte
   191  }