Skip to content
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

feat: Prometheus Endpoint #123

Merged
merged 1 commit into from
Apr 16, 2024
Merged

feat: Prometheus Endpoint #123

merged 1 commit into from
Apr 16, 2024

Conversation

aster1sk
Copy link

@aster1sk aster1sk commented Apr 16, 2024

I understand there is interest in keeping birdnet-go as lightweight as possible so instead of building out a full-blown Web UI, perhaps an alternative option could be to export metrics to Prometheus and visualized with existing tools like Grafana.

This PR include a bool configuration option to enable a very basic Prometheus CounterVec as well as all of the boilerplate Go performance metrics.

To enable the Prometheus endpoint, simply include the following in your configuration file.

realtime:
    prometheus: true

The current implementation is very simple, the following is sample output from development / testing :

# HELP birdnet_detections How many birdnet_detections partitioned by common name.
# TYPE birdnet_detections counter
birdnet_detections{name="American Robin"} 4
birdnet_detections{name="Downy Woodpecker"} 2
birdnet_detections{name="House Sparrow"} 10
birdnet_detections{name="Northern Cardinal"} 6
birdnet_detections{name="Pileated Woodpecker"} 1
birdnet_detections{name="Rock Pigeon"} 1
birdnet_detections{name="Ruby-crowned Kinglet"} 1

This could easily be improved upon with :

  • Dog or human counter events
  • Measuring confidence either by species or overall
  • Measuring processing time

Summary by CodeRabbit

  • New Features
    • Added support for Prometheus metrics in realtime processing, including tracking BirdNET detections and exposing metrics via an HTTP endpoint.

…rVec as well os default Go metrics listening on :2112
Copy link
Contributor

coderabbitai bot commented Apr 16, 2024

Walkthrough

The recent update integrates Prometheus metrics into the system's realtime processing capabilities. This includes initializing and registering metrics, specifically a counter for BirdNET detections by common name, and setting up an HTTP endpoint for metrics exposure. Additionally, the processor's method for handling pending detections now updates these metrics based on specific conditions, and configuration settings have been adjusted to support these changes.

Changes

File Path Change Summary
cmd/realtime/..., internal/analysis/realtime.go Added Prometheus metrics initialization, BirdNET detection counter, and metrics exposure via HTTP.
internal/analysis/processor/processor.go Updated pendingDetectionsFlusher method to modify Prometheus detection counter.
internal/conf/config.go Added support for Prometheus metrics in Realtime settings, including new configuration fields.

Poem

🌟🐰✨
In the realm of code, where the rabbits hop,
Metrics spring up, never to stop.
Counting the birds, so high and so free,
With Prometheus's help, as easy as can be.
Hooray for changes, let's give a cheer,
For better insights are now here!
🌟🐰✨


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 1659c9a and 44b9898.
Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum
Files selected for processing (4)
  • cmd/realtime/realtime.go (1 hunks)
  • internal/analysis/processor/processor.go (1 hunks)
  • internal/analysis/realtime.go (2 hunks)
  • internal/conf/config.go (2 hunks)
Additional comments not posted (3)
cmd/realtime/realtime.go (1)

40-40: Added flag for enabling Prometheus metrics correctly.

internal/conf/config.go (1)

43-44: Added configuration fields for Prometheus metrics correctly.

internal/analysis/processor/processor.go (1)

332-336: Update to Prometheus detection counter implemented correctly.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Comment on lines +51 to +62
if settings.Realtime.Prometheus {
settings.Realtime.PrometheusDetectionCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "birdnet_detections",
Help: "How many BirdNET detections partitioned by common name.",
},
[]string{"name"},
)
prometheus.MustRegister(settings.Realtime.PrometheusDetectionCounter)
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":2112", nil)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization of Prometheus metrics and setup of HTTP endpoint are implemented. Consider handling errors from http.ListenAndServe and making the port configurable.

- go http.ListenAndServe(":2112", nil)
+ go func() {
+     err := http.ListenAndServe(settings.Realtime.MetricsPort, nil)
+     if err != nil {
+         log.Printf("Failed to start metrics server: %v", err)
+     }
+ }()

Add a MetricsPort field to the Realtime struct in the configuration to allow port configuration.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if settings.Realtime.Prometheus {
settings.Realtime.PrometheusDetectionCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "birdnet_detections",
Help: "How many BirdNET detections partitioned by common name.",
},
[]string{"name"},
)
prometheus.MustRegister(settings.Realtime.PrometheusDetectionCounter)
http.Handle("/metrics", promhttp.Handler())
go http.ListenAndServe(":2112", nil)
}
if settings.Realtime.Prometheus {
settings.Realtime.PrometheusDetectionCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "birdnet_detections",
Help: "How many BirdNET detections partitioned by common name.",
},
[]string{"name"},
)
prometheus.MustRegister(settings.Realtime.PrometheusDetectionCounter)
http.Handle("/metrics", promhttp.Handler())
go func() {
err := http.ListenAndServe(settings.Realtime.MetricsPort, nil)
if err != nil {
log.Printf("Failed to start metrics server: %v", err)
}
}()
}

@tphakala tphakala changed the title Prometheus Endpoint feat: Prometheus Endpoint Apr 16, 2024
@tphakala tphakala merged commit f4cbefb into tphakala:main Apr 16, 2024
3 of 4 checks passed
@tphakala
Copy link
Owner

Thank you, merged.

@isZumpo
Copy link
Contributor

isZumpo commented Apr 16, 2024

This is very nice! I would like to add some more metrics in the future for observability of the health of the application etc 👍

Just one question, would it be better to offer the /metrics view at the same port used for serving the webpage itself at? In this case 8080. That would make the port forwarding a bit easier. However, I suppose one benefit of having it on a separate port is that it allows more control of who should have access to the metrics?

@tphakala
Copy link
Owner

tphakala commented Apr 17, 2024

I have been thinking of adding Go telemetry for some time , so this is more than welcome. I'd prefer to keep telemetry endpoint separate from main application, for example if people wish to publish their dashboard to Internet it would be at least some level security issue if application telemetry would be exposed as well.

if settings.Realtime.Prometheus {
settings.Realtime.PrometheusDetectionCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "birdnet_detections",
Copy link

@d0ugal d0ugal May 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a counter it probably should have ended with _total or _count. You'll probably see warnings like this when using PromQL.

PromQL info: metric might not be a counter, name does not end in _total/_sum/_count/_bucket

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey nice catch! I don't know anything about PromQL but I see no reason why this small change shouldn't be applied.

I think my (selfish) goal of this PR was simply to introduce an option to use existing visualization systems instead of building yet another HTML dashboard ( not saying there's anything wrong with the existing one <3 ).

Feel free to make this change if @tphakala's on board.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - this is just a minor nit. I am using the metric and it works get btw 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants