# How to get parent span id from span

**URL:** <https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430>\
**Category:** APM\
**Tags:** go\
**Created:** [March 6, 2021, 8:25am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430 "2021-03-06T08:25:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![peng\_xing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peng_xing/32/85082_2.png) [@peng\_xing](https://discuss.elastic.co/u/peng_xing)\
**Post date:** [March 6, 2021, 8:25am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/1 "2021-03-06T08:25:22Z")

</div>

I want to get some information from apm SpanContext to log in my logs, but I only can get transcationId, traceId and spanId, how to get the parent span id?

The code come from `apmlogrus`

```auto
// TraceContext returns a logrus.Fields containing the trace
// context of the transaction and span contained in ctx, if any.
func TraceContext(ctx context.Context) logrus.Fields {
	tx := apm.TransactionFromContext(ctx)
	if tx == nil {
		return nil
	}
	traceContext := tx.TraceContext()
	fields := logrus.Fields{
		FieldKeyTraceID: traceContext.Trace,
		FieldKeyTransactionID: traceContext.Span,
	}
	if span := apm.SpanFromContext(ctx); span != nil {
		fields[FieldKeySpanID] = span.TraceContext().Span
	}
	return fields
}

```

---

<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 7, 2021, 11:15pm UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/2 "2021-03-07T23:15:17Z")

</div>

Welcome to the forum @peng_xing!

It is not currently possible to extract the parent ID from a transaction or span. Could you explain why you want to log the parent span ID? We might consider adding way to access it.

---

<div class="post-metadata">

**Author:** ![peng\_xing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peng_xing/32/85082_2.png) [@peng\_xing](https://discuss.elastic.co/u/peng_xing)\
**Post date:** [March 8, 2021, 3:00am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/3 "2021-03-08T03:00:03Z")

</div>

I want to record the tracing information in the logs. The most import fields I think are traceId，spanId and parentSpanId. But TraceContext method only provides traceId and spanId fields.  
It is worth mentioning that we are not using elastic stack to store logs currently, only using elastic apm feature.

---

<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 8, 2021, 3:23am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/4 "2021-03-08T03:23:32Z")

</div>

> [@peng\_xing](#):
>
> It is worth mentioning that we are not using elastic stack to store logs currently, only using elastic apm feature.

OK. What you would would use the parent ID field in your logs for? The fields (`trace.id`, `span.id`) that `apmlogrus` adds to logs will give you enough information to correlate log records with their associated spans. You can then link from that span to its parent in ES.

---

<div class="post-metadata">

**Author:** ![peng\_xing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peng_xing/32/85082_2.png) [@peng\_xing](https://discuss.elastic.co/u/peng_xing)\
**Post date:** [March 8, 2021, 3:55am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/5 "2021-03-08T03:55:47Z")

</div>

> You can then link from that span to its parent in ES.

Yes, but I think maybe recording parent span id in the logs is a more convenient way, right?

I agreed that `trace.id` and `span.id` are enough information to correlate logs records. But I think the context of tracing should include parent id.

---

<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 8, 2021, 7:40am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/6 "2021-03-08T07:40:29Z")

</div>

I don't think we'll change the log integrations to add `parent.id` to the log fields, but we could add methods to Transaction and Span to fetch the parent ID. You could then write some custom code to inject `parent.id` into your logs. Would that work for you?

---

<div class="post-metadata">

**Author:** ![peng\_xing](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peng_xing/32/85082_2.png) [@peng\_xing](https://discuss.elastic.co/u/peng_xing)\
**Post date:** [March 8, 2021, 8:25am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/7 "2021-03-08T08:25:21Z")

</div>

> [@axw](#):
>
> You could then write some custom code to inject `parent.id` into your logs. Would that work for you?

That's great. Thank you

---

<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 8, 2021, 8:29am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/8 "2021-03-08T08:29:32Z")

</div>

I've opened an issue on GitHub to track this enhancement: [Introduce a way to extract the parent ID from Transactions and Spans · Issue #909 · elastic/apm-agent-go · GitHub](https://github.com/elastic/apm-agent-go/issues/909)

---

<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 29, 2021, 4:29am UTC](https://discuss.elastic.co/t/how-to-get-parent-span-id-from-span/266430/9 "2021-03-29T04:29:35Z")

</div>

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