Categorygithub.com/instana/go-sensor
modulepackage
1.67.1
Repository: https://github.com/instana/go-sensor.git
Documentation: pkg.go.dev

# README

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

# Packages

Package acceptor provides marshaling structs for Instana serverless acceptor API.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

BatchSize returns an opentracing.Tag to mark the span as a batched span representing similar span categories.
ContextWithSpan returns a new context.Context holding a reference to an active span.
DefaultOptions returns the default set of options to configure Instana sensor.
DefaultSecretsMatcher returns the default secrets matcher, that matches strings containing "key", "password" and "secret" ignoring the case.
DefaultTracerOptions returns the default set of options to configure a tracer.
EumSnippet generates javascript code to initialize JavaScript agent Deprecated: this snippet is outdated and this method will be removed in the next major version.
Flush forces Instana collector to send all buffered data to the agent.
FormatID converts an Instana ID to a value that can be used in context propagation (such as HTTP headers).
FormatLongID converts a 128-bit Instana ID passed in two quad words to an unsigned hex string suitable for context propagation.
GetCollector return the instance of instana Collector.
Header2ID calls instana.ParseID() and returns its result.
ID2Header calls instana.FormatID() and returns its result and a nil error.
InitCollector creates a new [Collector].
InitSensor initializes the sensor (without tracing) to begin collecting and reporting metrics.
InstrumentSQLDriver instruments provided database driver for use with `sql.Open()`.
NamedMatcher returns a secrets matcher supported by Instana host agent configuration See https://www.instana.com/docs/setup_and_manage/host_agent/configuration/#secrets.
NewRecorder initializes a new span recorder.
NewRootSpanContext initializes a new root span context issuing a new trace ID.
NewSensor creates a new [Sensor].
NewSensorWithTracer returns a new [Sensor] that uses provided tracer to report spans.
NewSpanContext initializes a new child span context from its parent.
NewSpanData initializes a new span data from tracer span.
NewTestRecorder initializes a new span recorder that keeps all collected until they are requested.
NewTracer initializes a new tracer with default options.
NewTracerWithEverything initializes and configures a new tracer.
NewTracerWithOptions initializes and configures a new tracer that collects and sends spans to the agent.
ParseDBConnDetails parses a database connection string (connStr) and returns a DbConnDetails struct.
ParseID converts an header context value into an Instana ID.
ParseLongID converts an header context value into a 128-bit Instana ID.
Ready returns whether the Instana collector is ready to collect and send data to the agent.
RoundTripper wraps an existing http.RoundTripper and injects the tracing headers into the outgoing request.
SendDefaultServiceEvent sends a default event which already contains the service and host.
SendHostEvent sends an event on the current host.
SendServiceEvent sends an event on a specific service.
SetLogger configures the default logger to be used by Instana go-sensor.
ShutdownCollector cleans up the collector and sensor reference.
ShutdownSensor cleans up the internal global sensor reference.
SpanFromContext retrieves previously stored active span from context.
SQLInstrumentAndOpen returns instrumented `*sql.DB`.
SQLOpen is a convenience wrapper for `sql.Open()` to use the instrumented version of a driver previosly registered using `instana.InstrumentSQLDriver()`.
StartMetrics initializes the communication with the agent.
StartSQLSpan creates a span based on DbConnDetails and a query, and attempts to detect which kind of database it belongs.
SuppressTracing returns an opentracing.Tag to mark the span and any of its child spans as not to be sent to the agent.
TracingHandlerFunc is an HTTP middleware that captures the tracing data and ensures trace context propagation via OpenTracing headers.
TracingNamedHandlerFunc is an HTTP middleware that similarly to instana.TracingHandlerFunc() captures the tracing data, while allowing to provide a unique route indetifier to be associated with each request.
WrapSQLConnector wraps an existing sql.Connector and instruments the DB calls made using it.

# Constants

