generated_metrics.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 metricHttpcheckDuration 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 httpcheck.duration metric with initial data.
  16. func (m *metricHttpcheckDuration) init() {
  17. m.data.SetName("httpcheck.duration")
  18. m.data.SetDescription("Measures the duration of the HTTP check.")
  19. m.data.SetUnit("ms")
  20. m.data.SetEmptyGauge()
  21. m.data.Gauge().DataPoints().EnsureCapacity(m.capacity)
  22. }
  23. func (m *metricHttpcheckDuration) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, httpURLAttributeValue string) {
  24. if !m.config.Enabled {
  25. return
  26. }
  27. dp := m.data.Gauge().DataPoints().AppendEmpty()
  28. dp.SetStartTimestamp(start)
  29. dp.SetTimestamp(ts)
  30. dp.SetIntValue(val)
  31. dp.Attributes().PutStr("http.url", httpURLAttributeValue)
  32. }
  33. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  34. func (m *metricHttpcheckDuration) updateCapacity() {
  35. if m.data.Gauge().DataPoints().Len() > m.capacity {
  36. m.capacity = m.data.Gauge().DataPoints().Len()
  37. }
  38. }
  39. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  40. func (m *metricHttpcheckDuration) emit(metrics pmetric.MetricSlice) {
  41. if m.config.Enabled && m.data.Gauge().DataPoints().Len() > 0 {
  42. m.updateCapacity()
  43. m.data.MoveTo(metrics.AppendEmpty())
  44. m.init()
  45. }
  46. }
  47. func newMetricHttpcheckDuration(cfg MetricConfig) metricHttpcheckDuration {
  48. m := metricHttpcheckDuration{config: cfg}
  49. if cfg.Enabled {
  50. m.data = pmetric.NewMetric()
  51. m.init()
  52. }
  53. return m
  54. }
  55. type metricHttpcheckError struct {
  56. data pmetric.Metric // data buffer for generated metric.
  57. config MetricConfig // metric config provided by user.
  58. capacity int // max observed number of data points added to the metric.
  59. }
  60. // init fills httpcheck.error metric with initial data.
  61. func (m *metricHttpcheckError) init() {
  62. m.data.SetName("httpcheck.error")
  63. m.data.SetDescription("Records errors occurring during HTTP check.")
  64. m.data.SetUnit("{error}")
  65. m.data.SetEmptySum()
  66. m.data.Sum().SetIsMonotonic(false)
  67. m.data.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  68. m.data.Sum().DataPoints().EnsureCapacity(m.capacity)
  69. }
  70. func (m *metricHttpcheckError) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, httpURLAttributeValue string, errorMessageAttributeValue string) {
  71. if !m.config.Enabled {
  72. return
  73. }
  74. dp := m.data.Sum().DataPoints().AppendEmpty()
  75. dp.SetStartTimestamp(start)
  76. dp.SetTimestamp(ts)
  77. dp.SetIntValue(val)
  78. dp.Attributes().PutStr("http.url", httpURLAttributeValue)
  79. dp.Attributes().PutStr("error.message", errorMessageAttributeValue)
  80. }
  81. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  82. func (m *metricHttpcheckError) updateCapacity() {
  83. if m.data.Sum().DataPoints().Len() > m.capacity {
  84. m.capacity = m.data.Sum().DataPoints().Len()
  85. }
  86. }
  87. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  88. func (m *metricHttpcheckError) emit(metrics pmetric.MetricSlice) {
  89. if m.config.Enabled && m.data.Sum().DataPoints().Len() > 0 {
  90. m.updateCapacity()
  91. m.data.MoveTo(metrics.AppendEmpty())
  92. m.init()
  93. }
  94. }
  95. func newMetricHttpcheckError(cfg MetricConfig) metricHttpcheckError {
  96. m := metricHttpcheckError{config: cfg}
  97. if cfg.Enabled {
  98. m.data = pmetric.NewMetric()
  99. m.init()
  100. }
  101. return m
  102. }
  103. type metricHttpcheckStatus struct {
  104. data pmetric.Metric // data buffer for generated metric.
  105. config MetricConfig // metric config provided by user.
  106. capacity int // max observed number of data points added to the metric.
  107. }
  108. // init fills httpcheck.status metric with initial data.
  109. func (m *metricHttpcheckStatus) init() {
  110. m.data.SetName("httpcheck.status")
  111. m.data.SetDescription("1 if the check resulted in status_code matching the status_class, otherwise 0.")
  112. m.data.SetUnit("1")
  113. m.data.SetEmptySum()
  114. m.data.Sum().SetIsMonotonic(false)
  115. m.data.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
  116. m.data.Sum().DataPoints().EnsureCapacity(m.capacity)
  117. }
  118. func (m *metricHttpcheckStatus) recordDataPoint(start pcommon.Timestamp, ts pcommon.Timestamp, val int64, httpURLAttributeValue string, httpStatusCodeAttributeValue int64, httpMethodAttributeValue string, httpStatusClassAttributeValue string) {
  119. if !m.config.Enabled {
  120. return
  121. }
  122. dp := m.data.Sum().DataPoints().AppendEmpty()
  123. dp.SetStartTimestamp(start)
  124. dp.SetTimestamp(ts)
  125. dp.SetIntValue(val)
  126. dp.Attributes().PutStr("http.url", httpURLAttributeValue)
  127. dp.Attributes().PutInt("http.status_code", httpStatusCodeAttributeValue)
  128. dp.Attributes().PutStr("http.method", httpMethodAttributeValue)
  129. dp.Attributes().PutStr("http.status_class", httpStatusClassAttributeValue)
  130. }
  131. // updateCapacity saves max length of data point slices that will be used for the slice capacity.
  132. func (m *metricHttpcheckStatus) updateCapacity() {
  133. if m.data.Sum().DataPoints().Len() > m.capacity {
  134. m.capacity = m.data.Sum().DataPoints().Len()
  135. }
  136. }
  137. // emit appends recorded metric data to a metrics slice and prepares it for recording another set of data points.
  138. func (m *metricHttpcheckStatus) emit(metrics pmetric.MetricSlice) {
  139. if m.config.Enabled && m.data.Sum().DataPoints().Len() > 0 {
  140. m.updateCapacity()
  141. m.data.MoveTo(metrics.AppendEmpty())
  142. m.init()
  143. }
  144. }
  145. func newMetricHttpcheckStatus(cfg MetricConfig) metricHttpcheckStatus {
  146. m := metricHttpcheckStatus{config: cfg}
  147. if cfg.Enabled {
  148. m.data = pmetric.NewMetric()
  149. m.init()
  150. }
  151. return m
  152. }
  153. // MetricsBuilder provides an interface for scrapers to report metrics while taking care of all the transformations
  154. // required to produce metric representation defined in metadata and user config.
  155. type MetricsBuilder struct {
  156. config MetricsBuilderConfig // config of the metrics builder.
  157. startTime pcommon.Timestamp // start time that will be applied to all recorded data points.
  158. metricsCapacity int // maximum observed number of metrics per resource.
  159. metricsBuffer pmetric.Metrics // accumulates metrics data before emitting.
  160. buildInfo component.BuildInfo // contains version information.
  161. metricHttpcheckDuration metricHttpcheckDuration
  162. metricHttpcheckError metricHttpcheckError
  163. metricHttpcheckStatus metricHttpcheckStatus
  164. }
  165. // metricBuilderOption applies changes to default metrics builder.
  166. type metricBuilderOption func(*MetricsBuilder)
  167. // WithStartTime sets startTime on the metrics builder.
  168. func WithStartTime(startTime pcommon.Timestamp) metricBuilderOption {
  169. return func(mb *MetricsBuilder) {
  170. mb.startTime = startTime
  171. }
  172. }
  173. func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.CreateSettings, options ...metricBuilderOption) *MetricsBuilder {
  174. mb := &MetricsBuilder{
  175. config: mbc,
  176. startTime: pcommon.NewTimestampFromTime(time.Now()),
  177. metricsBuffer: pmetric.NewMetrics(),
  178. buildInfo: settings.BuildInfo,
  179. metricHttpcheckDuration: newMetricHttpcheckDuration(mbc.Metrics.HttpcheckDuration),
  180. metricHttpcheckError: newMetricHttpcheckError(mbc.Metrics.HttpcheckError),
  181. metricHttpcheckStatus: newMetricHttpcheckStatus(mbc.Metrics.HttpcheckStatus),
  182. }
  183. for _, op := range options {
  184. op(mb)
  185. }
  186. return mb
  187. }
  188. // updateCapacity updates max length of metrics and resource attributes that will be used for the slice capacity.
  189. func (mb *MetricsBuilder) updateCapacity(rm pmetric.ResourceMetrics) {
  190. if mb.metricsCapacity < rm.ScopeMetrics().At(0).Metrics().Len() {
  191. mb.metricsCapacity = rm.ScopeMetrics().At(0).Metrics().Len()
  192. }
  193. }
  194. // ResourceMetricsOption applies changes to provided resource metrics.
  195. type ResourceMetricsOption func(pmetric.ResourceMetrics)
  196. // WithResource sets the provided resource on the emitted ResourceMetrics.
  197. // It's recommended to use ResourceBuilder to create the resource.
  198. func WithResource(res pcommon.Resource) ResourceMetricsOption {
  199. return func(rm pmetric.ResourceMetrics) {
  200. res.CopyTo(rm.Resource())
  201. }
  202. }
  203. // WithStartTimeOverride overrides start time for all the resource metrics data points.
  204. // This option should be only used if different start time has to be set on metrics coming from different resources.
  205. func WithStartTimeOverride(start pcommon.Timestamp) ResourceMetricsOption {
  206. return func(rm pmetric.ResourceMetrics) {
  207. var dps pmetric.NumberDataPointSlice
  208. metrics := rm.ScopeMetrics().At(0).Metrics()
  209. for i := 0; i < metrics.Len(); i++ {
  210. switch metrics.At(i).Type() {
  211. case pmetric.MetricTypeGauge:
  212. dps = metrics.At(i).Gauge().DataPoints()
  213. case pmetric.MetricTypeSum:
  214. dps = metrics.At(i).Sum().DataPoints()
  215. }
  216. for j := 0; j < dps.Len(); j++ {
  217. dps.At(j).SetStartTimestamp(start)
  218. }
  219. }
  220. }
  221. }
  222. // EmitForResource saves all the generated metrics under a new resource and updates the internal state to be ready for
  223. // recording another set of data points as part of another resource. This function can be helpful when one scraper
  224. // needs to emit metrics from several resources. Otherwise calling this function is not required,
  225. // just `Emit` function can be called instead.
  226. // Resource attributes should be provided as ResourceMetricsOption arguments.
  227. func (mb *MetricsBuilder) EmitForResource(rmo ...ResourceMetricsOption) {
  228. rm := pmetric.NewResourceMetrics()
  229. ils := rm.ScopeMetrics().AppendEmpty()
  230. ils.Scope().SetName("otelcol/httpcheckreceiver")
  231. ils.Scope().SetVersion(mb.buildInfo.Version)
  232. ils.Metrics().EnsureCapacity(mb.metricsCapacity)
  233. mb.metricHttpcheckDuration.emit(ils.Metrics())
  234. mb.metricHttpcheckError.emit(ils.Metrics())
  235. mb.metricHttpcheckStatus.emit(ils.Metrics())
  236. for _, op := range rmo {
  237. op(rm)
  238. }
  239. if ils.Metrics().Len() > 0 {
  240. mb.updateCapacity(rm)
  241. rm.MoveTo(mb.metricsBuffer.ResourceMetrics().AppendEmpty())
  242. }
  243. }
  244. // Emit returns all the metrics accumulated by the metrics builder and updates the internal state to be ready for
  245. // recording another set of metrics. This function will be responsible for applying all the transformations required to
  246. // produce metric representation defined in metadata and user config, e.g. delta or cumulative.
  247. func (mb *MetricsBuilder) Emit(rmo ...ResourceMetricsOption) pmetric.Metrics {
  248. mb.EmitForResource(rmo...)
  249. metrics := mb.metricsBuffer
  250. mb.metricsBuffer = pmetric.NewMetrics()
  251. return metrics
  252. }
  253. // RecordHttpcheckDurationDataPoint adds a data point to httpcheck.duration metric.
  254. func (mb *MetricsBuilder) RecordHttpcheckDurationDataPoint(ts pcommon.Timestamp, val int64, httpURLAttributeValue string) {
  255. mb.metricHttpcheckDuration.recordDataPoint(mb.startTime, ts, val, httpURLAttributeValue)
  256. }
  257. // RecordHttpcheckErrorDataPoint adds a data point to httpcheck.error metric.
  258. func (mb *MetricsBuilder) RecordHttpcheckErrorDataPoint(ts pcommon.Timestamp, val int64, httpURLAttributeValue string, errorMessageAttributeValue string) {
  259. mb.metricHttpcheckError.recordDataPoint(mb.startTime, ts, val, httpURLAttributeValue, errorMessageAttributeValue)
  260. }
  261. // RecordHttpcheckStatusDataPoint adds a data point to httpcheck.status metric.
  262. func (mb *MetricsBuilder) RecordHttpcheckStatusDataPoint(ts pcommon.Timestamp, val int64, httpURLAttributeValue string, httpStatusCodeAttributeValue int64, httpMethodAttributeValue string, httpStatusClassAttributeValue string) {
  263. mb.metricHttpcheckStatus.recordDataPoint(mb.startTime, ts, val, httpURLAttributeValue, httpStatusCodeAttributeValue, httpMethodAttributeValue, httpStatusClassAttributeValue)
  264. }
  265. // Reset resets metrics builder to its initial state. It should be used when external metrics source is restarted,
  266. // and metrics builder should update its startTime and reset it's internal state accordingly.
  267. func (mb *MetricsBuilder) Reset(options ...metricBuilderOption) {
  268. mb.startTime = pcommon.NewTimestampFromTime(time.Now())
  269. for _, op := range options {
  270. op(mb)
  271. }
  272. }