# Create a customized transaction for any function or code

**URL:** https://discuss.elastic.co/t/create-a-customized-transaction-for-any-function-or-code/274520
**Category:** APM
**Tags:** go
**Created:** [May 31, 2021, 2:20pm UTC](https://discuss.elastic.co/t/create-a-customized-transaction-for-any-function-or-code/274520 "2021-05-31T14:20:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![karim-pai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karim-pai/32/89528_2.png) [@karim-pai](https://discuss.elastic.co/u/karim-pai)
#### Post date: [May 31, 2021, 2:20pm UTC](https://discuss.elastic.co/t/create-a-customized-transaction-for-any-function-or-code/274520/1 "2021-05-31T14:20:02Z")

</div>

Hi,

Is it possible to create/trigger a transaction for any given part of the code (apart from http or gRPC request).  
I know it is possible in python [custom code instrumentation](https://www.elastic.co/guide/en/apm/agent/python/current/instrumenting-custom-code.html)

E.g. I want to instrument this function

```auto
func hello(){
transaction := apm.DefaultTracer.StartTransaction("hello", "code")
defer transaction.End()
fmt.Println("Hello World")
}

```

Apm agent does not start the transaction when the above function is called.

Thanks for your feedback

---

<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: [June 1, 2021, 1:39am UTC](https://discuss.elastic.co/t/create-a-customized-transaction-for-any-function-or-code/274520/2 "2021-06-01T01:39:49Z")

</div>

@karim-pai welcome to the forum!

Is your program short-lived? If so, you will need to run the tracer's [Flush](https://pkg.go.dev/go.elastic.co/apm#Tracer.Flush) method before exiting to give a chance for the events to be sent to the server first. Add `apm.DefaultTracer.Flush(nil)` somewhere in your program and see if that helps.

---

<div class="post-metadata">

### Author: ![karim-pai](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karim-pai/32/89528_2.png) [@karim-pai](https://discuss.elastic.co/u/karim-pai)
#### Post date: [June 1, 2021, 9:36am UTC](https://discuss.elastic.co/t/create-a-customized-transaction-for-any-function-or-code/274520/3 "2021-06-01T09:36:12Z")

</div>

Hi @axw ,

Indeed, flushing the tracer works. Go Apm agent does not send immediately the transaction to Apm Server as python agent does.

Thanks for your help.

---

<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: [June 22, 2021, 5:37am UTC](https://discuss.elastic.co/t/create-a-customized-transaction-for-any-function-or-code/274520/4 "2021-06-22T05:37:08Z")

</div>

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