Golang Agent Overview and Setup Tutorial

Author

Reads 1.2K

Close-up of colorful programming code on a blurred computer monitor.
Credit: pexels.com, Close-up of colorful programming code on a blurred computer monitor.

Let's get started with setting up a Golang agent. A Golang agent is a lightweight, standalone program that can be used to collect and send metrics to a monitoring system.

Golang agents are designed to be easy to use and require minimal configuration. They are also highly customizable, allowing you to collect metrics from a wide range of sources.

To set up a Golang agent, you'll need to download the agent binary from the official Golang agent repository. This binary can be run on a variety of platforms, including Windows, macOS, and Linux.

Installation and Setup

To install the Go agent, you can follow the instructions in our GETTING_STARTED document or our GUIDE. This will get you started with the installation process.

You can also import the agent and create an application to get useful runtime information about your number of goroutines, garbage collection statistics, memory and CPU usage. This is a great way to get started without adding extra instrumentation to your code.

Credit: youtube.com, How to Install Go on WSL Ubuntu | Golang Setup on Windows 10 (Step-by-Step)

If you're starting a new project, create and navigate to your project directory using `mkdir my-agent-project` and `cd my-agent-project`. Then, initialize a new Go module with `go mod init github.com/yourusername/my-agent-project`.

To install the Agent SDK, use `go get github.com/pontus-devoteam/agent-sdk-go`. Note that this requires Go 1.23 or later.

You can also use our many INTEGRATION packages for out-of-the-box support for many popular Go web frameworks and libraries. We continue to add integration packages based on your feedback, so feel free to weigh in on potential integrations by opening an Issue here in our New Relic Go agent GitHub project.

Expand your knowledge: Newrelic Browser Agent

Compatibility and Requirements

To use the golang agent, you'll need a compatible environment. Go 1.22+ is the minimum version required.

The agent supports a range of operating systems, including Linux, OS X, and Windows. Windows support starts from Vista and Server 2008.

Expand your knowledge: Windows Install Golang

Getting Started and Development

To get started with the Go agent, you can run the example found in v3/examples/server/main.go, which will appear as "Example App" in your New Relic applications list. To run it, you'll need to access the exposed endpoints at http://localhost:8000/ and http://localhost:8000/notice_error.

A fresh viewpoint: Golang vs Go

Credit: youtube.com, Demystifying AI Agents: Building One from Scratch in Go

If you're starting a new project, create a new directory and navigate to it with `mkdir my-agent-project` and `cd my-agent-project`. Then, initialize a new Go module with `go mod init github.com/yourusername/my-agent-project` and install the Agent SDK with `go get github.com/pontus-devoteam/agent-sdk-go`. This requires Go 1.23 or later.

For development, you can check the source code of the Go agent, which is open source software. This allows you to browse its source code, send improvements, or create your own fork and build it.

Development

Development is where the magic happens. To start developing with the New Relic Go agent, you'll need to create a new project directory using the command `mkdir my-agent-project` and then navigate to it with `cd my-agent-project`.

You'll also need to initialize a new Go module using the command `go mod init github.com/yourusername/my-agent-project`. This will create a `go.mod` file in your project directory.

To install the Agent SDK, run the command `go get github.com/pontus-devoteam/agent-sdk-go`. Note that this requires Go 1.23 or later.

Worth a look: Golang Go

Credit: youtube.com, Getting started with Agent Development Kit

If you're starting from scratch, you can follow the example provided in the New Project Setup section to get started. This will guide you through the process of creating a new project, initializing a new Go module, and installing the Agent SDK.

Here are some key tools and frameworks that you can use with the New Relic Go agent:

These integration packages provide out-of-the-box support for popular Go web frameworks and libraries. You can use them to instrument your code and get the most out of the New Relic Go agent.

Overview

Getting Started with Agent SDK Go is exciting because it provides a comprehensive framework for building AI agents in Go. This means you can create agents that can use tools and perform handoffs to other specialized agents.

Agent SDK Go allows you to produce structured output, which is a big advantage. You can use it to create agents that are flexible and adaptable.

