options.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package influxdb2
  5. import (
  6. "crypto/tls"
  7. nethttp "net/http"
  8. "time"
  9. "github.com/influxdata/influxdb-client-go/v2/api/http"
  10. "github.com/influxdata/influxdb-client-go/v2/api/write"
  11. )
  12. // Options holds configuration properties for communicating with InfluxDB server
  13. type Options struct {
  14. // LogLevel to filter log messages. Each level mean to log all categories bellow. 0 error, 1 - warning, 2 - info, 3 - debug
  15. logLevel uint
  16. // Writing options
  17. writeOptions *write.Options
  18. // Http options
  19. httpOptions *http.Options
  20. }
  21. // BatchSize returns size of batch
  22. func (o *Options) BatchSize() uint {
  23. return o.WriteOptions().BatchSize()
  24. }
  25. // SetBatchSize sets number of points sent in single request
  26. func (o *Options) SetBatchSize(batchSize uint) *Options {
  27. o.WriteOptions().SetBatchSize(batchSize)
  28. return o
  29. }
  30. // FlushInterval returns flush interval in ms
  31. func (o *Options) FlushInterval() uint {
  32. return o.WriteOptions().FlushInterval()
  33. }
  34. // SetFlushInterval sets flush interval in ms in which is buffer flushed if it has not been already written
  35. func (o *Options) SetFlushInterval(flushIntervalMs uint) *Options {
  36. o.WriteOptions().SetFlushInterval(flushIntervalMs)
  37. return o
  38. }
  39. // RetryInterval returns the retry interval in ms
  40. func (o *Options) RetryInterval() uint {
  41. return o.WriteOptions().RetryInterval()
  42. }
  43. // SetRetryInterval sets retry interval in ms, which is set if not sent by server
  44. func (o *Options) SetRetryInterval(retryIntervalMs uint) *Options {
  45. o.WriteOptions().SetRetryInterval(retryIntervalMs)
  46. return o
  47. }
  48. // MaxRetries returns maximum count of retry attempts of failed writes, default 5.
  49. func (o *Options) MaxRetries() uint {
  50. return o.WriteOptions().MaxRetries()
  51. }
  52. // SetMaxRetries sets maximum count of retry attempts of failed writes.
  53. // Setting zero value disables retry strategy.
  54. func (o *Options) SetMaxRetries(maxRetries uint) *Options {
  55. o.WriteOptions().SetMaxRetries(maxRetries)
  56. return o
  57. }
  58. // RetryBufferLimit returns retry buffer limit
  59. func (o *Options) RetryBufferLimit() uint {
  60. return o.WriteOptions().RetryBufferLimit()
  61. }
  62. // SetRetryBufferLimit sets maximum number of points to keep for retry. Should be multiple of BatchSize.
  63. func (o *Options) SetRetryBufferLimit(retryBufferLimit uint) *Options {
  64. o.WriteOptions().SetRetryBufferLimit(retryBufferLimit)
  65. return o
  66. }
  67. // MaxRetryInterval returns the maximum delay between each retry attempt in milliseconds, default 125,000.
  68. func (o *Options) MaxRetryInterval() uint {
  69. return o.WriteOptions().MaxRetryInterval()
  70. }
  71. // SetMaxRetryInterval sets the maximum delay between each retry attempt in millisecond.
  72. func (o *Options) SetMaxRetryInterval(maxRetryIntervalMs uint) *Options {
  73. o.WriteOptions().SetMaxRetryInterval(maxRetryIntervalMs)
  74. return o
  75. }
  76. // MaxRetryTime returns the maximum total retry timeout in millisecond, default 180,000.
  77. func (o *Options) MaxRetryTime() uint {
  78. return o.WriteOptions().MaxRetryTime()
  79. }
  80. // SetMaxRetryTime sets the maximum total retry timeout in millisecond.
  81. func (o *Options) SetMaxRetryTime(maxRetryTimeMs uint) *Options {
  82. o.WriteOptions().SetMaxRetryTime(maxRetryTimeMs)
  83. return o
  84. }
  85. // ExponentialBase returns the base for the exponential retry delay. Default 2.
  86. func (o *Options) ExponentialBase() uint {
  87. return o.WriteOptions().ExponentialBase()
  88. }
  89. // SetExponentialBase sets the base for the exponential retry delay.
  90. func (o *Options) SetExponentialBase(exponentialBase uint) *Options {
  91. o.WriteOptions().SetExponentialBase(exponentialBase)
  92. return o
  93. }
  94. // LogLevel returns log level
  95. func (o *Options) LogLevel() uint {
  96. return o.logLevel
  97. }
  98. // SetLogLevel set level to filter log messages. Each level mean to log all categories bellow. Default is ErrorLevel.
  99. // There are four level constant int the log package in this library:
  100. // - ErrorLevel
  101. // - WarningLevel
  102. // - InfoLevel
  103. // - DebugLevel
  104. // The DebugLevel will print also content of writen batches, queries.
  105. // The InfoLevel prints HTTP requests info, among others.
  106. // Set log.Log to nil in order to completely disable logging.
  107. func (o *Options) SetLogLevel(logLevel uint) *Options {
  108. o.logLevel = logLevel
  109. return o
  110. }
  111. // Precision returns time precision for writes
  112. func (o *Options) Precision() time.Duration {
  113. return o.WriteOptions().Precision()
  114. }
  115. // SetPrecision sets time precision to use in writes for timestamp. In unit of duration: time.Nanosecond, time.Microsecond, time.Millisecond, time.Second
  116. func (o *Options) SetPrecision(precision time.Duration) *Options {
  117. o.WriteOptions().SetPrecision(precision)
  118. return o
  119. }
  120. // UseGZip returns true if write request are gzip`ed
  121. func (o *Options) UseGZip() bool {
  122. return o.WriteOptions().UseGZip()
  123. }
  124. // SetUseGZip specifies whether to use GZip compression in write requests.
  125. func (o *Options) SetUseGZip(useGZip bool) *Options {
  126. o.WriteOptions().SetUseGZip(useGZip)
  127. return o
  128. }
  129. // HTTPClient returns the http.Client that is configured to be used
  130. // for HTTP requests. It will return the one that has been set using
  131. // SetHTTPClient or it will construct a default client using the
  132. // other configured options.
  133. func (o *Options) HTTPClient() *nethttp.Client {
  134. return o.httpOptions.HTTPClient()
  135. }
  136. // SetHTTPClient will configure the http.Client that is used
  137. // for HTTP requests. If set to nil, an HTTPClient will be
  138. // generated.
  139. //
  140. // Setting the HTTPClient will cause the other HTTP options
  141. // to be ignored.
  142. // In case of UsersAPI.SignIn() is used, HTTPClient.Jar will be used for storing session cookie.
  143. func (o *Options) SetHTTPClient(c *nethttp.Client) *Options {
  144. o.httpOptions.SetHTTPClient(c)
  145. return o
  146. }
  147. // TLSConfig returns TLS config
  148. func (o *Options) TLSConfig() *tls.Config {
  149. return o.HTTPOptions().TLSConfig()
  150. }
  151. // SetTLSConfig sets TLS configuration for secure connection
  152. func (o *Options) SetTLSConfig(tlsConfig *tls.Config) *Options {
  153. o.HTTPOptions().SetTLSConfig(tlsConfig)
  154. return o
  155. }
  156. // HTTPRequestTimeout returns HTTP request timeout
  157. func (o *Options) HTTPRequestTimeout() uint {
  158. return o.HTTPOptions().HTTPRequestTimeout()
  159. }
  160. // SetHTTPRequestTimeout sets HTTP request timeout in sec
  161. func (o *Options) SetHTTPRequestTimeout(httpRequestTimeout uint) *Options {
  162. o.HTTPOptions().SetHTTPRequestTimeout(httpRequestTimeout)
  163. return o
  164. }
  165. // WriteOptions returns write related options
  166. func (o *Options) WriteOptions() *write.Options {
  167. if o.writeOptions == nil {
  168. o.writeOptions = write.DefaultOptions()
  169. }
  170. return o.writeOptions
  171. }
  172. // HTTPOptions returns HTTP related options
  173. func (o *Options) HTTPOptions() *http.Options {
  174. if o.httpOptions == nil {
  175. o.httpOptions = http.DefaultOptions()
  176. }
  177. return o.httpOptions
  178. }
  179. // AddDefaultTag adds a default tag. DefaultTags are added to each written point.
  180. // If a tag with the same key already exist it is overwritten.
  181. // If a point already defines such a tag, it is left unchanged
  182. func (o *Options) AddDefaultTag(key, value string) *Options {
  183. o.WriteOptions().AddDefaultTag(key, value)
  184. return o
  185. }
  186. // ApplicationName returns application name used in the User-Agent HTTP header
  187. func (o *Options) ApplicationName() string {
  188. return o.HTTPOptions().ApplicationName()
  189. }
  190. // SetApplicationName sets an application name to the User-Agent HTTP header
  191. func (o *Options) SetApplicationName(appName string) *Options {
  192. o.HTTPOptions().SetApplicationName(appName)
  193. return o
  194. }
  195. // DefaultOptions returns Options object with default values
  196. func DefaultOptions() *Options {
  197. return &Options{logLevel: 0, writeOptions: write.DefaultOptions(), httpOptions: http.DefaultOptions()}
  198. }