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

**URL:** <https://discuss.elastic.co/t/hi-can-someone-show-a-relatively-complete-and-runnable-example-to-show-how-to-instrument-a-go-program/222500>\
**Category:** APM\
**Tags:** go\
**Created:** [March 6, 2020, 6:41pm UTC](https://discuss.elastic.co/t/hi-can-someone-show-a-relatively-complete-and-runnable-example-to-show-how-to-instrument-a-go-program/222500 "2020-03-06T18:41:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hitzhangjie](https://avatars.discourse-cdn.com/v4/letter/h/3e96dc/32.png) [@hitzhangjie](https://discuss.elastic.co/u/hitzhangjie)\
**Post date:** [March 6, 2020, 6:41pm UTC](https://discuss.elastic.co/t/hi-can-someone-show-a-relatively-complete-and-runnable-example-to-show-how-to-instrument-a-go-program/222500/1 "2020-03-06T18:41:10Z")

</div>

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：

- [https://www.elastic.co/guide/en/apm/agent/go/current/custom-instrumentation.html](https://www.elastic.co/guide/en/apm/agent/go/current/custom-instrumentation.html)
- [https://www.elastic.co/guide/en/apm/agent/go/1.x/opentracing.html#opentracing-apm-tags](https://www.elastic.co/guide/en/apm/agent/go/1.x/opentracing.html#opentracing-apm-tags)

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![axw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/axw/32/28197_2.png) [@axw](https://discuss.elastic.co/u/axw)\
**Post date:** [March 9, 2020, 6:31am UTC](https://discuss.elastic.co/t/hi-can-someone-show-a-relatively-complete-and-runnable-example-to-show-how-to-instrument-a-go-program/222500/2 "2020-03-09T06:31:32Z")

</div>

@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 [How to instrument your Go application with the Elastic APM Go agent | Elastic Blog](https://www.elastic.co/blog/how-to-instrument-your-go-app-with-the-elastic-apm-go-agent)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 30, 2020, 2:31am UTC](https://discuss.elastic.co/t/hi-can-someone-show-a-relatively-complete-and-runnable-example-to-show-how-to-instrument-a-go-program/222500/3 "2020-03-30T02:31:35Z")

</div>

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