github.com/GuanceCloud/cliutils@v1.1.21/point/option.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package point 7 8 import ( 9 sync "sync" 10 "time" 11 ) 12 13 const ( 14 // Limit tag/field key/value length. 15 defaultMaxFieldValLen int = 32 * 1024 * 1024 // if field value is string,limit to 32M 16 defaultKeyLen = 256 17 ) 18 19 type Option func(*cfg) 20 21 var cfgPool sync.Pool 22 23 func GetCfg(opts ...Option) *cfg { 24 v := cfgPool.Get() 25 if v == nil { 26 v = newCfg() 27 } 28 29 x := v.(*cfg) 30 for _, opt := range opts { 31 if opt != nil { 32 opt(x) 33 } 34 } 35 36 return x 37 } 38 39 func PutCfg(c *cfg) { 40 c.reset() 41 cfgPool.Put(c) 42 } 43 44 type cfg struct { 45 t time.Time 46 47 timestamp int64 // same as t 48 49 // Inject extra tags to the point 50 extraTags KVs 51 52 precision Precision 53 54 // During build the point, basic checking apllied. The checking will 55 // modify point tags/fields if necessary. After checking the point, 56 // some warnings will append to point. 57 precheck bool 58 59 keySorted bool 60 61 enableDotInKey, // enable dot(.) in tag/field key name 62 enableStrField, // enable string field value 63 // For uint64 field, if value < maxint64ļ¼convert to int64, or dropped if value > maxint64 64 enableU64Field bool 65 66 enc Encoding 67 68 callback Callback 69 70 // Limitations on point 71 maxTags, 72 maxFields, 73 maxTagKeyLen, 74 maxFieldKeyLen, 75 maxTagValLen, 76 maxFieldValLen, 77 maxMeasurementLen, 78 maxTagKeyValComposeLen int 79 80 disabledKeys, 81 requiredKeys []*Key 82 } 83 84 func newCfg() *cfg { 85 return &cfg{ 86 maxMeasurementLen: 1024, 87 maxTags: 256, 88 maxFields: 1024, 89 90 precision: PrecNS, 91 maxTagKeyLen: defaultKeyLen, 92 maxFieldKeyLen: defaultKeyLen, 93 precheck: true, 94 enableStrField: true, 95 enableU64Field: true, 96 enableDotInKey: true, 97 98 // Merged all tag's key/value, should not exceed 64k. 99 // Merge like this: tag1=1tag2=2tag2=3 100 maxTagKeyValComposeLen: 64 * 1024, 101 102 maxTagValLen: 1024, 103 maxFieldValLen: defaultMaxFieldValLen, 104 extraTags: nil, 105 } 106 } 107 108 func (c *cfg) reset() { 109 c.callback = nil 110 c.disabledKeys = nil 111 c.enableDotInKey = true 112 c.enableStrField = true 113 c.enableU64Field = true 114 c.enc = DefaultEncoding 115 c.extraTags = nil 116 c.maxFieldKeyLen = defaultKeyLen 117 c.maxFieldValLen = defaultMaxFieldValLen 118 c.maxFields = 1024 119 c.maxMeasurementLen = 1024 120 c.maxTagKeyLen = defaultKeyLen 121 c.maxTagKeyValComposeLen = 64 * 1024 122 c.maxTagValLen = 1024 123 c.maxTags = 256 124 c.precheck = true 125 c.precision = PrecNS 126 c.requiredKeys = nil 127 c.t = time.Time{} 128 c.timestamp = -1 129 } 130 131 func WithMaxKVComposeLen(n int) Option { return func(c *cfg) { c.maxTagKeyValComposeLen = n } } 132 func WithMaxMeasurementLen(n int) Option { return func(c *cfg) { c.maxMeasurementLen = n } } 133 func WithCallback(fn Callback) Option { return func(c *cfg) { c.callback = fn } } 134 func WithU64Field(on bool) Option { return func(c *cfg) { c.enableU64Field = on } } 135 func WithStrField(on bool) Option { return func(c *cfg) { c.enableStrField = on } } 136 func WithDotInKey(on bool) Option { return func(c *cfg) { c.enableDotInKey = on } } 137 func WithPrecheck(on bool) Option { return func(c *cfg) { c.precheck = on } } 138 func WithKeySorted(on bool) Option { return func(c *cfg) { c.keySorted = on } } 139 140 func WithTime(t time.Time) Option { 141 return func(c *cfg) { 142 c.t = t 143 c.timestamp = t.UnixNano() 144 } 145 } 146 147 func WithTimestamp(ts int64) Option { return func(c *cfg) { c.timestamp = ts } } 148 149 func WithEncoding(enc Encoding) Option { return func(c *cfg) { c.enc = enc } } 150 151 func WithExtraTags(tags map[string]string) Option { 152 return func(c *cfg) { 153 c.extraTags = NewTags(tags) 154 } 155 } 156 157 func WithMaxFieldKeyLen(n int) Option { return func(c *cfg) { c.maxFieldKeyLen = n } } 158 func WithMaxFieldValLen(n int) Option { return func(c *cfg) { c.maxFieldValLen = n } } 159 func WithMaxTagKeyLen(n int) Option { return func(c *cfg) { c.maxTagKeyLen = n } } 160 func WithMaxTagValLen(n int) Option { return func(c *cfg) { c.maxTagValLen = n } } 161 func WithMaxTags(n int) Option { return func(c *cfg) { c.maxTags = n } } 162 func WithMaxFields(n int) Option { return func(c *cfg) { c.maxFields = n } } 163 func WithPrecision(p Precision) Option { return func(c *cfg) { c.precision = p } } 164 165 func WithDisabledKeys(keys ...*Key) Option { 166 return func(c *cfg) { 167 c.disabledKeys = append(c.disabledKeys, keys...) 168 } 169 } 170 171 func WithRequiredKeys(keys ...*Key) Option { 172 return func(c *cfg) { 173 c.requiredKeys = append(c.requiredKeys, keys...) 174 } 175 } 176 177 // DefaultObjectOptions defined options on Object/CustomObject point. 178 func DefaultObjectOptions() []Option { 179 return []Option{ 180 WithDisabledKeys(disabledKeys[Object]...), 181 WithMaxFieldValLen(defaultMaxFieldValLen), 182 WithDotInKey(false), 183 WithRequiredKeys(requiredKeys[Object]...), 184 } 185 } 186 187 // DefaultLoggingOptions defined options on Logging point. 188 func DefaultLoggingOptions() []Option { 189 return append(CommonLoggingOptions(), []Option{ 190 WithDisabledKeys(disabledKeys[Logging]...), 191 WithMaxFieldValLen(defaultMaxFieldValLen), 192 WithRequiredKeys(requiredKeys[Logging]...), 193 }...) 194 } 195 196 // DefaultMetricOptions defined options on Metric point. 197 func DefaultMetricOptions() []Option { 198 return []Option{ 199 WithStrField(false), 200 WithDotInKey(true), 201 } 202 } 203 204 // DefaultMetricOptionsForInflux1X get influxdb 1.x options. 205 // For influxdb 1.x, uint64 not support. 206 func DefaultMetricOptionsForInflux1X() []Option { 207 return []Option{ 208 WithU64Field(false), 209 } 210 } 211 212 // CommonLoggingOptions defined options on RUM/Tracing/Security/Event/Profile/Network point. 213 func CommonLoggingOptions() []Option { 214 return []Option{ 215 WithDotInKey(false), 216 } 217 }