Top 10 tips for instrumenting Golang with New Relic, part 5
Tags: tutorial, devops, coding, monitoring
Published: 2023-11-11 21:17:37

_To read this full New Relic blog, [click here](https://newrelic.com/blog/how-to-relic/top-tips-golang-5?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong)._

---

Are you building modern apps in Golang? This multi-part blog series will help you instrument Golang faster by providing tips, guidance, and best practices with New Relic.

Typically, the content in this blog would be presented in a workshop, but we've made it a blog series so anyone can follow along "hands on," using sample code, Instruqt, this blog, and videos. For the best experience:

[See and run the full code that I'll be walking through in this multi-part series.](https://github.com/ntcsteve/instrumentGolangWithNR)

Review previous guides - [Part 1](https://newrelic.com/blog/how-to-relic/top-tips-golang-1?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong), [Part 2](https://newrelic.com/blog/how-to-relic/top-tips-golang-2?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong), [Part 3](https://newrelic.com/blog/how-to-relic/top-tips-golang-3?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) and [Part 4](https://newrelic.com/blog/how-to-relic/top-tips-golang-4?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong).

##Go Instrumentation Checklist 

In [Part 1](https://newrelic.com/blog/how-to-relic/top-tips-golang-1?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong), [Part 2](https://newrelic.com/blog/how-to-relic/top-tips-golang-2?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong), [Part 3](https://newrelic.com/blog/how-to-relic/top-tips-golang-3?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) and [Part 4](https://newrelic.com/blog/how-to-relic/top-tips-golang-4?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) we provided extensive coverage and tips for better instrumentation of Golang in New Relic. The Golang agent differs from others in that it does not offer out-of-the-box auto instrumentation. Because Golang is a compiled language, it does not allow for dynamic runtime or method replacement like dynamic languages such as Java, .NET, Node.js, and PHP.

While Golang has unique language advantages, **code modification is required** to monitor Go applications. On the positive side, you have **complete control** over what to instrument and can highly customize it to your needs. In the last part of this series, we will summarize everything we have covered so far with a production checklist to use before onboarding your Go apps into production.

##1. Using appropriate API key

License keys are used to report almost all data, with the exception of browser and mobile monitoring data, which have their own keys. Each key is unique to an account, allowing for tracking and monitoring of multiple sources of data. This flexibility ensures comprehensive and accurate reporting. Please ensure you have the [right](https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/#overview-keys) license type before starting your Golang instrumentation. 

##2. Leverage best practices

As we've previously discussed, when working with compiled languages such as Go, manual instrumentation is essential. It's not only important to have a solid grasp of how to use the platform itself, but also to understand how to effectively utilize New Relic within your specific environment. By doing so, you can maximize the benefits of New Relic and ensure optimal performance of your Go applications. Here is the best practice [guide](https://docs.newrelic.com/docs/new-relic-solutions/best-practices-guides/full-stack-observability/apm-best-practices-guide/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) from New Relic, including the basics such as tags.

##3. Time to run the app

If you are instrumenting a small sample or a short-lived application in your development environment, it is important to include the following [code](https://github.com/newrelic/go-agent/blob/master/v3/examples/short-lived-process/main.go) segment. This is because the harvest cycle in the New Relic Go agent is set to 5 seconds, which means that data is collected and sent to New Relic every 5 seconds. By including this code segment, you ensure that the data from your app is accurately captured and monitored during development.

However, for production deployment, you can safely omit this code segment if your application runs for longer than 5 seconds. In production, the New Relic Go agent automatically adjusts the harvest cycle based on the needs and duration of your application. This ensures that your app's performance is effectively monitored without the need for manual intervention.

##4. Distributed Tracing

Starting from version 3.16.0, the Go agent has introduced a significant enhancement - the automatic enabling of [Distributed Tracing](https://docs.newrelic.com/docs/apm/agents/go-agent/configuration/go-agent-configuration/#distributed-tracing?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong). This means that the agent now proactively adds distributed tracing headers to outbound requests and intelligently scans incoming requests for distributed tracing headers. By default, this feature is enabled to provide seamless tracing capabilities.

Distributed Tracing is a powerful technique that allows you to trace the path of a single request as it traverses through various services and components of your application. It provides invaluable insights into the performance and behavior of your distributed systems, helping you identify bottlenecks, latency issues, and dependencies.

With the Go agent's automatic distributed tracing, you no longer need to manually configure tracing headers or worry about missing out on important tracing data. The agent takes care of all the necessary tracing operations behind the scenes, ensuring that your distributed systems are effectively monitored and traced.

However, if for any reason you want to disable distributed tracing, the Go agent provides a simple configuration option. By setting the value to false, you can turn off distributed tracing and revert to the default behavior. This gives you the flexibility to control the tracing capabilities of your application according to your specific requirements.

##5. Capture contextual data

When utilizing New Relic, it can be highly beneficial to leverage [custom attributes](https://docs.newrelic.com/docs/apm/agents/go-agent/api-guides/guide-using-go-agent-api/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) in order to enhance your events with additional metadata. By using the AddAttribute() code, you can easily incorporate your own contextual data into your transactions, allowing for a more comprehensive and meaningful understanding of your application's performance.

Custom attributes provide an opportunity to decorate your transactions with relevant information that goes beyond the default metrics and data captured by New Relic. This can include details specific to your application, such as user-specific information, product details, pricing data, or any other relevant metadata. By customizing your events with these attributes, you gain deeper insights into the factors that impact your application's performance and user experience.

With the ability to capture and analyze this additional contextual data, you can better understand the root causes of any performance issues or anomalies. This, in turn, enables you to make data-driven decisions and optimizations to improve the overall performance and user satisfaction of your application.

```
txn.AddAttribute("product", "widget")
txn.AddAttribute("price", 19.99)
txn.AddAttribute("importantCustomer", true)
```

##6. Instrumentation at scale

To improve the instrumentation at scale throughout your environment, it is recommended to examine the Gin Framework as a starting example. By following this approach, you can effectively instrument your middleware layer and gain valuable insights into its performance. Here is an example of how you can implement it:

```
router := gin.Default()
// Add the nrgin middleware before other middlewares or routes:
router.Use(nrgin.Middleware(app))
```

This package provides a Gin middleware that can be used to instrument all requests. To use it, call `nrgin.Middleware` to get a `gin.HandlerFunc`. Then, add the handler to your application as a middleware to instrument inbound requests handled by a `gin.Engine`. Implementing this approach allows you to gain deep insights into the behavior and performance of your middleware layer, facilitating effective troubleshooting and optimization. It enhances your ability to identify bottlenecks, track custom metrics, and make data-driven decisions to improve the overall performance of your application.

##7. Production rollout

Once you have determined how to incorporate these instrumentations at scale across your environment, it is essential to have a discussion with your team or engineering lead regarding the optimal approach for deploying them through your deployment pipeline. This step is crucial as it allows you to gather input and insights from your colleagues, ensuring that the deployment process is efficient, effective, and aligned with your team's goals and strategies.

Engaging in this discussion allows you to leverage the collective expertise and experience of your team members, enabling the identification of any potential challenges or considerations that need to be addressed. This collaborative approach fosters a sense of ownership and accountability among team members, as everyone has the opportunity to contribute their ideas and suggestions. Additionally, involving your engineering lead in this discussion has the added benefit of gaining their guidance and leadership. Their experience and knowledge can offer valuable insights into best practices and industry standards, enabling you to make informed decisions and avoid potential pitfalls. Here are the common operations you need to perform on the Go agent.

- 
To [install](https://docs.newrelic.com/docs/apm/agents/go-agent/installation/install-new-relic-go/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) the Go agent

- 
To [configure](https://docs.newrelic.com/docs/apm/agents/go-agent/configuration/go-agent-configuration/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) the Go agent

- 
To [update](https://docs.newrelic.com/docs/apm/agents/go-agent/installation/update-go-agent/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) the Go agent

- 
To [uninstall](https://docs.newrelic.com/docs/apm/agents/go-agent/installation/uninstall-go-agent/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong) the Go agent

##8. Apdex for Production

In this informative [video](https://www.youtube.com/watch?v=RoLIBfEpYZg), you will learn how to accurately calculate Apdex and identify the ideal threshold for your applications. The video provides a comprehensive guide that takes you through the process step by step, ensuring that you have a clear understanding of how to determine the correct threshold for your specific use case. Additionally, the video offers a simple and effective NRQL query that allows you to easily find the best thresholds for your app. By following the instructions provided in this video, you will be able to optimize the performance of your applications and ensure an exceptional user experience.

<iframe width="744" height="419" src="https://www.youtube.com/embed/RoLIBfEpYZg" title="How To Calculate Apdex and Set the Right Thresholds" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>

##9. Alerts and Dashboards

To improve visibility and control over your application's performance, you can utilize the powerful features of New Relic through configuring alerts and dashboards. By setting up alerts, you will receive timely notifications whenever there are fluctuations in key performance metrics across your entire stack. These alerts can be tailored to meet your specific needs, allowing you to monitor and track important aspects of your application.

In addition, New Relic's dashboards provide a comprehensive overview of your application's performance data, enabling you to visualize and analyze the most relevant metrics. With the ability to gather and chart specific data from different sources, the dashboards offer a flexible and customizable experience, allowing you to monitor your application's performance from anywhere within the New Relic platform.

By utilizing both alerts and dashboards, you can proactively monitor your application's performance, identify potential issues or bottlenecks, and take appropriate actions to ensure optimal performance and user experience. Whether it involves detecting sudden increases in response time, monitoring server health and resource utilization, or tracking the success rate of specific transactions, New Relic's alerts and dashboards provide you with the necessary tools and insights to effectively manage your application's performance.

- 
Introduction to [Alerts](https://docs.newrelic.com/docs/alerts-applied-intelligence/overview/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong)

- 
Introduction to [Dashboards](https://docs.newrelic.com/docs/query-your-data/explore-query-data/dashboards/introduction-dashboards/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong)

##10. Analyze Deployments

New Relic Change Tracking allows you to capture and record deployments and other changes in your system, providing valuable context to performance data. This feature enables you to easily view and analyze the impact of changes on system performance and quality. The change analysis UIs provide detailed insights, including faceted errors, log attribute trends, related issues and anomalies, and calculated impacts on key Golang signals. By utilizing change tracking metadata, such as commit SHAs, changelogs, and deep links, you can quickly identify and resolve issues. This powerful capability enhances your understanding of how changes affect your system and helps optimize development velocity.

There are several options available for you to begin recording changes (including deployments) to different parts of your system. These options provide both automated and manual ways to track changes effectively. Here are the available methods:

- 
[NerdGraph (GraphQL) API](https://docs.newrelic.com/docs/change-tracking/change-tracking-graphql/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong): By utilizing the NerdGraph API, you can take advantage of GraphQL mutations to designate the specific changes you want to track. This powerful API allows you to run queries and retrieve detailed information about the changes you are monitoring.

- 
[New Relic CLI](https://docs.newrelic.com/docs/change-tracking/change-tracking-cli/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong): Our command line interface, known as the New Relic CLI, offers a convenient way to designate and track changes. With just a few commands, you can easily specify the changes you want to monitor within your system.

- 
[Jenkins](https://docs.newrelic.com/docs/change-tracking/ci-cd/change-tracking-jenkins/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong): If you are using Jenkins as your CI/CD tool, we have developed a dedicated plugin that simplifies the process of tracking changes. By integrating this plugin into your Jenkins pipeline, you can effortlessly monitor and capture changes made to your system.

- 
[GitHub Actions](https://docs.newrelic.com/docs/change-tracking/ci-cd/change-tracking-github-actions/?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong): For those utilizing GitHub Actions in their CI/CD workflow, we provide comprehensive guidance on how to configure and leverage GitHub Actions for change tracking. This integration allows you to seamlessly track and manage changes directly within your CI/CD pipeline.

These flexible and versatile options empower you to choose the most suitable method for your specific requirements. Whether you prefer using APIs, command line interfaces, or integrating with popular CI/CD tools like Jenkins or GitHub Actions, New Relic has you covered when it comes to effectively tracking changes in your system.


---


_To read this full New Relic blog, [click here](https://newrelic.com/blog/how-to-relic/top-tips-golang-5?utm_source=devto&utm_medium=community&utm_campaign=global-fy24-q3-golong)._