To get started, visit go-agent.org for comprehensive documentation and examples. This will give you a solid foundation to build on.

Agent SDK Go supports multiple LLM providers, giving you the freedom to choose the best tool for the job.

You might like: Real Estate Agents Email

Integration and Configuration

Credit: youtube.com, Golang (Go) & application configuration management using viper: Building Microservices

To integrate the Go agent, you can use our many integration packages for out-of-the-box support for many popular Go web frameworks and libraries. We continue to add integration packages based on your feedback.

You can weigh in on potential integrations on the Support Forum and our New Relic Go agent GitHub project. Simply import the agent and create an application to get useful runtime information about your number of goroutines, garbage collection statistics, and memory and CPU usage.

To manage some aspects of New Relic monitoring, you can change your Go agent configuration settings. This includes turning on high-security mode, adding custom labels for filtering and sorting, and managing what information is reported.

On a similar theme: New Relic Java Agent

Integration Packages

Integration packages extend the base New Relic package to support various frameworks and libraries. This allows for easier integration with popular Go web frameworks and libraries.

For frameworks and databases without an integration package, you can still instrument using the New Relic package primitives. This provides a flexible solution for those who need to integrate New Relic with frameworks not explicitly supported.

New Relic offers many integration packages for out-of-the-box support. These packages can be found by checking the Support Forum and the New Relic Go agent GitHub project.

You can weigh in on potential integrations on these platforms to help shape the future of New Relic's Go agent support.

Expand your knowledge: Install Golang Package

Serve

Diverse team working together at a modern office table with papers and technology.
Credit: pexels.com, Diverse team working together at a modern office table with papers and technology.

Serving up data with New Relic's Go agent is a breeze. You can serve the agent protocol on a given connection using the ServeAgent function, which returns when an I/O error occurs.

To get started, you'll need to import the agent and create an application to get useful runtime information about your number of goroutines, garbage collection statistics, and memory and CPU usage.

The ServeAgent function is just one of the many tools at your disposal. You can also use the ForwardToRemote and ForwardToAgent functions to route authentication requests to the ssh-agent process or a given keyring, respectively.

New Relic's Go agent makes it easy to instrument your code and get great data in a couple of ways. You can simply import the agent and create an application, or use their many integration packages for out-of-the-box support for popular Go web frameworks and libraries.

Here are some key functions to keep in mind:

By serving up your data with New Relic's Go agent, you'll gain a new level of visibility into your Golang applications and be able to monitor their performance, identify errors, and analyze business data with ease.

Additional reading: New Relic .net Agent

Logging and Monitoring

Credit: youtube.com, Caching, Logging and Monitoring

The New Relic Go agent makes it easy to log and monitor your Go application. You can send agent log messages to various logging libraries such as Logrus, Zap, and Zerolog.

To do this, you can use the integration packages provided by New Relic, which are listed in the Agent Logging table. This table shows the project, integration package, and description for each logging library.

Here is the Agent Logging table:

With these integration packages, you can easily send your log messages to New Relic and get a better understanding of your application's performance and behavior.

Logging

Logging is a crucial aspect of monitoring your application's performance. It allows you to track and analyze errors, understand user behavior, and make data-driven decisions.

To get started with logging, you'll need to choose a logging library that fits your needs. Some popular options include Logrus, Zap, and Zerolog, which can be integrated with New Relic using specific packages.

Credit: youtube.com, LOG - Logging and Monitoring

These packages, such as v3/integrations/nrlogrus and v3/integrations/nrzap, enable you to send agent log messages to these logging libraries.

You can also use the standard library logger, which can be integrated with New Relic using the v3/integrations/logcontext-v2/logWriter package.

Here are some specific logging libraries and their corresponding integration packages:

By integrating these logging libraries with New Relic, you can gain valuable insights into your application's performance and user behavior.

Monitor App

Monitoring your app is crucial for identifying and fixing errors, as well as understanding its performance and health. To get started, you can simply import the New Relic Go agent and create an application to get useful runtime information about your number of goroutines, garbage collection statistics, and memory and CPU usage.

