generated_metrics.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // Code generated by mdatagen. DO NOT EDIT.
  2. package metadata
  3. import (
  4. "time"
  5. "go.opentelemetry.io/collector/component"
  6. "go.opentelemetry.io/collector/pdata/pcommon"
  7. "go.opentelemetry.io/collector/pdata/pmetric"
  8. "go.opentelemetry.io/collector/receiver"
  9. )
  10. type metricSshcheckDuration struct {
  11. data pmetric.Metric // data buffer for generated metric.
  12. config MetricConfig // metric config provided by user.
  13. capacity int // max observed number of data points added to the metric.
  14. }
  15. // init fills sshcheck.duration metric with initial data.
  16. func (m *metricSshcheckDuration) init() {
  17. m.data.SetName("sshcheck.duration")
  18. m.data.SetDescription("Measures the duration of SSH connection.")
  19. m.data.SetUnit("ms")
  20. m.data.SetEmptyGauge()
  21. }
  22. func (m *metricSshcheckDuration) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64) {
  23. if !m.config.Enabled {
  24. return
  25. }
  26. dp := m.data.Gauge().DataPoints().AppendEmpty()
  27. dp.SetStartTimestamp(start)
  28. dp.SetTimestamp(ts)
  29. dp.SetIntValue(val)
  30. }
  31. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  32. func (m *metricSshcheckDuration) updateCapacity() {
  33. if m.data.Gauge().DataPoints().Len() > m.capacity {
  34. m.capacity = m.data.Gauge().DataPoints().Len()
  35. }
  36. }
  37. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  38. func (m *metricSshcheckDuration) emit(metrics pmetric.MetricSlice) {
  39. if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 {
  40. m.updateCapacity()
  41. m.data.MoveTo(metrics.AppendEmpty())
  42. m.init()
  43. }
  44. }
  45. func newMetricSshcheckDuration(cfg MetricConfig) metricSshcheckDuration {
  46. m := metricSshcheckDuration{config: cfg}
  47. if cfg.Enabled {
  48. m.data = pmetric.NewMetric()
  49. m.init()
  50. }
  51. return m
  52. }
  53. type metricSshcheckError struct {
  54. data pmetric.Metric // data buffer for generated metric.
  55. config MetricConfig // metric config provided by user.
  56. capacity int // max observed number of data points added to the metric.
  57. }
  58. // init fills sshcheck.error metric with initial data.
  59. func (m *metricSshcheckError) init() {
  60. m.data.SetName("sshcheck.error")
  61. m.data.SetDescription("Records errors occurring during SSH check.")
  62. m.data.SetUnit("{error}")
  63. m.data.SetEmptySum()
  64. m.data.Sum().SetIsMonotonic(false)
  65. m.data.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  66. m.data.Sum().DataPoints().EnsureCapacity(m.capacity)
  67. }
  68. func (m *metricSshcheckError) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, errorMessageAttributeValue string) {
  69. if !m.config.Enabled {
  70. return
  71. }
  72. dp := m.data.Sum().DataPoints().AppendEmpty()
  73. dp.SetStartTimestamp(start)
  74. dp.SetTimestamp(ts)
  75. dp.SetIntValue(val)
  76. dp.Attributes().PutStr("error.message", errorMessageAttributeValue)
  77. }
  78. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  79. func (m *metricSshcheckError) updateCapacity() {
  80. if m.data.Sum().DataPoints().Len() > m.capacity {
  81. m.capacity = m.data.Sum().DataPoints().Len()
  82. }
  83. }
  84. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  85. func (m *metricSshcheckError) emit(metrics pmetric.MetricSlice) {
  86. if m.config.Enabled && m.data.Sum().DataPoints().Len() > 0 {
  87. m.updateCapacity()
  88. m.data.MoveTo(metrics.AppendEmpty())
  89. m.init()
  90. }
  91. }
  92. func newMetricSshcheckError(cfg MetricConfig) metricSshcheckError {
  93. m := metricSshcheckError{config: cfg}
  94. if cfg.Enabled {
  95. m.data = pmetric.NewMetric()
  96. m.init()
  97. }
  98. return m
  99. }
  100. type metricSshcheckSftpDuration struct {
  101. data pmetric.Metric // data buffer for generated metric.
  102. config MetricConfig // metric config provided by user.
  103. capacity int // max observed number of data points added to the metric.
  104. }
  105. // init fills sshcheck.sftp_duration metric with initial data.
  106. func (m *metricSshcheckSftpDuration) init() {
  107. m.data.SetName("sshcheck.sftp_duration")
  108. m.data.SetDescription("Measures SFTP request duration.")
  109. m.data.SetUnit("ms")
  110. m.data.SetEmptyGauge()
  111. }
  112. func (m *metricSshcheckSftpDuration) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64) {
  113. if !m.config.Enabled {
  114. return
  115. }
  116. dp := m.data.Gauge().DataPoints().AppendEmpty()
  117. dp.SetStartTimestamp(start)
  118. dp.SetTimestamp(ts)
  119. dp.SetIntValue(val)
  120. }
  121. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  122. func (m *metricSshcheckSftpDuration) updateCapacity() {
  123. if m.data.Gauge().DataPoints().Len() > m.capacity {
  124. m.capacity = m.data.Gauge().DataPoints().Len()
  125. }
  126. }
  127. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  128. func (m *metricSshcheckSftpDuration) emit(metrics pmetric.MetricSlice) {
  129. if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 {
  130. m.updateCapacity()
  131. m.data.MoveTo(metrics.AppendEmpty())
  132. m.init()
  133. }
  134. }
  135. func newMetricSshcheckSftpDuration(cfg MetricConfig) metricSshcheckSftpDuration {
  136. m := metricSshcheckSftpDuration{config: cfg}
  137. if cfg.Enabled {
  138. m.data = pmetric.NewMetric()
  139. m.init()
  140. }
  141. return m
  142. }
  143. type metricSshcheckSftpError struct {
  144. data pmetric.Metric // data buffer for generated metric.
  145. config MetricConfig // metric config provided by user.
  146. capacity int // max observed number of data points added to the metric.
  147. }
  148. // init fills sshcheck.sftp_error metric with initial data.
  149. func (m *metricSshcheckSftpError) init() {
  150. m.data.SetName("sshcheck.sftp_error")
  151. m.data.SetDescription("Records errors occurring during SFTP check.")
  152. m.data.SetUnit("{error}")
  153. m.data.SetEmptySum()
  154. m.data.Sum().SetIsMonotonic(false)
  155. m.data.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  156. m.data.Sum().DataPoints().EnsureCapacity(m.capacity)
  157. }
  158. func (m *metricSshcheckSftpError) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, errorMessageAttributeValue string) {
  159. if !m.config.Enabled {
  160. return
  161. }
  162. dp := m.data.Sum().DataPoints().AppendEmpty()
  163. dp.SetStartTimestamp(start)
  164. dp.SetTimestamp(ts)
  165. dp.SetIntValue(val)
  166. dp.Attributes().PutStr("error.message", errorMessageAttributeValue)
  167. }
  168. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  169. func (m *metricSshcheckSftpError) updateCapacity() {
  170. if m.data.Sum().DataPoints().Len() > m.capacity {
  171. m.capacity = m.data.Sum().DataPoints().Len()
  172. }
  173. }
  174. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  175. func (m *metricSshcheckSftpError) emit(metrics pmetric.MetricSlice) {
  176. if m.config.Enabled && m.data.Sum().DataPoints().Len() > 0 {
  177. m.updateCapacity()
  178. m.data.MoveTo(metrics.AppendEmpty())
  179. m.init()
  180. }
  181. }
  182. func newMetricSshcheckSftpError(cfg MetricConfig) metricSshcheckSftpError {
  183. m := metricSshcheckSftpError{config: cfg}
  184. if cfg.Enabled {
  185. m.data = pmetric.NewMetric()
  186. m.init()
  187. }
  188. return m
  189. }
  190. type metricSshcheckSftpStatus struct {
  191. data pmetric.Metric // data buffer for generated metric.
  192. config MetricConfig // metric config provided by user.
  193. capacity int // max observed number of data points added to the metric.
  194. }
  195. // init fills sshcheck.sftp_status metric with initial data.
  196. func (m *metricSshcheckSftpStatus) init() {
  197. m.data.SetName("sshcheck.sftp_status")
  198. m.data.SetDescription("1 if the SFTP server replied to request, otherwise 0.")
  199. m.data.SetUnit("1")
  200. m.data.SetEmptySum()
  201. m.data.Sum().SetIsMonotonic(false)
  202. m.data.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  203. }
  204. func (m *metricSshcheckSftpStatus) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64) {
  205. if !m.config.Enabled {
  206. return
  207. }
  208. dp := m.data.Sum().DataPoints().AppendEmpty()
  209. dp.SetStartTimestamp(start)
  210. dp.SetTimestamp(ts)
  211. dp.SetIntValue(val)
  212. }
  213. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  214. func (m *metricSshcheckSftpStatus) updateCapacity() {
  215. if m.data.Sum().DataPoints().Len() > m.capacity {
  216. m.capacity = m.data.Sum().DataPoints().Len()
  217. }
  218. }
  219. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  220. func (m *metricSshcheckSftpStatus) emit(metrics pmetric.MetricSlice) {
  221. if m.config.Enabled && m.data.Sum().DataPoints().Len() > 0 {
  222. m.updateCapacity()
  223. m.data.MoveTo(metrics.AppendEmpty())
  224. m.init()
  225. }
  226. }
  227. func newMetricSshcheckSftpStatus(cfg MetricConfig) metricSshcheckSftpStatus {
  228. m := metricSshcheckSftpStatus{config: cfg}
  229. if cfg.Enabled {
  230. m.data = pmetric.NewMetric()
  231. m.init()
  232. }
  233. return m
  234. }
  235. type metricSshcheckStatus struct {
  236. data pmetric.Metric // data buffer for generated metric.
  237. config MetricConfig // metric config provided by user.
  238. capacity int // max observed number of data points added to the metric.
  239. }
  240. // init fills sshcheck.status metric with initial data.
  241. func (m *metricSshcheckStatus) init() {
  242. m.data.SetName("sshcheck.status")
  243. m.data.SetDescription("1 if the SSH client successfully connected, otherwise 0.")
  244. m.data.SetUnit("1")
  245. m.data.SetEmptySum()
  246. m.data.Sum().SetIsMonotonic(false)
  247. m.data.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  248. }
  249. func (m *metricSshcheckStatus) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64) {
  250. if !m.config.Enabled {
  251. return
  252. }
  253. dp := m.data.Sum().DataPoints().AppendEmpty()
  254. dp.SetStartTimestamp(start)
  255. dp.SetTimestamp(ts)
  256. dp.SetIntValue(val)
  257. }
  258. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  259. func (m *metricSshcheckStatus) updateCapacity() {
  260. if m.data.Sum().DataPoints().Len() > m.capacity {
  261. m.capacity = m.data.Sum().DataPoints().Len()
  262. }
  263. }
  264. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  265. func (m *metricSshcheckStatus) emit(metrics pmetric.MetricSlice) {
  266. if m.config.Enabled && m.data.Sum().DataPoints().Len() > 0 {
  267. m.updateCapacity()
  268. m.data.MoveTo(metrics.AppendEmpty())
  269. m.init()
  270. }
  271. }
  272. func newMetricSshcheckStatus(cfg MetricConfig) metricSshcheckStatus {
  273. m := metricSshcheckStatus{config: cfg}
  274. if cfg.Enabled {
  275. m.data = pmetric.NewMetric()
  276. m.init()
  277. }
  278. return m
  279. }
  280. // MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations
  281. // required to produce metric representation defined in metadata and user config.
  282. type MetricsBuilder struct {
  283. config MetricsBuilderConfig // config of the metrics builder.
  284. startTime pcommon.Timestamp // start time that will be applied to all recorded data points.
  285. metricsCapacity int // maximum observed number of metrics per resource.
  286. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
  287. buildInfo component.BuildInfo // contains version information.
  288. metricSshcheckDuration metricSshcheckDuration
  289. metricSshcheckError metricSshcheckError
  290. metricSshcheckSftpDuration metricSshcheckSftpDuration
  291. metricSshcheckSftpError metricSshcheckSftpError
  292. metricSshcheckSftpStatus metricSshcheckSftpStatus
  293. metricSshcheckStatus metricSshcheckStatus
  294. }
  295. // metricBuilderOption applies changes to default metrics builder.
  296. type metricBuilderOption func(*MetricsBuilder)
  297. // WithStartTime sets startTime on the metrics builder.
  298. func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption {
  299. return func(mb *MetricsBuilder) {
  300. mb.startTime = startTime
  301. }
  302. }
  303. func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder {
  304. mb := &MetricsBuilder{
  305. config: mbc,
  306. startTime: pcommon.NewTimestampFromTime(time.Now()),
  307. metricsBuffer: pmetric.NewMetrics(),
  308. buildInfo: settings.BuildInfo,
  309. metricSshcheckDuration: newMetricSshcheckDuration(mbc.Metrics.SshcheckDuration),
  310. metricSshcheckError: newMetricSshcheckError(mbc.Metrics.SshcheckError),
  311. metricSshcheckSftpDuration: newMetricSshcheckSftpDuration(mbc.Metrics.SshcheckSftpDuration),
  312. metricSshcheckSftpError: newMetricSshcheckSftpError(mbc.Metrics.SshcheckSftpError),
  313. metricSshcheckSftpStatus: newMetricSshcheckSftpStatus(mbc.Metrics.SshcheckSftpStatus),
  314. metricSshcheckStatus: newMetricSshcheckStatus(mbc.Metrics.SshcheckStatus),
  315. }
  316. for _, op := range options {
  317. op(mb)
  318. }
  319. return mb
  320. }
  321. // NewResourceBuilder returns a new resource builder that should be used to build a resource associated with for the emitted metrics.
  322. func (mb *MetricsBuilder) NewResourceBuilder() *ResourceBuilder {
  323. return NewResourceBuilder(mb.config.ResourceAttributes)
  324. }
  325. // updateCapacity updates max length of metrics and resource attributes that will be used for the slice capacity.
  326. func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) {
  327. if mb.metricsCapacity < rm.ScopeMetrics().At(0).Metrics().Len() {
  328. mb.metricsCapacity = rm.ScopeMetrics().At(0).Metrics().Len()
  329. }
  330. }
  331. // ResourceMetricsOption applies changes to provided resource metrics.
  332. type ResourceMetricsOption func(pmetric.ResourceMetrics)
  333. // WithResource sets the provided resource on the emitted ResourceMetrics.
  334. // It's recommended to use ResourceBuilder to create the resource.
  335. func WithResource(res pcommon.Resource) ResourceMetricsOption {
  336. return func(rm pmetric.ResourceMetrics) {
  337. res.CopyTo(rm.Resource())
  338. }
  339. }
  340. // WithStartTimeOverride overrides start time for all the resource metrics data points.
  341. // This option should be only used if different start time has to be set on metrics coming from different resources.
  342. func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption {
  343. return func(rm pmetric.ResourceMetrics) {
  344. var dps pmetric.NumberDataPointSlice
  345. metrics := rm.ScopeMetrics().At(0).Metrics()
  346. for i := 0; i < metrics.Len(); i++ {
  347. switch metrics.At(i).Type() {
  348. case pmetric.MetricTypeGauge:
  349. dps = metrics.At(i).Gauge().DataPoints()
  350. case pmetric.MetricTypeSum:
  351. dps = metrics.At(i).Sum().DataPoints()
  352. }
  353. for j := 0; j < dps.Len(); j++ {
  354. dps.At(j).SetStartTimestamp(start)
  355. }
  356. }
  357. }
  358. }
  359. // EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for
  360. // recording another set of data points as part of another resource. This function can be helpful when one scraper
  361. // needs to emit metrics from several resources. Otherwise calling this function is not required,
  362. // just `Emit` function can be called instead.
  363. // Resource attributes should be provided as ResourceMetricsOption arguments.
  364. func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) {
  365. rm := pmetric.NewResourceMetrics()
  366. ils := rm.ScopeMetrics().AppendEmpty()
  367. ils.Scope().SetName("otelcol/sshcheckreceiver")
  368. ils.Scope().SetVersion(mb.buildInfo.Version)
  369. ils.Metrics().EnsureCapacity(mb.metricsCapacity)
  370. mb.metricSshcheckDuration.emit(ils.Metrics())
  371. mb.metricSshcheckError.emit(ils.Metrics())
  372. mb.metricSshcheckSftpDuration.emit(ils.Metrics())
  373. mb.metricSshcheckSftpError.emit(ils.Metrics())
  374. mb.metricSshcheckSftpStatus.emit(ils.Metrics())
  375. mb.metricSshcheckStatus.emit(ils.Metrics())
  376. for _, op := range rmo {
  377. op(rm)
  378. }
  379. if ils.Metrics().Len() > 0 {
  380. mb.updateCapacity(rm)
  381. rm.MoveTo(mb.metricsBuffer.ResourceMetrics().AppendEmpty())
  382. }
  383. }
  384. // Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for
  385. // recording another set of metrics. This function will be responsible for applying all the transformations required to
  386. // produce metric representation defined in metadata and user config, e.g. delta or cumulative.
  387. func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics {
  388. mb.EmitForResource(rmo...)
  389. metrics := mb.metricsBuffer
  390. mb.metricsBuffer = pmetric.NewMetrics()
  391. return metrics
  392. }
  393. // RecordSshcheckDurationDataPoint adds a data point to sshcheck.duration metric.
  394. func (mb *MetricsBuilder) RecordSshcheckDurationDataPoint(ts pcommon.Timestamp, val int64) {
  395. mb.metricSshcheckDuration.recordDataPoint(mb.startTime, ts, val)
  396. }
  397. // RecordSshcheckErrorDataPoint adds a data point to sshcheck.error metric.
  398. func (mb *MetricsBuilder) RecordSshcheckErrorDataPoint(ts pcommon.Timestamp, val int64, errorMessageAttributeValue string) {
  399. mb.metricSshcheckError.recordDataPoint(mb.startTime, ts, val, errorMessageAttributeValue)
  400. }
  401. // RecordSshcheckSftpDurationDataPoint adds a data point to sshcheck.sftp_duration metric.
  402. func (mb *MetricsBuilder) RecordSshcheckSftpDurationDataPoint(ts pcommon.Timestamp, val int64) {
  403. mb.metricSshcheckSftpDuration.recordDataPoint(mb.startTime, ts, val)
  404. }
  405. // RecordSshcheckSftpErrorDataPoint adds a data point to sshcheck.sftp_error metric.
  406. func (mb *MetricsBuilder) RecordSshcheckSftpErrorDataPoint(ts pcommon.Timestamp, val int64, errorMessageAttributeValue string) {
  407. mb.metricSshcheckSftpError.recordDataPoint(mb.startTime, ts, val, errorMessageAttributeValue)
  408. }
  409. // RecordSshcheckSftpStatusDataPoint adds a data point to sshcheck.sftp_status metric.
  410. func (mb *MetricsBuilder) RecordSshcheckSftpStatusDataPoint(ts pcommon.Timestamp, val int64) {
  411. mb.metricSshcheckSftpStatus.recordDataPoint(mb.startTime, ts, val)
  412. }
  413. // RecordSshcheckStatusDataPoint adds a data point to sshcheck.status metric.
  414. func (mb *MetricsBuilder) RecordSshcheckStatusDataPoint(ts pcommon.Timestamp, val int64) {
  415. mb.metricSshcheckStatus.recordDataPoint(mb.startTime, ts, val)
  416. }
  417. // Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted,
  418. // and metrics builder should update its startTime and reset it's internal state accordingly.
  419. func (mb *MetricsBuilder) Reset(options ...metricBuilderOption) {
  420. mb.startTime = pcommon.NewTimestampFromTime(time.Now())
  421. for _, op := range options {
  422. op(mb)
  423. }
  424. }