Skip to content

🚀 Go Distributed Tracing & Metrics Sensor for Instana

License

Notifications You must be signed in to change notification settings

instana/go-sensor

Folders and files

NameName
Last commit message
Last commit date
Mar 28, 2024
Jan 22, 2021
Mar 21, 2025
Mar 14, 2025
Jun 5, 2024
Nov 22, 2022
Oct 26, 2022
Jan 22, 2021
Sep 5, 2024
Mar 21, 2025
Oct 26, 2022
Mar 21, 2025
Feb 13, 2025
Oct 26, 2022
Mar 6, 2024
Nov 22, 2022
Oct 26, 2022
Aug 21, 2024
Mar 4, 2020
Sep 11, 2024
Dec 5, 2023
Jan 22, 2021
Sep 6, 2024
Nov 23, 2022
Feb 13, 2025
Jul 28, 2023
May 22, 2023
Jan 29, 2025
Apr 25, 2023
Jun 26, 2023
Nov 21, 2022
Jun 5, 2024
Jan 29, 2025
Jan 29, 2025
Jan 28, 2025
May 22, 2023
Jan 28, 2025
Dec 10, 2024
Jan 29, 2025
Nov 13, 2024
Nov 14, 2022
Jan 29, 2025
Feb 3, 2025
Feb 19, 2025
Nov 22, 2022
Nov 10, 2022
Jan 22, 2021
Mar 4, 2020
Oct 26, 2022
Mar 6, 2023
Nov 7, 2022
May 22, 2023
Nov 8, 2023
Feb 13, 2025
Feb 13, 2025
Feb 13, 2025
Nov 30, 2021
Jan 29, 2025
Jun 26, 2023
Jan 29, 2025
Jun 5, 2024
Jan 29, 2025
Sep 5, 2024
Jan 29, 2025
Feb 13, 2025
Sep 5, 2024
Mar 25, 2024
Jan 29, 2025
Jan 10, 2025
May 23, 2023
Sep 26, 2023
Jan 29, 2025
Dec 10, 2024
Jan 29, 2025
Jul 3, 2023
Mar 6, 2024
Sep 19, 2023
Jan 29, 2025
Oct 25, 2021
Jan 29, 2025
May 26, 2023
Jan 22, 2021
Oct 26, 2022
Mar 25, 2021
Jul 26, 2021
May 22, 2023
Oct 26, 2022
May 9, 2022
Oct 26, 2022
Jan 29, 2025
Nov 22, 2022
Jan 29, 2025
Feb 15, 2024
Jan 29, 2025
Sep 26, 2023
Jan 28, 2025
Oct 20, 2022
Sep 20, 2021
Jan 22, 2021
Oct 26, 2022
Dec 3, 2024
Mar 25, 2024
May 5, 2022
Jan 29, 2025
Jan 29, 2025
Sep 26, 2023
Dec 10, 2024
Dec 10, 2024
Dec 10, 2024
Dec 10, 2024
Dec 10, 2024
Dec 10, 2024
Dec 10, 2024
Jan 29, 2025
Dec 10, 2024
May 16, 2023
Mar 21, 2025
Dec 10, 2024
Sep 19, 2023
Jan 22, 2021
Oct 26, 2022
Nov 7, 2022
Nov 12, 2024
Nov 12, 2024
Feb 13, 2025
Sep 24, 2024

Repository files navigation

IBM Instana Go Tracer

Build Status PkgGoDev OpenTracing Go Report Card

The IBM Instana Go Tracer is an SDK that collects traces, metrics, logs and provides profiling for Go applications. The tracer is part of the IBM Instana Observability tool set.

Compatibility

Supported Runtimes


Go Collector 1.67 or later supports Go 1.24 and 1.23.

Note

Make sure to always use the latest version of the tracer, as it provides new features, improvements, security updates and fixes.

Installation

To add the tracer to your project, run:

go get -u github.com/instana/go-sensor@latest

Note

As a good practice, add this command to your CI pipeline or your automated tool before building the application to keep the tracer up to date.

Usage

Initial Setup

Once the tracer is added to the project, import the package into the entrypoint file of your application:

import (
  ...
  instana "github.com/instana/go-sensor"
)

Create a reference to the collector and initialize it with a service name:

var (
  ...
  col instana.TracerLogger
)

func init() {
  ...
  col = instana.InitCollector(&instana.Options{
    Service: "My app",
  })
}

Note

The tracer expects the Instana Agent to be up and running in the default port 42699. You can change the port with the environment variable INSTANA_AGENT_PORT.

Note

For non default options, like the Agent host and port, the tracer can be configured either via SDK options, environment variables or Agent options.

Collecting Metrics

Once the collector has been initialized with instana.InitCollector, application metrics such as memory, CPU consumption, active goroutine count etc will be automatically collected and reported to the Agent without further actions or configurations to the SDK. This data is then already available in the dashboard.

