Skip to content

Commit

Permalink
[enrichments]Add timestamp.us for span and txn (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar authored Aug 13, 2024
1 parent 40b4fd9 commit 5d34f44
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions enrichments/trace/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ScopeConfig struct {
// ElasticTransactionConfig configures the enrichment attributes for the
// spans which are identified as elastic transaction.
type ElasticTransactionConfig struct {
TimestampUs AttributeConfig `mapstructure:"timestamp_us"`
ID AttributeConfig `mapstructure:"id"`
Root AttributeConfig `mapstructure:"root"`
Name AttributeConfig `mapstructure:"name"`
Expand All @@ -54,6 +55,7 @@ type ElasticTransactionConfig struct {
// ElasticSpanConfig configures the enrichment attributes for the spans
// which are NOT identified as elastic transaction.
type ElasticSpanConfig struct {
TimestampUs AttributeConfig `mapstructure:"timestamp_us"`
Name AttributeConfig `mapstructure:"name"`
ProcessorEvent AttributeConfig `mapstructure:"processor_event"`
RepresentativeCount AttributeConfig `mapstructure:"representative_count"`
Expand Down Expand Up @@ -81,6 +83,7 @@ func Enabled() Config {
ServiceFrameworkVersion: AttributeConfig{Enabled: true},
},
Transaction: ElasticTransactionConfig{
TimestampUs: AttributeConfig{Enabled: true},
ID: AttributeConfig{Enabled: true},
Root: AttributeConfig{Enabled: true},
Name: AttributeConfig{Enabled: true},
Expand All @@ -92,6 +95,7 @@ func Enabled() Config {
RepresentativeCount: AttributeConfig{Enabled: true},
},
Span: ElasticSpanConfig{
TimestampUs: AttributeConfig{Enabled: true},
Name: AttributeConfig{Enabled: true},
ProcessorEvent: AttributeConfig{Enabled: true},
TypeSubtype: AttributeConfig{Enabled: true},
Expand Down
1 change: 1 addition & 0 deletions enrichments/trace/internal/elastic/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
AttributeServiceFrameworkVersion = "service.framework.version"

// span attributes
AttributeTimestampUs = "timestamp.us"
AttributeProcessorEvent = "processor.event"
AttributeTransactionID = "transaction.id"
AttributeTransactionRoot = "transaction.root"
Expand Down
26 changes: 26 additions & 0 deletions enrichments/trace/internal/elastic/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ func (s *spanEnrichmentContext) enrichTransaction(
span ptrace.Span,
cfg config.ElasticTransactionConfig,
) {
if cfg.TimestampUs.Enabled {
s.setTimestampUs(span)
}
if cfg.ID.Enabled {
span.Attributes().PutStr(AttributeTransactionID, span.SpanID().String())
}
Expand Down Expand Up @@ -197,6 +200,9 @@ func (s *spanEnrichmentContext) enrichSpan(
span ptrace.Span,
cfg config.ElasticSpanConfig,
) {
if cfg.TimestampUs.Enabled {
s.setTimestampUs(span)
}
if cfg.Name.Enabled {
span.Attributes().PutStr(AttributeSpanName, span.Name())
}
Expand Down Expand Up @@ -232,6 +238,26 @@ func (s *spanEnrichmentContext) normalizeAttributes() {
}
}

// setTimestampUs sets the attribute timestamp.us for span and
// span events. This is a temporary function to enable higher
// resolution timestamps in Elasticsearch. For more details see:
// https://github.com/elastic/opentelemetry-dev/issues/374.
//
// TODO (lahsivjar): If more enrichments need to be added
// for span events then consider having a separate flow for span
// events enrichment with its own configuration.
func (s *spanEnrichmentContext) setTimestampUs(span ptrace.Span) {
startTsUs := int64(span.StartTimestamp()) / 1000
span.Attributes().PutInt(AttributeTimestampUs, startTsUs)

events := span.Events()
for i := 0; i < events.Len(); i++ {
event := events.At(i)
eventTsUs := int64(event.Timestamp()) / 1000
event.Attributes().PutInt(AttributeTimestampUs, eventTsUs)
}
}

func (s *spanEnrichmentContext) setTxnType(span ptrace.Span) {
txnType := "unknown"
switch {
Expand Down
24 changes: 24 additions & 0 deletions enrichments/trace/internal/elastic/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
input: ptrace.NewSpan(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: int64(0),
AttributeTransactionRoot: true,
AttributeTransactionID: "",
AttributeTransactionName: "",
Expand All @@ -83,6 +84,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: int64(0),
AttributeTransactionRoot: true,
AttributeTransactionID: "",
AttributeTransactionName: "",
Expand All @@ -105,6 +107,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand Down Expand Up @@ -133,6 +136,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand Down Expand Up @@ -160,6 +164,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand Down Expand Up @@ -188,6 +193,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand Down Expand Up @@ -216,6 +222,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand All @@ -239,6 +246,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand All @@ -262,6 +270,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand All @@ -285,6 +294,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
}(),
config: config.Enabled().Transaction,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeTransactionRoot: true,
AttributeTransactionID: "0100000000000000",
AttributeTransactionName: "testtxn",
Expand Down Expand Up @@ -344,6 +354,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: int64(0),
AttributeSpanName: "",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -368,6 +379,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: int64(0),
AttributeSpanName: "",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -388,6 +400,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -414,6 +427,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand Down Expand Up @@ -447,6 +461,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand Down Expand Up @@ -478,6 +493,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand Down Expand Up @@ -505,6 +521,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -529,6 +546,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -555,6 +573,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -578,6 +597,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -602,6 +622,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand All @@ -626,6 +647,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand Down Expand Up @@ -656,6 +678,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand Down Expand Up @@ -692,6 +715,7 @@ func TestElasticSpanEnrich(t *testing.T) {
}(),
config: config.Enabled().Span,
enrichedAttrs: map[string]any{
AttributeTimestampUs: startTs.AsTime().UnixMicro(),
AttributeSpanName: "testspan",
AttributeProcessorEvent: "span",
AttributeSpanRepresentativeCount: float64(1),
Expand Down

0 comments on commit 5d34f44

Please sign in to comment.