Hi, can someone show a relatively complete and runnable example to show how to instrument a go program?

Hi, I find Elastic APM is a very good feature. I want to instrument my go program with the Elastic APM native API or opentracing API.

I read the article here:

I am still a little confused, I have configured the apm-server, es, kibana, when I run the code:

package main

import (
	"context"

	"go.elastic.co/apm"
	"go.elastic.co/apm/module/apmot"

	"github.com/opentracing/opentracing-go"
)

func main() {
	opentracing.SetGlobalTracer(apmot.New())

	parent, ctx := opentracing.StartSpanFromContext(context.Background(), "parent")
	child, _ := opentracing.StartSpanFromContext(ctx, "child")
	child.Finish()
	parent.Finish()

	// Transaction created through native API.
	transaction := apm.DefaultTracer.StartTransaction("GET /", "request")
	ctx = apm.ContextWithTransaction(context.Background(), transaction)

	// Span created through OpenTracing API will be a child of the transaction.
	otSpan, ctx := opentracing.StartSpanFromContext(ctx, "ot-span")
	_ = otSpan

	// Span created through the native API will be a child of the span created
	// above via the OpenTracing API.
	apmSpan, ctx := apm.StartSpan(ctx, "apm-span", "apm-span")
	_ = apmSpan
}

apm-server can receive request, while the APM page in kibana show nothing.

Can someone show me a runnable example? Thanks!

@hitzhangjie welcome to the forum, and thanks for trying out Elastic APM!

apm-server can receive request, while the APM page in kibana show nothing.

That's probably because your program exits before the events are sent to the server. Spans are buffered and sent in the background. You can force them to be sent by calling apm.DefaultTracer.Flush(nil) at the end of your main function.

Can someone show me a runnable example? Thanks!

package main

import (
        "context"

        "github.com/opentracing/opentracing-go"

        "go.elastic.co/apm"
        "go.elastic.co/apm/module/apmot"
)

func main() {
        opentracing.SetGlobalTracer(apmot.New())
        defer apm.DefaultTracer.Flush(nil)

        parent, ctx := opentracing.StartSpanFromContext(context.Background(), "parent")
        child, _ := opentracing.StartSpanFromContext(ctx, "child")
        child.Finish()
        parent.Finish()

        // Transaction created through native API.
        transaction := apm.DefaultTracer.StartTransaction("GET /", "request")
        ctx = apm.ContextWithTransaction(context.Background(), transaction)

        // Span created through OpenTracing API will be a child of the transaction.
        otSpan, ctx := opentracing.StartSpanFromContext(ctx, "ot-span")
        defer otSpan.Finish()

        // Span created through the native API will be a child of the span created
        // above via the OpenTracing API.
        apmSpan, ctx := apm.StartSpan(ctx, "apm-span", "apm-span")
        defer apmSpan.End()
}

I'd also recommend taking a look at https://www.elastic.co/blog/how-to-instrument-your-go-app-with-the-elastic-apm-go-agent

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.