-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add metric prefix config and expose metricCollectors (#106)
- Loading branch information
A.Samet İleri
authored
Feb 18, 2024
1 parent
bce9002
commit a73ddbe
Showing
16 changed files
with
335 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package kafka | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
func Test_NewCollector(t *testing.T) { | ||
t.Run("When_Default_Prefix_Value_Used", func(t *testing.T) { | ||
cronsumerMetric := &ConsumerMetric{} | ||
expectedTotalProcessedMessagesCounter := prometheus.NewDesc( | ||
prometheus.BuildFQName(Name, "processed_messages_total", "current"), | ||
"Total number of processed messages.", | ||
emptyStringList, | ||
nil, | ||
) | ||
expectedTotalUnprocessedMessagesCounter := prometheus.NewDesc( | ||
prometheus.BuildFQName(Name, "unprocessed_messages_total", "current"), | ||
"Total number of unprocessed messages.", | ||
emptyStringList, | ||
nil, | ||
) | ||
|
||
collector := NewMetricCollector("", cronsumerMetric) | ||
|
||
if !reflect.DeepEqual(collector.totalProcessedMessagesCounter, expectedTotalProcessedMessagesCounter) { | ||
t.Errorf("Expected: %+v, Actual: %+v", collector.totalProcessedMessagesCounter, expectedTotalProcessedMessagesCounter) | ||
} | ||
if !reflect.DeepEqual(collector.totalUnprocessedMessagesCounter, expectedTotalUnprocessedMessagesCounter) { | ||
t.Errorf("Expected: %+v, Actual: %+v", collector.totalUnprocessedMessagesCounter, expectedTotalUnprocessedMessagesCounter) | ||
} | ||
}) | ||
t.Run("When_Custom_Prefix_Value_Used", func(t *testing.T) { | ||
cronsumerMetric := &ConsumerMetric{} | ||
expectedTotalProcessedMessagesCounter := prometheus.NewDesc( | ||
prometheus.BuildFQName("custom_prefix", "processed_messages_total", "current"), | ||
"Total number of processed messages.", | ||
emptyStringList, | ||
nil, | ||
) | ||
expectedTotalUnprocessedMessagesCounter := prometheus.NewDesc( | ||
prometheus.BuildFQName("custom_prefix", "unprocessed_messages_total", "current"), | ||
"Total number of unprocessed messages.", | ||
emptyStringList, | ||
nil, | ||
) | ||
|
||
collector := NewMetricCollector("custom_prefix", cronsumerMetric) | ||
|
||
if !reflect.DeepEqual(collector.totalProcessedMessagesCounter, expectedTotalProcessedMessagesCounter) { | ||
t.Errorf("Expected: %+v, Actual: %+v", collector.totalProcessedMessagesCounter, expectedTotalProcessedMessagesCounter) | ||
} | ||
if !reflect.DeepEqual(collector.totalUnprocessedMessagesCounter, expectedTotalUnprocessedMessagesCounter) { | ||
t.Errorf("Expected: %+v, Actual: %+v", collector.totalUnprocessedMessagesCounter, expectedTotalUnprocessedMessagesCounter) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
If you run this example and go to http://localhost:8000/metrics, | ||
|
||
you can see first and second consumers metrics as shown below | ||
|
||
``` | ||
# HELP first_discarded_messages_total_current Total number of discarded messages. | ||
# TYPE first_discarded_messages_total_current counter | ||
first_discarded_messages_total_current 0 | ||
# HELP first_processed_messages_total_current Total number of processed messages. | ||
# TYPE first_processed_messages_total_current counter | ||
first_processed_messages_total_current 0 | ||
# HELP first_retried_messages_total_current Total number of retried messages. | ||
# TYPE first_retried_messages_total_current counter | ||
first_retried_messages_total_current 0 | ||
# HELP first_unprocessed_messages_total_current Total number of unprocessed messages. | ||
# TYPE first_unprocessed_messages_total_current counter | ||
first_unprocessed_messages_total_current 0 | ||
# HELP second_discarded_messages_total_current Total number of discarded messages. | ||
# TYPE second_discarded_messages_total_current counter | ||
second_discarded_messages_total_current 0 | ||
# HELP second_processed_messages_total_current Total number of processed messages. | ||
# TYPE second_processed_messages_total_current counter | ||
second_processed_messages_total_current 0 | ||
# HELP second_retried_messages_total_current Total number of retried messages. | ||
# TYPE second_retried_messages_total_current counter | ||
second_retried_messages_total_current 0 | ||
# HELP second_unprocessed_messages_total_current Total number of unprocessed messages. | ||
# TYPE second_unprocessed_messages_total_current counter | ||
second_unprocessed_messages_total_current 0 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const port = 8000 | ||
|
||
func StartAPI(metricCollectors ...prometheus.Collector) { | ||
f := fiber.New( | ||
fiber.Config{ | ||
DisableStartupMessage: true, | ||
DisableDefaultDate: true, | ||
DisableHeaderNormalizing: true, | ||
}, | ||
) | ||
|
||
metricMiddleware, err := NewMetricMiddleware(f, metricCollectors...) | ||
|
||
if err == nil { | ||
f.Use(metricMiddleware) | ||
} else { | ||
fmt.Printf("metric middleware cannot be initialized: %v", err) | ||
} | ||
|
||
fmt.Printf("server starting on port %d", port) | ||
|
||
go listen(f) | ||
} | ||
|
||
func listen(f *fiber.App) { | ||
if err := f.Listen(fmt.Sprintf(":%d", port)); err != nil { | ||
fmt.Printf("server cannot start on port %d, err: %v", port, err) | ||
} | ||
} |
Oops, something went wrong.