Golang Metrics for Go Developers

Author

Reads 230

Programming Code on Laptop Screen
Credit: pexels.com, Programming Code on Laptop Screen

As a Go developer, you're likely no stranger to the importance of metrics in your code. In fact, metrics can help you identify performance bottlenecks, optimize your application, and even catch bugs before they become major issues.

Go has a built-in way to collect metrics, which can be used to monitor your application's performance. This is achieved through the use of the `runtime.MemStats` struct, which provides information about the memory usage of your application.

To get started with collecting metrics in Go, you'll need to use the `runtime` package. This package provides a range of functions for collecting and analyzing metrics, including memory usage, CPU usage, and even garbage collection statistics.

Consider reading: Install Golang Package

Metrics Basics

The Go runtime package provides live information on process metrics, including estimated total CPU time spent running user Go code and cumulative sum of memory allocated to the heap by the application.

This information is exposed through a new package called runtime/metrics, which aggregates the list of metrics and their metadata in a single API call: metrics.All(). The returning metadata struct contains properties such as Name, Description, Kind, and Cumulative.

Here are some key metrics provided by the runtime/metrics package:

These metrics can be collected and printed using the metrics.Read(samples) function with the metadata of requested metrics.

Labels

Credit: youtube.com, Metric Progression & Factor Labeling Basics.mpg

Labels are used to push metrics with additional context and filter out unwanted information. Most metrics have an equivalent ending with WithLabels, which allows you to translate metrics into Prometheus labels.

Some labels may increase the cardinality of metrics, so the library provides a filtering system to control which labels are sent to the underlying Sink. This filtering system is global to all metrics.

You can specify which labels to allow or block using the Config.AllowedLabels and Config.BlockedLabels fields. If Config.AllowedLabels is not nil, only the specified labels will be sent to the underlying Sink. If Config.BlockedLabels is not nil, any label specified in this value will not be sent.

Here's a quick summary of the label filtering options:

By default, both Config.AllowedLabels and Config.BlockedLabels are nil, meaning no tags are filtered at all. This allows you to globally block some tags with high cardinality at the application level.

Memory Demystified

Memory usage is a crucial metric for any application, and understanding it can help you optimize your code for better performance.

Credit: youtube.com, The Missing Benchmark Metric: Memory Consumption

A Gauge metric in OpenTelemetry is used to track values that fluctuate over time without being accumulated across time periods. This is particularly useful for metrics like memory usage, which vary based on application state.

Here are some key memory metrics provided by the Go runtime package:

These metrics can help you understand how your application is using memory and identify potential issues.

Backwards Compatibility

Backwards Compatibility is crucial when it comes to upgrading libraries. Go treats a renamed module as entirely distinct and will happily compile both modules into the same binary.

This means that having two global metrics handlers could cause a subset of metrics to be effectively lost. If your application configures go-metrics exporting via the armon namespace, then any metrics sent to go-metrics via the hashicorp namespaced module will never get exported.

To avoid this issue, you can use build tags to specify which metrics handler to use. Here are the available build tags:

  • armonmetrics - Using this tag will cause metrics to be routed to armon/go-metrics
  • hashicorpmetrics - Using this tag will cause all metrics to be routed to hashicorp/go-metrics

If no build tag is specified, the default behavior is to use armon/go-metrics. This will change in the future, so it's essential to plan ahead.

Monitoring and Visualization

Credit: youtube.com, How to Monitor/Instrument Golang with Prometheus (Counter - Gauge - Histogram - Summary)

Monitoring and Visualization is a crucial part of any GoLang application, allowing you to track key metrics and make data-driven decisions.

Prometheus is a popular tool for storing and visualizing metrics, and can be easily integrated into your GoLang application. You can use the Prometheus server to collect metrics from your application and store them in a server store.

Grafana is a great tool for visualizing metrics, and can be used in conjunction with Prometheus to create dashboards and panels that provide valuable insights into your application's performance.

Another approach to monitoring and visualization is to use OpenTelemetry instrumentation libraries and the OpenTelemetry Collector agent. This allows you to collect and process metrics from multiple sources and ship them to multiple backends.

The OpenTelemetry Collector agent can collect, process, and ship metrics to multiple backends, making it a powerful tool for monitoring and visualization.

Here are the three metrics automatically generated by the otelhttp instrumentation:

  1. http.server.request.size: A Counter that tracks the cumulative size of all HTTP request messages.
  2. http.server.response.size: A Counter that tracks the cumulative size of all HTTP response messages.
  3. http.server.duration: A Histogram that measures the duration of inbound HTTP requests, with data grouped into predefined buckets for analyzing response times.

These metrics can be used to gain valuable insights into your application's performance and make data-driven decisions to optimize its performance.

OpenTelemetry Integration

Credit: youtube.com, Implementing OpenTelemetry Metrics in Golang