AWS DynamoDB client span.
AWS Lambda entry span.
AWS Lambda invoke span.
AWS S3 client span.
AWS SNS client span.
AWS SQS client span.
Azure function span.
ContainsIgnoreCaseMatcher matches the substring in a string ignoring the case.
ContainsMatcher matches the substring in a string.
database names.
Cosmos client span.
database names.
Couchbase client span.
database names.
Valid log levels.
DefaultForceSpanSendAt is the default max number of spans to buffer before force sending them to the agent.
DefaultMaxBufferedSpans is the default span buffer size.
The kind of a span associated with an inbound call, this must be the first span in the trace.
EqualsIgnoreCaseMatcher matches the string exactly ignoring the case.
EqualsMatcher matches the string exactly.
Valid log levels.
The kind of a span associated with an outbound call, e.g.
FieldB OT Baggage header.
FieldL Level header.
FieldS Span ID header.
FieldSynthetic if set to 1, marks the call as synthetic, e.g.
FieldT Trace ID header.
Google Cloud PubSub client span.
Google Cloud Storage client span.
GraphQL client span.
GraphQL server span.
Registered types supported by Instana.
HTTP server and client spans.
Valid log levels.
The default kind for a span that is associated with a call within the same service.
Kafka consumer/producer span.
Logging span.
MaxLogsPerSpan The maximum number of logs allowed on a span.
MongoDB client span.
database names.
MySQL client span.
NoneMatcher does not match any string.
database names.
PostgreSQL client span.
RabbitMQ client span.
database names.
Redis client span.
RegexpMatcher matches the string using a set of regular expressions.
Registered types supported by Instana.
RPC server and client spans.
SDK span, a generic span containing arbitrary data.
Defaults for the Event API.
Defaults for the Event API.
Severity values for events sent to the instana agent.
Severity values for events sent to the instana agent.
Severity values for events sent to the instana agent.
SnapshotPeriod is the amount of time in seconds between snapshot reports.
Version is the version of Instana sensor.
Valid log levels.

# Variables

ErrAgentNotReady is an error returned for an attempt to communicate with an agent before the client announcement process is done.

# Structs

