gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. local grafana = import 'github.com/grafana/grafonnet-lib/grafonnet/grafana.libsonnet';
  2. local prometheus = grafana.prometheus;
  3. local addIssueLabelsOverrides(labels) =
  4. {
  5. fieldConfig+: {
  6. overrides+: [
  7. {
  8. matcher: {
  9. id: 'byRegexp',
  10. options: label.label,
  11. },
  12. properties: [
  13. {
  14. id: 'color',
  15. value: {
  16. fixedColor: label.color,
  17. mode: 'fixed',
  18. },
  19. },
  20. ],
  21. }
  22. for label in labels
  23. ],
  24. },
  25. };
  26. {
  27. grafanaDashboards+:: {
  28. local giteaSelector = 'job=~"$job", instance=~"$instance"',
  29. local giteaStatsPanel =
  30. grafana.statPanel.new(
  31. 'Gitea stats',
  32. datasource='$datasource',
  33. reducerFunction='lastNotNull',
  34. graphMode='none',
  35. colorMode='value',
  36. )
  37. .addTargets(
  38. [
  39. prometheus.target(expr='%s{%s}' % [metric.name, giteaSelector], legendFormat=metric.description, intervalFactor=10)
  40. for metric in $._config.giteaStatMetrics
  41. ]
  42. )
  43. + {
  44. fieldConfig+: {
  45. defaults+: {
  46. color: {
  47. fixedColor: 'blue',
  48. mode: 'fixed',
  49. },
  50. },
  51. },
  52. },
  53. local giteaUptimePanel =
  54. grafana.statPanel.new(
  55. 'Uptime',
  56. datasource='$datasource',
  57. reducerFunction='last',
  58. graphMode='area',
  59. colorMode='value',
  60. )
  61. .addTarget(prometheus.target(expr='time()-process_start_time_seconds{%s}' % giteaSelector, intervalFactor=1))
  62. + {
  63. fieldConfig+: {
  64. defaults+: {
  65. color: {
  66. fixedColor: 'blue',
  67. mode: 'fixed',
  68. },
  69. unit: 's',
  70. },
  71. },
  72. },
  73. local giteaMemoryPanel =
  74. grafana.graphPanel.new(
  75. 'Memory usage',
  76. datasource='$datasource'
  77. )
  78. .addTarget(prometheus.target(expr='process_resident_memory_bytes{%s}' % giteaSelector, intervalFactor=2))
  79. + {
  80. type: 'timeseries',
  81. options+: {
  82. tooltip: {
  83. mode: 'multi',
  84. },
  85. legend+: {
  86. displayMode: 'hidden',
  87. },
  88. },
  89. fieldConfig+: {
  90. defaults+: {
  91. custom+: {
  92. lineInterpolation: 'smooth',
  93. fillOpacity: 15,
  94. },
  95. color: {
  96. fixedColor: 'green',
  97. mode: 'fixed',
  98. },
  99. unit: 'decbytes',
  100. },
  101. },
  102. },
  103. local giteaCpuPanel =
  104. grafana.graphPanel.new(
  105. 'CPU usage',
  106. datasource='$datasource'
  107. )
  108. .addTarget(prometheus.target(expr='rate(process_cpu_seconds_total{%s}[$__rate_interval])*100' % giteaSelector, intervalFactor=2))
  109. + {
  110. type: 'timeseries',
  111. options+: {
  112. tooltip: {
  113. mode: 'multi',
  114. },
  115. legend+: {
  116. displayMode: 'hidden',
  117. },
  118. },
  119. fieldConfig+: {
  120. defaults+: {
  121. custom+: {
  122. lineInterpolation: 'smooth',
  123. gradientMode: 'scheme',
  124. fillOpacity: 15,
  125. axisSoftMin: 0,
  126. axisSoftMax: 0,
  127. },
  128. color: {
  129. mode: 'continuous-GrYlRd', // from green to red (100%)
  130. },
  131. unit: 'percent',
  132. },
  133. overrides: [
  134. {
  135. matcher: {
  136. id: 'byRegexp',
  137. options: '.+',
  138. },
  139. properties: [
  140. {
  141. id: 'max',
  142. value: 100,
  143. },
  144. {
  145. id: 'min',
  146. value: 0,
  147. },
  148. ],
  149. },
  150. ],
  151. },
  152. },
  153. local giteaFileDescriptorsPanel =
  154. grafana.graphPanel.new(
  155. 'File descriptors usage',
  156. datasource='$datasource',
  157. )
  158. .addTarget(prometheus.target(expr='process_open_fds{%s}' % giteaSelector, intervalFactor=2))
  159. .addTarget(prometheus.target(expr='process_max_fds{%s}' % giteaSelector, intervalFactor=2))
  160. .addSeriesOverride(
  161. {
  162. alias: '/process_max_fds.+/',
  163. color: '#F2495C', // red
  164. dashes: true,
  165. fill: 0,
  166. },
  167. )
  168. + {
  169. type: 'timeseries',
  170. options+: {
  171. tooltip: {
  172. mode: 'multi',
  173. },
  174. legend+: {
  175. displayMode: 'hidden',
  176. },
  177. },
  178. fieldConfig+: {
  179. defaults+: {
  180. custom+: {
  181. lineInterpolation: 'smooth',
  182. gradientMode: 'scheme',
  183. fillOpacity: 0,
  184. },
  185. color: {
  186. fixedColor: 'green',
  187. mode: 'fixed',
  188. },
  189. unit: '',
  190. },
  191. overrides: [
  192. {
  193. matcher: {
  194. id: 'byFrameRefID',
  195. options: 'B',
  196. },
  197. properties: [
  198. {
  199. id: 'custom.lineStyle',
  200. value: {
  201. fill: 'dash',
  202. dash: [
  203. 10,
  204. 10,
  205. ],
  206. },
  207. },
  208. {
  209. id: 'color',
  210. value: {
  211. mode: 'fixed',
  212. fixedColor: 'red',
  213. },
  214. },
  215. ],
  216. },
  217. ],
  218. },
  219. },
  220. local giteaChangesPanelPrototype =
  221. grafana.graphPanel.new(
  222. '',
  223. datasource='$datasource',
  224. interval='$agg_interval',
  225. maxDataPoints=10000,
  226. )
  227. + {
  228. type: 'timeseries',
  229. options+: {
  230. tooltip: {
  231. mode: 'multi',
  232. },
  233. legend+: {
  234. calcs+: [
  235. 'sum',
  236. ],
  237. },
  238. },
  239. fieldConfig+: {
  240. defaults+: {
  241. noValue: '0',
  242. custom+: {
  243. drawStyle: 'bars',
  244. barAlignment: -1,
  245. fillOpacity: 50,
  246. gradientMode: 'hue',
  247. pointSize: 1,
  248. lineWidth: 0,
  249. stacking: {
  250. group: 'A',
  251. mode: 'normal',
  252. },
  253. },
  254. },
  255. },
  256. },
  257. local giteaChangesPanelAll =
  258. giteaChangesPanelPrototype
  259. .addTarget(prometheus.target(expr='changes(process_start_time_seconds{%s}[$__interval]) > 0' % [giteaSelector], legendFormat='Restarts', intervalFactor=1))
  260. .addTargets(
  261. [
  262. prometheus.target(expr='floor(delta(%s{%s}[$__interval])) > 0' % [metric.name, giteaSelector], legendFormat=metric.description, intervalFactor=1)
  263. for metric in $._config.giteaStatMetrics
  264. ]
  265. ) + { id: 200 }, // some unique number, beyond the maximum number of panels in the dashboard,
  266. local giteaChangesPanelTotal =
  267. grafana.statPanel.new(
  268. 'Changes',
  269. datasource='-- Dashboard --',
  270. reducerFunction='sum',
  271. graphMode='none',
  272. textMode='value_and_name',
  273. colorMode='value',
  274. )
  275. + {
  276. targets+: [
  277. {
  278. panelId: giteaChangesPanelAll.id,
  279. refId: 'A',
  280. },
  281. ],
  282. }
  283. + {
  284. fieldConfig+: {
  285. defaults+: {
  286. color: {
  287. mode: 'palette-classic',
  288. },
  289. },
  290. },
  291. },
  292. local giteaChangesByRepositories =
  293. giteaChangesPanelPrototype
  294. .addTarget(prometheus.target(expr='floor(increase(gitea_issues_by_repository{%s}[$__interval])) > 0' % [giteaSelector], legendFormat='{{ repository }}', intervalFactor=1))
  295. + { id: 210 }, // some unique number, beyond the maximum number of panels in the dashboard,
  296. local giteaChangesByRepositoriesTotal =
  297. grafana.statPanel.new(
  298. 'Issues by repository',
  299. datasource='-- Dashboard --',
  300. reducerFunction='sum',
  301. graphMode='none',
  302. textMode='value_and_name',
  303. colorMode='value',
  304. )
  305. + {
  306. id: 211,
  307. targets+: [
  308. {
  309. panelId: giteaChangesByRepositories.id,
  310. refId: 'A',
  311. },
  312. ],
  313. }
  314. + {
  315. fieldConfig+: {
  316. defaults+: {
  317. color: {
  318. mode: 'palette-classic',
  319. },
  320. },
  321. },
  322. },
  323. local giteaChangesByLabel =
  324. giteaChangesPanelPrototype
  325. .addTarget(prometheus.target(expr='floor(increase(gitea_issues_by_label{%s}[$__interval])) > 0' % [giteaSelector], legendFormat='{{ label }}', intervalFactor=1))
  326. + addIssueLabelsOverrides($._config.issueLabels)
  327. + { id: 220 }, // some unique number, beyond the maximum number of panels in the dashboard,
  328. local giteaChangesByLabelTotal =
  329. grafana.statPanel.new(
  330. 'Issues by labels',
  331. datasource='-- Dashboard --',
  332. reducerFunction='sum',
  333. graphMode='none',
  334. textMode='value_and_name',
  335. colorMode='value',
  336. )
  337. + addIssueLabelsOverrides($._config.issueLabels)
  338. + {
  339. id: 221,
  340. targets+: [
  341. {
  342. panelId: giteaChangesByLabel.id,
  343. refId: 'A',
  344. },
  345. ],
  346. }
  347. + {
  348. fieldConfig+: {
  349. defaults+: {
  350. color: {
  351. mode: 'palette-classic',
  352. },
  353. },
  354. },
  355. },
  356. 'gitea-overview.json':
  357. grafana.dashboard.new(
  358. '%s Overview' % $._config.dashboardNamePrefix,
  359. time_from='%s' % $._config.dashboardPeriod,
  360. editable=false,
  361. tags=($._config.dashboardTags),
  362. timezone='%s' % $._config.dashboardTimezone,
  363. refresh='%s' % $._config.dashboardRefresh,
  364. graphTooltip='shared_crosshair',
  365. uid='gitea-overview'
  366. )
  367. .addTemplate(
  368. {
  369. current: {
  370. text: 'Prometheus',
  371. value: 'Prometheus',
  372. },
  373. hide: 0,
  374. label: 'Data Source',
  375. name: 'datasource',
  376. options: [],
  377. query: 'prometheus',
  378. refresh: 1,
  379. regex: '',
  380. type: 'datasource',
  381. },
  382. )
  383. .addTemplate(
  384. {
  385. hide: 0,
  386. label: 'job',
  387. name: 'job',
  388. options: [],
  389. datasource: '$datasource',
  390. query: 'label_values(gitea_organizations, job)',
  391. refresh: 1,
  392. regex: '',
  393. type: 'query',
  394. multi: true,
  395. allValue: '.+'
  396. },
  397. )
  398. .addTemplate(
  399. {
  400. hide: 0,
  401. label: 'instance',
  402. name: 'instance',
  403. options: [],
  404. datasource: '$datasource',
  405. query: 'label_values(gitea_organizations{job="$job"}, instance)',
  406. refresh: 1,
  407. regex: '',
  408. type: 'query',
  409. multi: true,
  410. allValue: '.+'
  411. },
  412. )
  413. .addTemplate(
  414. {
  415. hide: 0,
  416. label: 'aggregation interval',
  417. name: 'agg_interval',
  418. auto_min: '1m',
  419. auto: true,
  420. query: '1m,10m,1h,1d,7d',
  421. type: 'interval',
  422. },
  423. )
  424. .addPanel(grafana.row.new(title='General'), gridPos={ x: 0, y: 0, w: 0, h: 0 },)
  425. .addPanel(giteaStatsPanel, gridPos={ x: 0, y: 0, w: 16, h: 4 })
  426. .addPanel(giteaUptimePanel, gridPos={ x: 16, y: 0, w: 8, h: 4 })
  427. .addPanel(giteaMemoryPanel, gridPos={ x: 0, y: 4, w: 8, h: 6 })
  428. .addPanel(giteaCpuPanel, gridPos={ x: 8, y: 4, w: 8, h: 6 })
  429. .addPanel(giteaFileDescriptorsPanel, gridPos={ x: 16, y: 4, w: 8, h: 6 })
  430. .addPanel(grafana.row.new(title='Changes', collapse=false), gridPos={ x: 0, y: 10, w: 24, h: 8 })
  431. .addPanel(giteaChangesPanelTotal, gridPos={ x: 0, y: 12, w: 6, h: 8 })
  432. + // use patching instead of .addPanel() to keep static ids
  433. {
  434. panels+: std.flattenArrays([
  435. [
  436. giteaChangesPanelAll { gridPos: { x: 6, y: 12, w: 18, h: 8 } },
  437. ],
  438. if $._config.showIssuesByRepository then
  439. [
  440. giteaChangesByRepositoriesTotal { gridPos: { x: 0, y: 20, w: 6, h: 8 } },
  441. giteaChangesByRepositories { gridPos: { x: 6, y: 20, w: 18, h: 8 } },
  442. ] else [],
  443. if $._config.showIssuesByLabel then
  444. [
  445. giteaChangesByLabelTotal { gridPos: { x: 0, y: 28, w: 6, h: 8 } },
  446. giteaChangesByLabel { gridPos: { x: 6, y: 28, w: 18, h: 8 } },
  447. ] else [],
  448. ]),
  449. },
  450. },
  451. }