To set up OpenTelemetry in your Go application, you need to install the necessary packages first. This command installs OpenTelemetry SDK components, including the Go implementation of OpenTelemetry APIs and an exporter for exporting metrics to the standard output.

You should create an otel.go file at the project root to set up the OpenTelemetry SDK. This code sets up a metrics system for your Go application using OpenTelemetry, preparing and registering the application's metrics provider, and providing a mechanism for shutting down the initialized OpenTelemetry SDK components.

A Resource instance is created to give the application a unique identity by setting attributes like the ServiceName and ServiceVersion. A MeterProvider is initialized with newMeterProvider() and set as the global provider using otel.SetMeterProvider().

To export collected metrics data, a stdoutmetric exporter is configured to output metrics to the console, providing a straightforward way to test or debug configuration. The exporter outputs data every three seconds instead of every minute for a quicker feedback cycle.

Curious to learn more? Check out: How to Update a Github Using Golang

Credit: youtube.com, OpenTelemetry Golang Tutorial (Tracing in Grafana & Kubernetes & Tempo)

Here's a summary of the steps to set up OpenTelemetry:

  • Install OpenTelemetry SDK components
  • Create an otel.go file to set up the OpenTelemetry SDK
  • Create a Resource instance to give the application a unique identity
  • Initialize a MeterProvider and set it as the global provider
  • Configure a stdoutmetric exporter to output metrics to the console

You can explore the automatic OpenTelemetry dashboard in Better Stack to visualize your metrics. To send metrics data to an OpenTelemetry backend, you can route the data through the OpenTelemetry Collector, which can then process and forward the metrics to a backend of your choice.

Metric Collection

Collecting metrics is a crucial step in monitoring and understanding your Go application's performance. The dedicated documentation for collecting Go custom metrics with DogStatsD is a great resource to get started.

To collect custom metrics, you'll want to see the dedicated documentation for DogStatsD. It's a reliable source of information for this specific task.

DogStatsD is a service that helps you collect and send metrics to your monitoring system. This is particularly useful for Go applications, as it provides a straightforward way to collect and track custom metrics.

For more information on how to implement DogStatsD in your Go application, be sure to check out the dedicated documentation. It's a treasure trove of information on this specific topic.

See what others are reading: Golang Applications

Customization and Optimization

Credit: youtube.com, Software Architecture in Golang: Measuring Instability as Software Package Metrics

Go is a statically typed language, which means that the compiler checks for type errors at compile time, reducing runtime errors. This makes it easier to catch and fix mistakes early on.

To take advantage of Go's type safety, developers can use the built-in `fmt.Println` function to print formatted output, as demonstrated in the example, which shows how to print a custom message with a variable value.

By using Go's type safety features, developers can write more reliable and maintainable code, reducing the risk of runtime errors and making it easier to scale their applications.

Worth a look: Golang vs Go

Customizing Histograms

Customizing Histograms can be done using the metric.WithExplicitBucketBoundaries() function, which allows you to adjust the default bucket boundaries that are optimized for millisecond durations.

If you're measuring in seconds, for instance, you can customize the bucket boundaries to better fit your use case. The default boundaries are optimized for millisecond durations, but you can change them to suit your needs.

Dropping Unwanted

Digital Gauge Meter Reading 7:43
Credit: pexels.com, Digital Gauge Meter Reading 7:43

You can filter out unwanted metrics using custom views to prevent an overload of data. To drop metrics, modify your newMeterProvider() function.

Creating individual views for each unwanted metric can be tedious, especially if you have many to drop. You can use a custom view with regular expressions to drop multiple metrics at once.

A regular expression can match multiple metric names and apply the Drop aggregation to them. If a metric matches the regex, the view drops it; otherwise, the metric is retained.

Using a regular expression can save time and effort when managing a large number of unwanted metrics. This approach can also help you keep only the metrics that are useful to prevent added costs.

Once you save the file, you can test the changes by sending traffic to the server root using a tool like wrk.

A Shout Out to a Go Development Team

I've had the opportunity to discuss additional metrics with the Go development team, who were delighted to include them in the next versions. The team, including Sean Liao, Michael Knyszek, and Russ Cox, was responsive to the community's needs.

Credit: youtube.com, 2022 LLVM Dev Mtg: Challenges Of Enabling Golang Binaries Optimization By BOLT

The Go development team's philosophy is that this package should contain changing metrics not intended for static configuration, such as Go Version and Max maximum number of CPUs (GOMaxPROCS).

The team shared that exposing these metrics in a new package could help diagnose issues better, similar to the runtime metrics package.

For another approach, see: Golang Go

Claire Beier

Senior Writer

Claire Beier is a seasoned writer with a passion for creating informative and engaging content. With a keen eye for detail and a talent for simplifying complex concepts, Claire has established herself as a go-to expert in the field of web development. Her articles on HTML elements have been widely praised for their clarity and accessibility.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.