-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add Prometheus metrics support for DNS query monitoring #7937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
metalmatze
wants to merge
4
commits into
AdguardTeam:master
Choose a base branch
from
metalmatze:feature/prometheus-metrics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| ) | ||
|
|
||
| // DNS query result types matching internal/stats package | ||
| const ( | ||
| ResultNotFiltered = "not_filtered" | ||
| ResultFiltered = "filtered" | ||
| ResultSafeBrowsing = "safe_browsing" | ||
| ResultSafeSearch = "safe_search" | ||
| ResultParental = "parental" | ||
| ResultUnknown = "unknown" | ||
| ) | ||
|
|
||
| // DNSQueries tracks DNS queries by their processing result | ||
| var DNSQueries = prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "adguard_dns_queries_total", | ||
| Help: "Total number of DNS queries by processing result", | ||
| }, []string{"result"}) | ||
|
|
||
| // DNSResponseTime tracks DNS query response times using native exponential histogram | ||
| var DNSResponseTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
| Name: "adguard_dns_response_time_seconds", | ||
| Help: "DNS query response time in seconds", | ||
| NativeHistogramBucketFactor: 1.1, | ||
| }, []string{"result"}) | ||
|
|
||
| // RegisterDNSMetrics registers all DNS-related metrics with the provided registry | ||
| func RegisterDNSMetrics(registry *prometheus.Registry) { | ||
| registry.MustRegister(DNSQueries) | ||
| registry.MustRegister(DNSResponseTime) | ||
| } | ||
|
|
||
| // IncrementDNSQueryByResult increments counters for a specific query result type | ||
| func IncrementDNSQueryByResult(result string) { | ||
| DNSQueries.WithLabelValues(result).Inc() | ||
| } | ||
|
|
||
| // ObserveDNSResponseTime records a DNS query response time | ||
| func ObserveDNSResponseTime(result string, duration time.Duration) { | ||
| DNSResponseTime.WithLabelValues(result).Observe(duration.Seconds()) | ||
| } |
This file contains hidden or 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,53 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/testutil" | ||
| ) | ||
|
|
||
| func TestDNSMetrics(t *testing.T) { | ||
| // Create a new counter for isolated testing | ||
| testCounter := prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
| Name: "test_dns_queries_by_result_total", | ||
| Help: "Test counter for DNS queries by processing result", | ||
| }, []string{"result"}) | ||
|
|
||
| // Test incrementing queries by result | ||
| testCounter.WithLabelValues(ResultFiltered).Inc() | ||
| testCounter.WithLabelValues(ResultNotFiltered).Inc() | ||
| testCounter.WithLabelValues(ResultSafeBrowsing).Inc() | ||
| testCounter.WithLabelValues(ResultNotFiltered).Inc() // Add another not filtered | ||
|
|
||
| // Verify result counters | ||
| filteredValue := testutil.ToFloat64(testCounter.WithLabelValues(ResultFiltered)) | ||
| if filteredValue != 1 { | ||
| t.Errorf("Expected filtered queries to be 1, got %f", filteredValue) | ||
| } | ||
|
|
||
| notFilteredValue := testutil.ToFloat64(testCounter.WithLabelValues(ResultNotFiltered)) | ||
| if notFilteredValue != 2 { | ||
| t.Errorf("Expected not filtered queries to be 2, got %f", notFilteredValue) | ||
| } | ||
|
|
||
| safeBrowsingValue := testutil.ToFloat64(testCounter.WithLabelValues(ResultSafeBrowsing)) | ||
| if safeBrowsingValue != 1 { | ||
| t.Errorf("Expected safe browsing queries to be 1, got %f", safeBrowsingValue) | ||
| } | ||
| } | ||
|
|
||
| func TestRegisterDNSMetrics(t *testing.T) { | ||
| registry := prometheus.NewRegistry() | ||
|
|
||
| // This should not panic | ||
| RegisterDNSMetrics(registry) | ||
|
|
||
| // Registering again should panic due to duplicate registration | ||
| defer func() { | ||
| if r := recover(); r == nil { | ||
| t.Error("Expected panic when registering metrics twice") | ||
| } | ||
| }() | ||
| RegisterDNSMetrics(registry) | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it makes sense to have the metrics endpoint unauthenticated like the
/dns-queryendpoint for Prometheus scraping, it would be good to document the security considerations of exposing metrics without authentication. Consider adding a comment explaining what information is exposed and any potential security implications.