Tracing Calls

Let's collect traces of calls received by an HTTP server.

Before any changes, your code should look something like this:

// endpointHandler is the standard http.Handler function
http.HandleFunc("/endpoint", endpointHandler)

log.Fatal(http.ListenAndServe(":9090", nil))

Wrap the endpointHandler function with instana.TracingHandlerFunc. Now your code should look like this:

// endpointHandler is now wrapped by `instana.TracingHandlerFunc`
http.HandleFunc("/endpoint", instana.TracingHandlerFunc(col, "/endpoint", endpointHandler))

log.Fatal(http.ListenAndServe(":9090", nil))

When running the application, every time /endpoint is called, the tracer will collect this data and send it to the Instana Agent. You can monitor traces to this endpoint in the Instana UI.

Profiling

Unlike metrics, profiling needs to be enabled with the EnableAutoProfile option, as seen here:

col = instana.InitCollector(&instana.Options{
  Service: "My app",
  EnableAutoProfile: true,
})

You should be able to see your application profiling in the Instana UI under Analytics/Profiles.

Logging

In terms of logging, the SDK provides two distinct logging features:

  1. Traditional logging, that is, logs reported to the standard output, usually used for debugging purposes
  2. Instana logs, a feature that allows customers to report logs to the dashboard under Analytics/Logs

Traditional Logging

Many logs are provided by the SDK, usually prefixed with "INSTANA" and are useful to understand what the tracer is doing underneath. It can also be used for debugging and troubleshoot reasons. Customers can also provide logs by calling one of the following: Collector.Info(), Collector.Warn(), Collector.Error(), Collector.Debug(). You can setup the log level via options or the INSTANA_LOG_LEVEL environment variable.

You can find detailed information in the Instana documentation.

Instana Logs

Instana Logs are spans of the type log.go that are rendered in a special format in the dashboard. You can create logs and report them to the agent or attach them as children of an existing span.

The code snippet below shows how to create logs and send them to the agent:

col := instana.InitCollector(&instana.Options{
  Service: "My Go App",
})

col.StartSpan("log.go", []ot.StartSpanOption{
  ot.Tags{
    "log.level":   "error", // available levels: info, warn, error, debug
    "log.message": "error from log.go span",
  },
}...).Finish() // make sure to "finish" the span, so it's sent to the agent

This log can then be visualized in the dashboard under Analytics/Logs. You can add a filter by service name. In our example, the service name is "My Go App".

Opt-in Exit Spans

Go tracer support the opt-in feature for the exit spans. When enabled, the collector can start capturing exit spans, even without an entry span. This capability is particularly useful for scenarios like cronjobs and other background tasks, enabling the users to tailor the tracing according to their specific requirements. By setting the INSTANA_ALLOW_ROOT_EXIT_SPAN variable, users can choose whether the tracer should start a trace with an exit span or not. The environment variable can have 2 values. (1: Tracer should record exit spans for the outgoing calls, when it has no active entry span. 0 or any other values: Tracer should not start a trace with an exit span).

export INSTANA_ALLOW_ROOT_EXIT_SPAN=1

Complete Example

Basic Usage

package main

import (
  "log"
  "net/http"

  instana "github.com/instana/go-sensor"
)

func main() {
  col := instana.InitCollector(&instana.Options{
    Service:           "Basic Usage",
    EnableAutoProfile: true,
  })

  http.HandleFunc("/endpoint", instana.TracingHandlerFunc(col, "/endpoint", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
  }))

  log.Fatal(http.ListenAndServe(":7070", nil))
}

Wrapping up

Let's quickly summarize what we have seen so far:

  1. We learned how to install, import and initialize the Instana Go Tracer.
  2. Once the tracer is initialized, application metrics are collected out of the box.
  3. Application profiling can be enabled via the EnableAutoProfile option.
  4. Tracing incoming HTTP requests by wrapping the Go standard library http.Handler with instana.TracingHandlerFunc.

With this knowledge it's already possible to make your Go application traceable by our SDK. But there is much more you can do to enhance tracing for your application.

The basic functionality covers tracing for the following standard Go features:

  1. HTTP incoming requests
  2. HTTP outgoing requests
  3. SQL drivers

As we already covered HTTP incoming requests, we suggest that you understand how to collect data from HTTP outgoing requests and SQL driver databases.

Another interesting feature is the usage of additional packages located under instrumentation. Each of these packages provide tracing for specific Go packages like the AWS SDK, Gorm and Fiber.

What's Next

  1. Tracer Options
  2. Tracing HTTP Outgoing Requests
  3. Tracing SQL Driver Databases
  4. Tracing an application running on Azure Container Apps
  5. Tracing Other Go Packages
  6. Instrumenting Code Manually
  7. Generic Serverless Agent