AWSDynamoDBSpanData represents the `data` section of a AWS DynamoDB span sent within an OT span document.
AWSDynamoDBSpanTags contains fields within the `data.sns` section of an OT span document.
AWSInvokeSpanTags contains fields within the `aws.lambda.invoke` section of an OT span document.
AWSLambdaCloudWatchEventTags contains fields within the `data.lambda.cw.events` section of an OT span document.
AWSLambdaCloudWatchLogsTags contains fields within the `data.lambda.cw.logs` section of an OT span document.
AWSLambdaCloudWatchSpanTags contains fields within the `data.lambda.cw` section of an OT span document.
AWSLambdaInvokeSpanData represents the `data` section of a AWS Invoke span sent within an OT span document.
AWSLambdaS3SpanTags contains fields within the `data.lambda.s3` section of an OT span document.
AWSLambdaSpanData is the base span data type for AWS Lambda entry spans.
AWSLambdaSpanTags contains fields within the `data.lambda` section of an OT span document.
AWSLambdaSQSSpanTags contains fields within the `data.lambda.sqs` section of an OT span document.
AWSS3EventTags represens metadata for an S3 event.
AWSS3SpanData represents the `data` section of a AWS S3 span sent within an OT span document.
AWSS3SpanTags contains fields within the `data.s3` section of an OT span document.
AWSSNSSpanData represents the `data` section of a AWS SNS span sent within an OT span document.
AWSSNSSpanTags contains fields within the `data.sns` section of an OT span document.
AWSSQSMessageTags represents span tags for an SQS message delivery.
AWSSQSSpanData represents the `data` section of a AWS SQS span sent within an OT span document.
AWSSQSSpanTags contains fields within the `data.sqs` section of an OT span document.
No description provided by the author
No description provided by the author
Collector is used to inject tracing information into requests.
CosmosSpanData represents the `data` section of a Cosmos client span.
CosmosSpanTags contains fields within the `data.cosmos` section of an OT span document.
CouchbaseSpanData represents the `data` section of a Couchbase client span.
CouchbaseSpanTags contains fields within the `data.couchbase` section of an OT span document.
CustomSpanData holds user-defined span tags.
DbConnDetails holds the details of a database connection parsed from a connection string.
EUMCorrelationData represents the data sent by the Instana End-User Monitoring script integrated into frontend.
EventData is the construct serialized for the host agent.
GCPPubSubSpanData represents the `data` section of a Google Cloud Pub/Sub span sent within an OT span document.
GCPPubSubSpanTags contains fields within the `data.gcps` section of an OT span document.
GCPStorageSpanData represents the `data` section of a Google Cloud Storage span sent within an OT span document.
GCPStorageSpanTags contains fields within the `data.gcs` section of an OT span document.
GraphQLSpanData represents the `data` section of a GraphQL span sent within an OT span document.
GraphQLSpanTags contains fields within the `data.graphql` section of an OT span document.
HTTPSpanData represents the `data` section of an HTTP span sent within an OT span document.
HTTPSpanTags contains fields within the `data.http` section of an OT span document.
KafkaSpanData represents the `data` section of an Kafka span sent within an OT span document.
KafkaSpanTags contains fields within the `data.kafka` section of an OT span document.
LogSpanData represents the `data` section of a logging span.
LogSpanTags contains fields within the `data.log` section of an OT span document.
MongoDBSpanData represents the `data` section of a MongoDB client span.
MongoDBSpanTags contains fields within the `data.mongo` section of an OT span document.
MySQLSpanData represents the `data` section of a MySQL client span.
MySQLSpanTags contains fields within the `data.mysql` section of an OT span document.
Options allows the user to configure the to-be-initialized sensor.
PostgreSQLSpanData represents the `data` section of a PostgreSQL client span.
PostgreSQLSpanTags contains fields within the `data.pg` section of an OT span document.
RabbitMQSpanData represents the `data` section of an RabbitMQ span.
RabbitMQSpanTags contains fields within the `data.rabbitmq` section.
Recorder accepts spans, processes and queues them for delivery to the backend.
RedisSpanData represents the `data` section of a Redis client span.
RedisSpanTags contains fields within the `data.redis` section of an OT span document.
RPCSpanData represents the `data` section of an RPC span sent within an OT span document.
RPCSpanTags contains fields within the `data.rpc` section of an OT span document.
SDKSpanData represents the `data` section of an SDK span sent within an OT span document.
SDKSpanTags contains fields within the `data.sdk` section of an OT span document.
Sensor is used to inject tracing information into requests.
SnapshotCollector returns a snapshot of Go runtime.
Span represents the OpenTracing span document to be sent to the agent.
SpanContext holds the basic Span metadata.
SpanData contains fields to be sent in the `data` section of an OT span document.
SpanReference is a reference to a span, possibly belonging to another trace, that is relevant to the span context.
TraceReference is used to reference a parent span.
TracerOptions carry the tracer configuration.

# Interfaces

No description provided by the author
LeveledLogger is an interface of a generic logger that support different message levels.
Matcher verifies whether a string meets predefined conditions.
No description provided by the author
A SpanRecorder handles all of the `RawSpan` data generated via an associated `Tracer` (see `NewStandardTracer`) instance.
Tags is the interface that applies new tags to ot.Tags map.
Tracer extends the opentracing.Tracer interface.
TracerLogger represents the Instana Go collector and is composed by a tracer, a logger and a reference to the legacy sensor.

# Type aliases

ContextSensitiveFunc is a SpanSensitiveFunc that also takes context.Context Deprecated: use instana.ContextWithSpan() and instana.SpanFromContext() to inject and retrieve spans.
EntityData struct to hold snapshot data.
MemoryS struct to hold snapshot data.
MetricsS struct to hold snapshot data.
RegisteredSpanType represents the span type supported by Instana.
SnapshotS struct to hold snapshot data.
SpanKind represents values of field `k` in OpenTracing span representation.
SpanSensitiveFunc is a function executed within a span context Deprecated: use instana.ContextWithSpan() and instana.SpanFromContext() to inject and retrieve spans.