func()

in enrichments/trace/internal/elastic/span.go [578:646]


func (s *spanEventEnrichmentContext) enrich(
	parentCtx *spanEnrichmentContext,
	se ptrace.SpanEvent,
	cfg config.SpanEventConfig,
) {
	// Extract top level span event information.
	s.exception = se.Name() == "exception"
	if s.exception {
		se.Attributes().Range(func(k string, v pcommon.Value) bool {
			switch k {
			case semconv25.AttributeExceptionEscaped:
				s.exceptionEscaped = v.Bool()
			case semconv25.AttributeExceptionType:
				s.exceptionType = v.Str()
			case semconv25.AttributeExceptionMessage:
				s.exceptionMessage = v.Str()
			}
			return true
		})
	}

	// Enrich span event attributes.
	if cfg.TimestampUs.Enabled {
		se.Attributes().PutInt(elasticattr.TimestampUs, getTimestampUs(se.Timestamp()))
	}
	if cfg.ProcessorEvent.Enabled && s.exception {
		se.Attributes().PutStr(elasticattr.ProcessorEvent, "error")
	}
	if s.exceptionType == "" && s.exceptionMessage == "" {
		// Span event does not represent an exception
		return
	}

	// Span event represents exception
	if cfg.ErrorID.Enabled {
		if id, err := newUniqueID(); err == nil {
			se.Attributes().PutStr(elasticattr.ErrorID, id)
		}
	}
	if cfg.ErrorExceptionHandled.Enabled {
		se.Attributes().PutBool(elasticattr.ErrorExceptionHandled, !s.exceptionEscaped)
	}
	if cfg.ErrorGroupingKey.Enabled {
		// See https://github.com/elastic/apm-data/issues/299
		hash := md5.New()
		// ignoring errors in hashing
		if s.exceptionType != "" {
			io.WriteString(hash, s.exceptionType)
		} else if s.exceptionMessage != "" {
			io.WriteString(hash, s.exceptionMessage)
		}
		se.Attributes().PutStr(elasticattr.ErrorGroupingKey, hex.EncodeToString(hash.Sum(nil)))
	}
	if cfg.ErrorGroupingName.Enabled {
		if s.exceptionMessage != "" {
			se.Attributes().PutStr(elasticattr.ErrorGroupingName, s.exceptionMessage)
		}
	}

	// Transaction type and sampled are added as span event enrichment only for errors
	if parentCtx.isTransaction && s.exception {
		if cfg.TransactionSampled.Enabled {
			se.Attributes().PutBool(elasticattr.TransactionSampled, parentCtx.getSampled())
		}
		if cfg.TransactionType.Enabled {
			se.Attributes().PutStr(elasticattr.TransactionType, parentCtx.getTxnType())
		}
	}
}