metrics.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2022 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package iptables
  15. import (
  16. "github.com/prometheus/client_golang/prometheus"
  17. )
  18. type metricsClientWrapper struct {
  19. client Client
  20. operationCounter *prometheus.CounterVec
  21. }
  22. func wrapWithMetrics(client Client, protocol string, registerer prometheus.Registerer) Client {
  23. if registerer == nil {
  24. return client
  25. }
  26. labelNames := []string{
  27. "operation",
  28. "table",
  29. "chain",
  30. }
  31. counter := prometheus.NewCounterVec(prometheus.CounterOpts{
  32. Name: "kilo_iptables_operations_total",
  33. Help: "Number of iptables operations.",
  34. ConstLabels: prometheus.Labels{"protocol": protocol},
  35. }, labelNames)
  36. registerer.MustRegister(counter)
  37. return &metricsClientWrapper{client, counter}
  38. }
  39. func (m *metricsClientWrapper) AppendUnique(table string, chain string, rule ...string) error {
  40. m.operationCounter.With(prometheus.Labels{
  41. "operation": "AppendUnique",
  42. "table": table,
  43. "chain": chain,
  44. }).Inc()
  45. return m.client.AppendUnique(table, chain, rule...)
  46. }
  47. func (m *metricsClientWrapper) InsertUnique(table, chain string, pos int, rule ...string) error {
  48. m.operationCounter.With(prometheus.Labels{
  49. "operation": "InsertUnique",
  50. "table": table,
  51. "chain": chain,
  52. }).Inc()
  53. return m.client.InsertUnique(table, chain, pos, rule...)
  54. }
  55. func (m *metricsClientWrapper) Delete(table string, chain string, rule ...string) error {
  56. m.operationCounter.With(prometheus.Labels{
  57. "operation": "Delete",
  58. "table": table,
  59. "chain": chain,
  60. }).Inc()
  61. return m.client.Delete(table, chain, rule...)
  62. }
  63. func (m *metricsClientWrapper) Exists(table string, chain string, rule ...string) (bool, error) {
  64. m.operationCounter.With(prometheus.Labels{
  65. "operation": "Exists",
  66. "table": table,
  67. "chain": chain,
  68. }).Inc()
  69. return m.client.Exists(table, chain, rule...)
  70. }
  71. func (m *metricsClientWrapper) List(table string, chain string) ([]string, error) {
  72. m.operationCounter.With(prometheus.Labels{
  73. "operation": "List",
  74. "table": table,
  75. "chain": chain,
  76. }).Inc()
  77. return m.client.List(table, chain)
  78. }
  79. func (m *metricsClientWrapper) ClearChain(table string, chain string) error {
  80. m.operationCounter.With(prometheus.Labels{
  81. "operation": "ClearChain",
  82. "table": table,
  83. "chain": chain,
  84. }).Inc()
  85. return m.client.ClearChain(table, chain)
  86. }
  87. func (m *metricsClientWrapper) DeleteChain(table string, chain string) error {
  88. m.operationCounter.With(prometheus.Labels{
  89. "operation": "DeleteChain",
  90. "table": table,
  91. "chain": chain,
  92. }).Inc()
  93. return m.client.DeleteChain(table, chain)
  94. }
  95. func (m *metricsClientWrapper) NewChain(table string, chain string) error {
  96. m.operationCounter.With(prometheus.Labels{
  97. "operation": "NewChain",
  98. "table": table,
  99. "chain": chain,
  100. }).Inc()
  101. return m.client.NewChain(table, chain)
  102. }
  103. func (m *metricsClientWrapper) ListChains(table string) ([]string, error) {
  104. m.operationCounter.With(prometheus.Labels{
  105. "operation": "ListChains",
  106. "table": table,
  107. "chain": "*",
  108. }).Inc()
  109. return m.client.ListChains(table)
  110. }