By instrumenting your Go code, you can gain a new level of visibility into your application's performance and health. This includes monitoring throughput, response times, and errors of transactions in your applications and services.

Curious to learn more? Check out: Golang Applications

Credit: youtube.com, EP. 18 - GCP Cloud Logging And Monitoring Explained For Beginners

New Relic for Go provides many integration packages for out-of-the-box support for popular Go web frameworks and libraries. You can weigh in on potential integrations on the Support Forum and the New Relic Go agent GitHub project.

To identify and fix errors, you can find bottlenecks by seeing time spent on database calls, external system calls, and key blocks of code. You can also view Goroutine counts and identify possible Goroutine leaks.

Here are some key metrics you can monitor to gain a better understanding of your app's performance and health:

  • Memory usage
  • Garbage collection behavior
  • CPU usage
  • Throughput
  • Response times
  • Errors

User ID Mapping to Transactions and Errors

Associating a user ID with each transaction and error is crucial for effective logging and monitoring. This can be done by using the New Relic Go agent API txn.SetUserID("example user ID") to provide the agent with a user ID.

The API call requires a single argument of a string representing a unique identifier for an end user. This can be a UUID, a database id, or similar.

Related reading: Rest Api with Golang

Credit: youtube.com, Azure Application Insights Tutorial | Amazing telemetry service

To inform the New Relic Go agent of what user ID to associate the transaction with, the API call should be made at least once per transaction. This ensures that the agent has the necessary information to supply the enduser.id agent attribute on the current transaction.

The API is intended to be called every time a new user ID has entered scope, making it ideal to be called via middleware that is aware of user session creation. This approach ensures that the New Relic Go agent is always up-to-date with the latest user ID information.

Once the New Relic Go agent has been made aware of the user ID, it will supply the enduser.id agent attribute on the current transaction as well as on any errors noticed during the current transaction's lifespan. This provides valuable insights into user-specific issues.

Troubleshooting and Debugging

Troubleshooting can be a real pain, but there's a simple way to resolve version conflicts. Specify a version using the `go get` command, like this: `go get github.com/pontus-devoteam/[email protected]`.

Credit: youtube.com, Databutton Troubleshooting Framework: Debug Faster with AI Agents (Step-by-Step Guide)

If you're working with private repositories or local development, consider using Go workspaces or replace directives in your `go.mod` file. This can save you a lot of headaches in the long run.

For debugging, you can enable debug output for various components by setting the appropriate environment variable. For general debugging, you can set the `GODEBUG` environment variable.

Recommended read: Golang Set Env Variable

Troubleshooting

Troubleshooting is a crucial step in resolving issues with your software. If you encounter version conflicts, you can resolve them by specifying a version when you install the package: go get github.com/pontus-devoteam/[email protected] # Replace with desired version.

For private repositories or local development, consider using Go workspaces or replace directives in your go.mod file. This can help you manage dependencies and avoid conflicts.

If you're experiencing issues with version conflicts, try specifying a version when installing the package. This can help you resolve the conflict quickly.

A fresh viewpoint: Create a Package in Golang

Handle Errors

Handling errors is a crucial part of troubleshooting and debugging. You can dynamically apply an error group to each noticed error using a callback function.

Worth a look: Golang Create Error

Credit: youtube.com, Why Is The Debugging Cycle Essential For Effective Troubleshooting? - Learn To Troubleshoot

To do this, you need to provide the agent with a callback function using the newrelic.ConfigSetErrorGroupCallbackFunction option. This callback function will receive a newrelic.ErrorInfo object as its input argument.

The newrelic.ErrorInfo object contains various attributes such as the noticed Go error object, the time when the error was noticed, the error message, and the New Relic error class. These attributes can be accessed and used to determine the error group.

You can also use the NoticeError() and NoticeExpectedError() functions to report errors that the agent does not report automatically. Additionally, you can use the ErrorCollector.IgnoreStatusCodes() function to prevent the agent from reporting certain types of errors.

Here's a summary of the methods you can use to collect or ignore errors:

By using these methods, you can customize the way the Go agent reports errors to New Relic and improve your troubleshooting and debugging process.

