queries.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package saphanareceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver"
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "go.opentelemetry.io/collector/pdata/pcommon"
  10. "go.opentelemetry.io/collector/receiver/scrapererror"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/saphanareceiver/internal/metadata"
  12. )
  13. type queryStat struct {
  14. key string
  15. addMetricFunction func(*metadata.MetricsBuilder, pcommon.Timestamp, string, map[string]string) error
  16. }
  17. func (q *queryStat) collectStat(s *sapHanaScraper, m *monitoringQuery, now pcommon.Timestamp,
  18. row map[string]string) error {
  19. if val, ok := row[q.key]; ok {
  20. resourceAttributes := map[string]string{}
  21. for _, attr := range m.orderedResourceLabels {
  22. attrValue, ok := row[attr]
  23. if !ok {
  24. return fmt.Errorf("unable to parse metric for key %s, missing resource attribute '%s'", q.key, attr)
  25. }
  26. resourceAttributes[attr] = attrValue
  27. }
  28. mb, err := s.getMetricsBuilder(resourceAttributes)
  29. if err != nil {
  30. return fmt.Errorf("unable to parse metric for key %s: %w", q.key, err)
  31. }
  32. if q.addMetricFunction != nil {
  33. if err = q.addMetricFunction(mb, now, val, row); err != nil {
  34. return fmt.Errorf("failed to record metric for key %s: %w", q.key, err)
  35. }
  36. } else {
  37. return errors.New("incorrectly configured query, addMetricFunction must be provided")
  38. }
  39. }
  40. return nil
  41. }
  42. type monitoringQuery struct {
  43. query string
  44. orderedResourceLabels []string
  45. orderedMetricLabels []string
  46. orderedStats []queryStat
  47. Enabled func(c *Config) bool
  48. }
  49. var queries = []monitoringQuery{
  50. {
  51. query: "SELECT HOST, SUM(CASE WHEN ACTIVE_STATUS = 'YES' THEN 1 ELSE 0 END) AS active_services, SUM(CASE WHEN ACTIVE_STATUS = 'YES' THEN 0 ELSE 1 END) AS inactive_services FROM SYS.M_SERVICES GROUP BY HOST",
  52. orderedResourceLabels: []string{"host"},
  53. orderedStats: []queryStat{
  54. {
  55. key: "active_services",
  56. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  57. row map[string]string) error {
  58. return mb.RecordSaphanaServiceCountDataPoint(now, val, metadata.AttributeServiceStatusActive)
  59. },
  60. },
  61. {
  62. key: "inactive_services",
  63. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  64. row map[string]string) error {
  65. return mb.RecordSaphanaServiceCountDataPoint(now, val, metadata.AttributeServiceStatusInactive)
  66. },
  67. },
  68. },
  69. Enabled: func(c *Config) bool {
  70. return c.MetricsBuilderConfig.Metrics.SaphanaServiceCount.Enabled
  71. },
  72. },
  73. {
  74. query: "SELECT HOST, SUM(CASE WHEN IS_ACTIVE = 'TRUE' THEN 1 ELSE 0 END) AS active_threads, SUM(CASE WHEN IS_ACTIVE = 'TRUE' THEN 0 ELSE 1 END) AS inactive_threads FROM SYS.M_SERVICE_THREADS GROUP BY HOST",
  75. orderedResourceLabels: []string{"host"},
  76. orderedStats: []queryStat{
  77. {
  78. key: "active_threads",
  79. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  80. row map[string]string) error {
  81. return mb.RecordSaphanaServiceThreadCountDataPoint(now, val, metadata.AttributeThreadStatusActive)
  82. },
  83. },
  84. {
  85. key: "inactive_threads",
  86. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  87. row map[string]string) error {
  88. return mb.RecordSaphanaServiceThreadCountDataPoint(now, val, metadata.AttributeThreadStatusInactive)
  89. },
  90. },
  91. },
  92. Enabled: func(c *Config) bool {
  93. return c.MetricsBuilderConfig.Metrics.SaphanaServiceThreadCount.Enabled
  94. },
  95. },
  96. {
  97. query: "SELECT HOST, SUM(MAIN_MEMORY_SIZE_IN_DATA) AS \"mem_main_data\", SUM(MAIN_MEMORY_SIZE_IN_DICT) AS \"mem_main_dict\", SUM(MAIN_MEMORY_SIZE_IN_INDEX) AS \"mem_main_index\", SUM(MAIN_MEMORY_SIZE_IN_MISC) AS \"mem_main_misc\", SUM(DELTA_MEMORY_SIZE_IN_DATA) AS \"mem_delta_data\", SUM(DELTA_MEMORY_SIZE_IN_DICT) AS \"mem_delta_dict\", SUM(DELTA_MEMORY_SIZE_IN_INDEX) AS \"mem_delta_index\", SUM(DELTA_MEMORY_SIZE_IN_MISC) AS \"mem_delta_misc\" FROM M_CS_ALL_COLUMNS GROUP BY HOST",
  98. orderedResourceLabels: []string{"host"},
  99. orderedStats: []queryStat{
  100. {
  101. key: "main_data",
  102. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  103. row map[string]string) error {
  104. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeMain, metadata.AttributeColumnMemorySubtypeData)
  105. },
  106. },
  107. {
  108. key: "main_dict",
  109. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  110. row map[string]string) error {
  111. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeMain, metadata.AttributeColumnMemorySubtypeDict)
  112. },
  113. },
  114. {
  115. key: "main_index",
  116. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  117. row map[string]string) error {
  118. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeMain, metadata.AttributeColumnMemorySubtypeIndex)
  119. },
  120. },
  121. {
  122. key: "main_misc",
  123. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  124. row map[string]string) error {
  125. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeMain, metadata.AttributeColumnMemorySubtypeMisc)
  126. },
  127. },
  128. {
  129. key: "delta_data",
  130. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  131. row map[string]string) error {
  132. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeDelta, metadata.AttributeColumnMemorySubtypeData)
  133. },
  134. },
  135. {
  136. key: "delta_dict",
  137. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  138. row map[string]string) error {
  139. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeDelta, metadata.AttributeColumnMemorySubtypeDict)
  140. },
  141. },
  142. {
  143. key: "delta_index",
  144. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  145. row map[string]string) error {
  146. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeDelta, metadata.AttributeColumnMemorySubtypeIndex)
  147. },
  148. },
  149. {
  150. key: "delta_misc",
  151. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  152. row map[string]string) error {
  153. return mb.RecordSaphanaColumnMemoryUsedDataPoint(now, val, metadata.AttributeColumnMemoryTypeDelta, metadata.AttributeColumnMemorySubtypeMisc)
  154. },
  155. },
  156. },
  157. Enabled: func(c *Config) bool {
  158. return c.MetricsBuilderConfig.Metrics.SaphanaColumnMemoryUsed.Enabled
  159. },
  160. },
  161. {
  162. query: "SELECT HOST, SUM(USED_FIXED_PART_SIZE) fixed, SUM(USED_VARIABLE_PART_SIZE) variable FROM SYS.M_RS_TABLES GROUP BY HOST",
  163. orderedResourceLabels: []string{"host"},
  164. orderedStats: []queryStat{
  165. {
  166. key: "fixed",
  167. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  168. row map[string]string) error {
  169. return mb.RecordSaphanaRowStoreMemoryUsedDataPoint(now, val, metadata.AttributeRowMemoryTypeFixed)
  170. },
  171. },
  172. {
  173. key: "variable",
  174. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  175. row map[string]string) error {
  176. return mb.RecordSaphanaRowStoreMemoryUsedDataPoint(now, val, metadata.AttributeRowMemoryTypeVariable)
  177. },
  178. },
  179. },
  180. Enabled: func(c *Config) bool {
  181. return c.MetricsBuilderConfig.Metrics.SaphanaRowStoreMemoryUsed.Enabled
  182. },
  183. },
  184. {
  185. query: "SELECT HOST, COMPONENT, sum(USED_MEMORY_SIZE) used_mem_size FROM SYS.M_SERVICE_COMPONENT_MEMORY GROUP BY HOST, COMPONENT",
  186. orderedResourceLabels: []string{"host"},
  187. orderedMetricLabels: []string{"component"},
  188. orderedStats: []queryStat{
  189. {
  190. key: "used_mem_size",
  191. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  192. row map[string]string) error {
  193. return mb.RecordSaphanaComponentMemoryUsedDataPoint(now, val, row["component"])
  194. },
  195. },
  196. },
  197. Enabled: func(c *Config) bool {
  198. return c.MetricsBuilderConfig.Metrics.SaphanaComponentMemoryUsed.Enabled
  199. },
  200. },
  201. {
  202. query: "SELECT HOST, CONNECTION_STATUS, COUNT(*) AS connections FROM SYS.M_CONNECTIONS WHERE CONNECTION_STATUS != '' GROUP BY HOST, CONNECTION_STATUS",
  203. orderedResourceLabels: []string{"host"},
  204. orderedMetricLabels: []string{"connection_status"},
  205. orderedStats: []queryStat{
  206. {
  207. key: "connections",
  208. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  209. row map[string]string) error {
  210. return mb.RecordSaphanaConnectionCountDataPoint(now, val,
  211. metadata.MapAttributeConnectionStatus[strings.ToLower(row["connection_status"])])
  212. },
  213. },
  214. },
  215. Enabled: func(c *Config) bool {
  216. return c.MetricsBuilderConfig.Metrics.SaphanaConnectionCount.Enabled
  217. },
  218. },
  219. {
  220. query: "SELECT seconds_between(CURRENT_TIMESTAMP, UTC_START_TIME) age FROM SYS.M_BACKUP_CATALOG WHERE STATE_NAME = 'successful' ORDER BY UTC_START_TIME DESC LIMIT 1",
  221. orderedMetricLabels: []string{},
  222. orderedStats: []queryStat{
  223. {
  224. key: "age",
  225. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  226. row map[string]string) error {
  227. return mb.RecordSaphanaBackupLatestDataPoint(now, val)
  228. },
  229. },
  230. },
  231. Enabled: func(c *Config) bool {
  232. return c.MetricsBuilderConfig.Metrics.SaphanaBackupLatest.Enabled
  233. },
  234. },
  235. {
  236. query: "SELECT HOST, SYSTEM_ID, DATABASE_NAME, seconds_between(START_TIME, CURRENT_TIMESTAMP) age FROM SYS.M_DATABASE",
  237. orderedResourceLabels: []string{"host"},
  238. orderedMetricLabels: []string{"system", "database"},
  239. orderedStats: []queryStat{
  240. {
  241. key: "age",
  242. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  243. row map[string]string) error {
  244. return mb.RecordSaphanaUptimeDataPoint(now, val, row["system"], row["database"])
  245. },
  246. },
  247. },
  248. Enabled: func(c *Config) bool {
  249. return c.MetricsBuilderConfig.Metrics.SaphanaUptime.Enabled
  250. },
  251. },
  252. {
  253. query: "SELECT ALERT_RATING, COUNT(*) AS alerts FROM _SYS_STATISTICS.STATISTICS_CURRENT_ALERTS GROUP BY ALERT_RATING",
  254. orderedMetricLabels: []string{"alert_rating"},
  255. orderedStats: []queryStat{
  256. {
  257. key: "alerts",
  258. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  259. row map[string]string) error {
  260. return mb.RecordSaphanaAlertCountDataPoint(now, val, row["alert_rating"])
  261. },
  262. },
  263. },
  264. Enabled: func(c *Config) bool {
  265. return c.MetricsBuilderConfig.Metrics.SaphanaAlertCount.Enabled
  266. },
  267. },
  268. {
  269. query: "SELECT HOST, SUM(UPDATE_TRANSACTION_COUNT) updates, SUM(COMMIT_COUNT) commits, SUM(ROLLBACK_COUNT) rollbacks FROM SYS.M_WORKLOAD GROUP BY HOST",
  270. orderedResourceLabels: []string{"host"},
  271. orderedStats: []queryStat{
  272. {
  273. key: "updates",
  274. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  275. row map[string]string) error {
  276. return mb.RecordSaphanaTransactionCountDataPoint(now, val, metadata.AttributeTransactionTypeUpdate)
  277. },
  278. },
  279. {
  280. key: "commits",
  281. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  282. row map[string]string) error {
  283. return mb.RecordSaphanaTransactionCountDataPoint(now, val, metadata.AttributeTransactionTypeCommit)
  284. },
  285. },
  286. {
  287. key: "rollbacks",
  288. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  289. row map[string]string) error {
  290. return mb.RecordSaphanaTransactionCountDataPoint(now, val, metadata.AttributeTransactionTypeRollback)
  291. },
  292. },
  293. },
  294. Enabled: func(c *Config) bool {
  295. return c.MetricsBuilderConfig.Metrics.SaphanaTransactionCount.Enabled
  296. },
  297. },
  298. {
  299. query: "SELECT HOST, COUNT(*) blocks FROM SYS.M_BLOCKED_TRANSACTIONS GROUP BY HOST",
  300. orderedResourceLabels: []string{"host"},
  301. orderedStats: []queryStat{
  302. {
  303. key: "blocks",
  304. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  305. row map[string]string) error {
  306. return mb.RecordSaphanaTransactionBlockedDataPoint(now, val)
  307. },
  308. },
  309. },
  310. Enabled: func(c *Config) bool {
  311. return c.MetricsBuilderConfig.Metrics.SaphanaTransactionBlocked.Enabled
  312. },
  313. },
  314. {
  315. query: "SELECT HOST, \"PATH\", USAGE_TYPE, TOTAL_SIZE-USED_SIZE free_size, USED_SIZE FROM SYS.M_DISKS",
  316. orderedResourceLabels: []string{"host"},
  317. orderedMetricLabels: []string{"path", "usage_type"},
  318. orderedStats: []queryStat{
  319. {
  320. key: "free_size",
  321. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  322. row map[string]string) error {
  323. return mb.RecordSaphanaDiskSizeCurrentDataPoint(now, val, row["path"], row["usage_type"], metadata.AttributeDiskStateUsedFreeFree)
  324. },
  325. },
  326. {
  327. key: "used_size",
  328. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  329. row map[string]string) error {
  330. return mb.RecordSaphanaDiskSizeCurrentDataPoint(now, val, row["path"], row["usage_type"], metadata.AttributeDiskStateUsedFreeUsed)
  331. },
  332. },
  333. },
  334. Enabled: func(c *Config) bool {
  335. return c.MetricsBuilderConfig.Metrics.SaphanaDiskSizeCurrent.Enabled
  336. },
  337. },
  338. {
  339. query: "SELECT SYSTEM_ID, PRODUCT_NAME, PRODUCT_LIMIT, PRODUCT_USAGE, seconds_between(CURRENT_TIMESTAMP, EXPIRATION_DATE) expiration FROM SYS.M_LICENSES",
  340. orderedMetricLabels: []string{"system", "product"},
  341. orderedStats: []queryStat{
  342. {
  343. key: "limit",
  344. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  345. row map[string]string) error {
  346. return mb.RecordSaphanaLicenseLimitDataPoint(now, val, row["system"], row["product"])
  347. },
  348. },
  349. {
  350. key: "usage",
  351. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  352. row map[string]string) error {
  353. return mb.RecordSaphanaLicensePeakDataPoint(now, val, row["system"], row["product"])
  354. },
  355. },
  356. {
  357. key: "expiration",
  358. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  359. row map[string]string) error {
  360. return mb.RecordSaphanaLicenseExpirationTimeDataPoint(now, val, row["system"], row["product"])
  361. },
  362. },
  363. },
  364. Enabled: func(c *Config) bool {
  365. return c.MetricsBuilderConfig.Metrics.SaphanaLicenseExpirationTime.Enabled ||
  366. c.MetricsBuilderConfig.Metrics.SaphanaLicenseLimit.Enabled ||
  367. c.MetricsBuilderConfig.Metrics.SaphanaLicensePeak.Enabled
  368. },
  369. },
  370. {
  371. query: "SELECT HOST, PORT, SECONDARY_HOST, REPLICATION_MODE, BACKLOG_SIZE, BACKLOG_TIME, TO_VARCHAR(TO_DECIMAL(IFNULL(MAP(SHIPPED_LOG_BUFFERS_COUNT, 0, 0, SHIPPED_LOG_BUFFERS_DURATION / SHIPPED_LOG_BUFFERS_COUNT), 0), 10, 2)) avg_replication_time FROM SYS.M_SERVICE_REPLICATION",
  372. orderedMetricLabels: []string{"host", "port", "secondary", "mode"},
  373. orderedStats: []queryStat{
  374. {
  375. key: "backlog_size",
  376. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  377. row map[string]string) error {
  378. return mb.RecordSaphanaReplicationBacklogSizeDataPoint(now, val, row["host"], row["secondary"], row["port"], row["mode"])
  379. },
  380. },
  381. {
  382. key: "backlog_time",
  383. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  384. row map[string]string) error {
  385. return mb.RecordSaphanaReplicationBacklogTimeDataPoint(now, val, row["host"], row["secondary"], row["port"], row["mode"])
  386. },
  387. },
  388. {
  389. key: "average_time",
  390. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  391. row map[string]string) error {
  392. return mb.RecordSaphanaReplicationAverageTimeDataPoint(now, val, row["host"], row["secondary"], row["port"], row["mode"])
  393. },
  394. },
  395. },
  396. Enabled: func(c *Config) bool {
  397. return c.MetricsBuilderConfig.Metrics.SaphanaReplicationAverageTime.Enabled ||
  398. c.MetricsBuilderConfig.Metrics.SaphanaReplicationBacklogSize.Enabled ||
  399. c.MetricsBuilderConfig.Metrics.SaphanaReplicationBacklogTime.Enabled
  400. },
  401. },
  402. {
  403. query: "SELECT HOST, SUM(FINISHED_NON_INTERNAL_REQUEST_COUNT) \"external\", SUM(ALL_FINISHED_REQUEST_COUNT-FINISHED_NON_INTERNAL_REQUEST_COUNT) internal, SUM(ACTIVE_REQUEST_COUNT) active, SUM(PENDING_REQUEST_COUNT) pending, TO_VARCHAR(TO_DECIMAL(AVG(RESPONSE_TIME), 10, 2)) avg_time FROM SYS.M_SERVICE_STATISTICS WHERE ACTIVE_REQUEST_COUNT > -1 GROUP BY HOST",
  404. orderedResourceLabels: []string{"host"},
  405. orderedStats: []queryStat{
  406. {
  407. key: "external",
  408. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  409. row map[string]string) error {
  410. return mb.RecordSaphanaNetworkRequestFinishedCountDataPoint(now, val, metadata.AttributeInternalExternalRequestTypeExternal)
  411. },
  412. },
  413. {
  414. key: "internal",
  415. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  416. row map[string]string) error {
  417. return mb.RecordSaphanaNetworkRequestFinishedCountDataPoint(now, val, metadata.AttributeInternalExternalRequestTypeInternal)
  418. },
  419. },
  420. {
  421. key: "active",
  422. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  423. row map[string]string) error {
  424. return mb.RecordSaphanaNetworkRequestCountDataPoint(now, val, metadata.AttributeActivePendingRequestStateActive)
  425. },
  426. },
  427. {
  428. key: "pending",
  429. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  430. row map[string]string) error {
  431. return mb.RecordSaphanaNetworkRequestCountDataPoint(now, val, metadata.AttributeActivePendingRequestStatePending)
  432. },
  433. },
  434. {
  435. key: "avg_time",
  436. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  437. row map[string]string) error {
  438. return mb.RecordSaphanaNetworkRequestAverageTimeDataPoint(now, val)
  439. },
  440. },
  441. },
  442. Enabled: func(c *Config) bool {
  443. return c.MetricsBuilderConfig.Metrics.SaphanaNetworkRequestFinishedCount.Enabled ||
  444. c.MetricsBuilderConfig.Metrics.SaphanaNetworkRequestCount.Enabled ||
  445. c.MetricsBuilderConfig.Metrics.SaphanaNetworkRequestAverageTime.Enabled
  446. },
  447. },
  448. {
  449. query: "SELECT HOST, \"PATH\", \"TYPE\", SUM(TOTAL_READS) \"reads\", SUM(TOTAL_WRITES) writes, SUM(TOTAL_READ_SIZE) read_size, SUM(TOTAL_WRITE_SIZE) write_size, SUM(TOTAL_READ_TIME) read_time, SUM(TOTAL_WRITE_TIME) write_time FROM SYS.M_VOLUME_IO_TOTAL_STATISTICS GROUP BY HOST, \"PATH\", \"TYPE\"",
  450. orderedResourceLabels: []string{"host"},
  451. orderedMetricLabels: []string{"path", "type"},
  452. orderedStats: []queryStat{
  453. {
  454. key: "reads",
  455. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  456. row map[string]string) error {
  457. return mb.RecordSaphanaVolumeOperationCountDataPoint(now, val, row["path"], row["type"], metadata.AttributeVolumeOperationTypeRead)
  458. },
  459. },
  460. {
  461. key: "writes",
  462. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  463. row map[string]string) error {
  464. return mb.RecordSaphanaVolumeOperationCountDataPoint(now, val, row["path"], row["type"], metadata.AttributeVolumeOperationTypeWrite)
  465. },
  466. },
  467. {
  468. key: "read_size",
  469. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  470. row map[string]string) error {
  471. return mb.RecordSaphanaVolumeOperationSizeDataPoint(now, val, row["path"], row["type"], metadata.AttributeVolumeOperationTypeRead)
  472. },
  473. },
  474. {
  475. key: "write_size",
  476. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  477. row map[string]string) error {
  478. return mb.RecordSaphanaVolumeOperationSizeDataPoint(now, val, row["path"], row["type"], metadata.AttributeVolumeOperationTypeWrite)
  479. },
  480. },
  481. {
  482. key: "read_time",
  483. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  484. row map[string]string) error {
  485. return mb.RecordSaphanaVolumeOperationTimeDataPoint(now, val, row["path"], row["type"], metadata.AttributeVolumeOperationTypeRead)
  486. },
  487. },
  488. {
  489. key: "write_time",
  490. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  491. row map[string]string) error {
  492. return mb.RecordSaphanaVolumeOperationTimeDataPoint(now, val, row["path"], row["type"], metadata.AttributeVolumeOperationTypeWrite)
  493. },
  494. },
  495. },
  496. Enabled: func(c *Config) bool {
  497. return c.MetricsBuilderConfig.Metrics.SaphanaVolumeOperationCount.Enabled ||
  498. c.MetricsBuilderConfig.Metrics.SaphanaVolumeOperationSize.Enabled ||
  499. c.MetricsBuilderConfig.Metrics.SaphanaVolumeOperationTime.Enabled
  500. },
  501. },
  502. {
  503. query: "SELECT HOST, SERVICE_NAME, LOGICAL_MEMORY_SIZE, PHYSICAL_MEMORY_SIZE, CODE_SIZE, STACK_SIZE, HEAP_MEMORY_ALLOCATED_SIZE-HEAP_MEMORY_USED_SIZE heap_free, HEAP_MEMORY_USED_SIZE, SHARED_MEMORY_ALLOCATED_SIZE-SHARED_MEMORY_USED_SIZE shared_free, SHARED_MEMORY_USED_SIZE, COMPACTORS_ALLOCATED_SIZE, COMPACTORS_FREEABLE_SIZE, ALLOCATION_LIMIT, EFFECTIVE_ALLOCATION_LIMIT FROM SYS.M_SERVICE_MEMORY",
  504. orderedResourceLabels: []string{"host"},
  505. orderedMetricLabels: []string{"service"},
  506. orderedStats: []queryStat{
  507. {
  508. key: "logical_used",
  509. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  510. row map[string]string) error {
  511. return mb.RecordSaphanaServiceMemoryUsedDataPoint(now, val, row["service"], metadata.AttributeServiceMemoryUsedTypeLogical)
  512. },
  513. },
  514. {
  515. key: "physical_used",
  516. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  517. row map[string]string) error {
  518. return mb.RecordSaphanaServiceMemoryUsedDataPoint(now, val, row["service"], metadata.AttributeServiceMemoryUsedTypePhysical)
  519. },
  520. },
  521. {
  522. key: "code_size",
  523. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  524. row map[string]string) error {
  525. return mb.RecordSaphanaServiceCodeSizeDataPoint(now, val, row["service"])
  526. },
  527. },
  528. {
  529. key: "stack_size",
  530. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  531. row map[string]string) error {
  532. return mb.RecordSaphanaServiceStackSizeDataPoint(now, val, row["service"])
  533. },
  534. },
  535. {
  536. key: "heap_free",
  537. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  538. row map[string]string) error {
  539. return mb.RecordSaphanaServiceMemoryHeapCurrentDataPoint(now, val, row["service"], metadata.AttributeMemoryStateUsedFreeFree)
  540. },
  541. },
  542. {
  543. key: "heap_used",
  544. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  545. row map[string]string) error {
  546. return mb.RecordSaphanaServiceMemoryHeapCurrentDataPoint(now, val, row["service"], metadata.AttributeMemoryStateUsedFreeUsed)
  547. },
  548. },
  549. {
  550. key: "shared_free",
  551. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  552. row map[string]string) error {
  553. return mb.RecordSaphanaServiceMemorySharedCurrentDataPoint(now, val, row["service"], metadata.AttributeMemoryStateUsedFreeFree)
  554. },
  555. },
  556. {
  557. key: "shared_used",
  558. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  559. row map[string]string) error {
  560. return mb.RecordSaphanaServiceMemorySharedCurrentDataPoint(now, val, row["service"], metadata.AttributeMemoryStateUsedFreeUsed)
  561. },
  562. },
  563. {
  564. key: "compactors_allocated",
  565. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  566. row map[string]string) error {
  567. return mb.RecordSaphanaServiceMemoryCompactorsAllocatedDataPoint(now, val, row["service"])
  568. },
  569. },
  570. {
  571. key: "compactors_freeable",
  572. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  573. row map[string]string) error {
  574. return mb.RecordSaphanaServiceMemoryCompactorsFreeableDataPoint(now, val, row["service"])
  575. },
  576. },
  577. {
  578. key: "allocation_limit",
  579. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  580. row map[string]string) error {
  581. return mb.RecordSaphanaServiceMemoryLimitDataPoint(now, val, row["service"])
  582. },
  583. },
  584. {
  585. key: "effective_limit",
  586. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  587. row map[string]string) error {
  588. return mb.RecordSaphanaServiceMemoryEffectiveLimitDataPoint(now, val, row["service"])
  589. },
  590. },
  591. },
  592. Enabled: func(c *Config) bool {
  593. return c.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryUsed.Enabled ||
  594. c.MetricsBuilderConfig.Metrics.SaphanaServiceCodeSize.Enabled ||
  595. c.MetricsBuilderConfig.Metrics.SaphanaServiceStackSize.Enabled ||
  596. c.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryHeapCurrent.Enabled ||
  597. c.MetricsBuilderConfig.Metrics.SaphanaServiceMemorySharedCurrent.Enabled ||
  598. c.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryCompactorsAllocated.Enabled ||
  599. c.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryCompactorsFreeable.Enabled ||
  600. c.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryLimit.Enabled ||
  601. c.MetricsBuilderConfig.Metrics.SaphanaServiceMemoryEffectiveLimit.Enabled
  602. },
  603. },
  604. {
  605. query: "SELECT HOST, SCHEMA_NAME, SUM(ESTIMATED_MAX_MEMORY_SIZE_IN_TOTAL) estimated_max, SUM(LAST_COMPRESSED_RECORD_COUNT) last_compressed, SUM(READ_COUNT) \"reads\", SUM(WRITE_COUNT) writes, SUM(MERGE_COUNT) merges, SUM(MEMORY_SIZE_IN_MAIN) mem_main, SUM(MEMORY_SIZE_IN_DELTA) mem_delta, SUM(MEMORY_SIZE_IN_HISTORY_MAIN) mem_hist_main, SUM(MEMORY_SIZE_IN_HISTORY_DELTA) mem_hist_delta, SUM(RAW_RECORD_COUNT_IN_MAIN) records_main, SUM(RAW_RECORD_COUNT_IN_DELTA) records_delta, SUM(RAW_RECORD_COUNT_IN_HISTORY_MAIN) records_hist_main, SUM(RAW_RECORD_COUNT_IN_HISTORY_DELTA) records_hist_delta FROM SYS.M_CS_TABLES GROUP BY HOST, SCHEMA_NAME",
  606. orderedResourceLabels: []string{"host"},
  607. orderedMetricLabels: []string{"schema"},
  608. orderedStats: []queryStat{
  609. {
  610. key: "estimated_max",
  611. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  612. row map[string]string) error {
  613. return mb.RecordSaphanaSchemaMemoryUsedMaxDataPoint(now, val, row["schema"])
  614. },
  615. },
  616. {
  617. key: "last_compressed",
  618. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  619. row map[string]string) error {
  620. return mb.RecordSaphanaSchemaRecordCompressedCountDataPoint(now, val, row["schema"])
  621. },
  622. },
  623. {
  624. key: "reads",
  625. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  626. row map[string]string) error {
  627. return mb.RecordSaphanaSchemaOperationCountDataPoint(now, val, row["schema"], metadata.AttributeSchemaOperationTypeRead)
  628. },
  629. },
  630. {
  631. key: "writes",
  632. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  633. row map[string]string) error {
  634. return mb.RecordSaphanaSchemaOperationCountDataPoint(now, val, row["schema"], metadata.AttributeSchemaOperationTypeWrite)
  635. },
  636. },
  637. {
  638. key: "merges",
  639. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  640. row map[string]string) error {
  641. return mb.RecordSaphanaSchemaOperationCountDataPoint(now, val, row["schema"], metadata.AttributeSchemaOperationTypeMerge)
  642. },
  643. },
  644. {
  645. key: "mem_main",
  646. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  647. row map[string]string) error {
  648. return mb.RecordSaphanaSchemaMemoryUsedCurrentDataPoint(now, val, row["schema"], metadata.AttributeSchemaMemoryTypeMain)
  649. },
  650. },
  651. {
  652. key: "mem_delta",
  653. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  654. row map[string]string) error {
  655. return mb.RecordSaphanaSchemaMemoryUsedCurrentDataPoint(now, val, row["schema"], metadata.AttributeSchemaMemoryTypeDelta)
  656. },
  657. },
  658. {
  659. key: "mem_history_main",
  660. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  661. row map[string]string) error {
  662. return mb.RecordSaphanaSchemaMemoryUsedCurrentDataPoint(now, val, row["schema"], metadata.AttributeSchemaMemoryTypeHistoryMain)
  663. },
  664. },
  665. {
  666. key: "mem_history_delta",
  667. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  668. row map[string]string) error {
  669. return mb.RecordSaphanaSchemaMemoryUsedCurrentDataPoint(now, val, row["schema"], metadata.AttributeSchemaMemoryTypeHistoryDelta)
  670. },
  671. },
  672. {
  673. key: "records_main",
  674. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  675. row map[string]string) error {
  676. return mb.RecordSaphanaSchemaRecordCountDataPoint(now, val, row["schema"], metadata.AttributeSchemaRecordTypeMain)
  677. },
  678. },
  679. {
  680. key: "records_delta",
  681. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  682. row map[string]string) error {
  683. return mb.RecordSaphanaSchemaRecordCountDataPoint(now, val, row["schema"], metadata.AttributeSchemaRecordTypeDelta)
  684. },
  685. },
  686. {
  687. key: "records_history_main",
  688. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  689. row map[string]string) error {
  690. return mb.RecordSaphanaSchemaRecordCountDataPoint(now, val, row["schema"], metadata.AttributeSchemaRecordTypeHistoryMain)
  691. },
  692. },
  693. {
  694. key: "records_history_delta",
  695. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  696. row map[string]string) error {
  697. return mb.RecordSaphanaSchemaRecordCountDataPoint(now, val, row["schema"], metadata.AttributeSchemaRecordTypeHistoryDelta)
  698. },
  699. },
  700. },
  701. Enabled: func(c *Config) bool {
  702. return c.MetricsBuilderConfig.Metrics.SaphanaSchemaMemoryUsedMax.Enabled ||
  703. c.MetricsBuilderConfig.Metrics.SaphanaSchemaRecordCompressedCount.Enabled ||
  704. c.MetricsBuilderConfig.Metrics.SaphanaSchemaOperationCount.Enabled ||
  705. c.MetricsBuilderConfig.Metrics.SaphanaSchemaMemoryUsedCurrent.Enabled ||
  706. c.MetricsBuilderConfig.Metrics.SaphanaSchemaRecordCount.Enabled
  707. },
  708. },
  709. {
  710. query: "SELECT HOST, FREE_PHYSICAL_MEMORY, USED_PHYSICAL_MEMORY, FREE_SWAP_SPACE, USED_SWAP_SPACE, INSTANCE_TOTAL_MEMORY_USED_SIZE, INSTANCE_TOTAL_MEMORY_PEAK_USED_SIZE, INSTANCE_TOTAL_MEMORY_ALLOCATED_SIZE-INSTANCE_TOTAL_MEMORY_USED_SIZE total_free, INSTANCE_CODE_SIZE, INSTANCE_SHARED_MEMORY_ALLOCATED_SIZE, TOTAL_CPU_USER_TIME, TOTAL_CPU_SYSTEM_TIME, TOTAL_CPU_WIO_TIME, TOTAL_CPU_IDLE_TIME FROM SYS.M_HOST_RESOURCE_UTILIZATION",
  711. orderedResourceLabels: []string{"host"},
  712. orderedStats: []queryStat{
  713. {
  714. key: "free_physical_memory",
  715. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  716. row map[string]string) error {
  717. return mb.RecordSaphanaHostMemoryCurrentDataPoint(now, val, metadata.AttributeMemoryStateUsedFreeFree)
  718. },
  719. },
  720. {
  721. key: "used_physical_memory",
  722. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  723. row map[string]string) error {
  724. return mb.RecordSaphanaHostMemoryCurrentDataPoint(now, val, metadata.AttributeMemoryStateUsedFreeUsed)
  725. },
  726. },
  727. {
  728. key: "free_swap_space",
  729. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  730. row map[string]string) error {
  731. return mb.RecordSaphanaHostSwapCurrentDataPoint(now, val, metadata.AttributeHostSwapStateFree)
  732. },
  733. },
  734. {
  735. key: "used_swap_space",
  736. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  737. row map[string]string) error {
  738. return mb.RecordSaphanaHostSwapCurrentDataPoint(now, val, metadata.AttributeHostSwapStateUsed)
  739. },
  740. },
  741. {
  742. key: "instance_total_used",
  743. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  744. row map[string]string) error {
  745. return mb.RecordSaphanaInstanceMemoryCurrentDataPoint(now, val, metadata.AttributeMemoryStateUsedFreeUsed)
  746. },
  747. },
  748. {
  749. key: "instance_total_used_peak",
  750. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  751. row map[string]string) error {
  752. return mb.RecordSaphanaInstanceMemoryUsedPeakDataPoint(now, val)
  753. },
  754. },
  755. {
  756. key: "instance_total_free",
  757. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  758. row map[string]string) error {
  759. return mb.RecordSaphanaInstanceMemoryCurrentDataPoint(now, val, metadata.AttributeMemoryStateUsedFreeFree)
  760. },
  761. },
  762. {
  763. key: "instance_code_size",
  764. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  765. row map[string]string) error {
  766. return mb.RecordSaphanaInstanceCodeSizeDataPoint(now, val)
  767. },
  768. },
  769. {
  770. key: "instance_shared_memory_allocated",
  771. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  772. row map[string]string) error {
  773. return mb.RecordSaphanaInstanceMemorySharedAllocatedDataPoint(now, val)
  774. },
  775. },
  776. {
  777. key: "cpu_user",
  778. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  779. row map[string]string) error {
  780. return mb.RecordSaphanaCPUUsedDataPoint(now, val, metadata.AttributeCPUTypeUser)
  781. },
  782. },
  783. {
  784. key: "cpu_system",
  785. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  786. row map[string]string) error {
  787. return mb.RecordSaphanaCPUUsedDataPoint(now, val, metadata.AttributeCPUTypeSystem)
  788. },
  789. },
  790. {
  791. key: "cpu_io_wait",
  792. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  793. row map[string]string) error {
  794. return mb.RecordSaphanaCPUUsedDataPoint(now, val, metadata.AttributeCPUTypeIoWait)
  795. },
  796. },
  797. {
  798. key: "cpu_idle",
  799. addMetricFunction: func(mb *metadata.MetricsBuilder, now pcommon.Timestamp, val string,
  800. row map[string]string) error {
  801. return mb.RecordSaphanaCPUUsedDataPoint(now, val, metadata.AttributeCPUTypeIdle)
  802. },
  803. },
  804. },
  805. Enabled: func(c *Config) bool {
  806. return c.MetricsBuilderConfig.Metrics.SaphanaHostMemoryCurrent.Enabled ||
  807. c.MetricsBuilderConfig.Metrics.SaphanaHostSwapCurrent.Enabled ||
  808. c.MetricsBuilderConfig.Metrics.SaphanaInstanceMemoryCurrent.Enabled ||
  809. c.MetricsBuilderConfig.Metrics.SaphanaInstanceMemoryUsedPeak.Enabled ||
  810. c.MetricsBuilderConfig.Metrics.SaphanaInstanceCodeSize.Enabled ||
  811. c.MetricsBuilderConfig.Metrics.SaphanaInstanceMemorySharedAllocated.Enabled ||
  812. c.MetricsBuilderConfig.Metrics.SaphanaCPUUsed.Enabled
  813. },
  814. },
  815. }
  816. func (m *monitoringQuery) CollectMetrics(ctx context.Context, s *sapHanaScraper, client client, now pcommon.Timestamp,
  817. errs *scrapererror.ScrapeErrors) {
  818. rows, err := client.collectDataFromQuery(ctx, m)
  819. if err != nil {
  820. errs.AddPartial(len(m.orderedStats), fmt.Errorf("error running query '%s': %w", m.query, err))
  821. return
  822. }
  823. for _, data := range rows {
  824. for _, stat := range m.orderedStats {
  825. if err := stat.collectStat(s, m, now, data); err != nil {
  826. errs.AddPartial(1, err)
  827. }
  828. }
  829. }
  830. }