Upgrading

Upgrading can be a bit of a challenge, but don't worry, I've got you covered. If you're upgrading from version 2.X to version 3.0, be sure to check our MIGRATION guide for details.

You'll want to take note of any specific steps or considerations mentioned in the guide, as they'll help ensure a smooth transition.

A fresh viewpoint: Golang Version Manager

Tools and Utilities

Credit: youtube.com, Exploring the new tool directive in Golang 1.24

In a golang agent, tools play a crucial role in allowing agents to perform actions using your Go functions.

These tools are essential for getting the most out of your Go functions, giving you the flexibility to automate tasks and streamline processes.

Tools are what make your Go functions truly useful, enabling agents to take action and drive results.

Func (*Key) String

The func (*Key) String method returns the storage form of an agent key, which is a base64 encoded serialized key.

If the comment is not empty, it's included in the returned string. This is useful for identifying and managing keys in a storage system.

The format of the returned string is base64 encoded, which is a standard method for encoding binary data. This encoding scheme preserves the original data, making it easy to decode and use.

In practical terms, this means that the returned string can be safely stored and retrieved without losing any critical information.

Tools

Clients Talking with Real Estate Agent
Credit: pexels.com, Clients Talking with Real Estate Agent

Tools are essential for getting work done efficiently. They allow you to perform actions using your Go functions.

Agents can use tools to automate repetitive tasks and streamline processes. This can save a significant amount of time and reduce errors.

Tools enable agents to execute custom actions, making them a powerful addition to your toolkit.

Example Use Cases

You can get started with the Go agent in just a few ways. Even without adding instrumentation, importing the agent and creating an application will provide useful runtime information about your number of goroutines, garbage collection statistics, and memory and CPU usage.

If you're using a popular Go web framework or library, you can take advantage of our many integration packages for out-of-the-box support. We continue to add integration packages based on user feedback, so be sure to weigh in on potential integrations by opening an issue in our New Relic Go agent GitHub project.

Here are some key benefits of using the Go agent with and without instrumentation:

Local LLM Examples

Credit: youtube.com, How I use LLMs

Local LLM Examples are a great way to get started with using a Large Language Model (LLM) in your projects.

To run these examples, you'll need to have LM Studio running on your local machine. This typically involves navigating to the example directory and running the example with the command `go run .`.

Make sure LM Studio is running with a server at http://127.0.0.1:1234/v1 before attempting to run the examples.

If you're using the multi-agent example, you can navigate to the example directory with the command `cd examples/multi_agent_example`.

Here are the general steps to run a local LLM example:

  1. Make sure LM Studio is running with a server at http://127.0.0.1:1234/v1
  2. Navigate to the example directory (e.g. `cd examples/multi_agent_example`)
  3. Run the example with `go run .`

Example (Test-Services)

The test-services-* targets allow developers to run tests without needing to execute a shell into the agent container. This is a convenient option for testing without the extra step of opening a shell.

By specifying PROFILE=nrpgx5, you can ensure that the postgres container is started alongside the agent container to test the nrpgx5 integration. This is necessary if you want to run postgres tests.

A fresh viewpoint: How to Run Golang File

Man and Woman Talking with Real Estate Agent
Credit: pexels.com, Man and Woman Talking with Real Estate Agent

To run a test, you can use the following docker command:

  1. docker exec executes a command in a running container.
  2. -e TEST=internal/utilization passes the TEST argument as an environment variable.
  3. nr-go is the name of the container where the command will be executed.
  4. make core-test is the command to be executed.

This command allows you to run tests without manually opening a shell into the agent container.

The test-services agent container is volume-mounted to the local working directory containing the agent checkout. This means that code and test changes can be made and will automatically propagate to the container environment.

Ismael Anderson

Lead Writer

Ismael Anderson is a seasoned writer with a passion for crafting informative and engaging content. With a focus on technical topics, he has established himself as a reliable source for readers seeking in-depth knowledge on complex subjects. His writing portfolio showcases a range of expertise, including articles on cloud computing and storage solutions, such as AWS S3.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.