Skip to content

Commit

Permalink
feat: correct id generation for log timestamp (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
nityanandagohain committed Aug 30, 2024
1 parent bb0d7ae commit 47e8ae2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions exporter/clickhouselogsexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type clickhouseLogsExporter struct {
db clickhouse.Conn
insertLogsSQL string
insertLogsSQLV2 string
ksuid ksuid.KSUID

logger *zap.Logger
cfg *Config
Expand Down Expand Up @@ -107,7 +106,6 @@ func newExporter(logger *zap.Logger, cfg *Config) (*clickhouseLogsExporter, erro
insertLogsSQLV2: insertLogsSQLV2,
logger: logger,
cfg: cfg,
ksuid: ksuid.New(),
usageCollector: collector,
wg: new(sync.WaitGroup),
closeChan: make(chan struct{}),
Expand Down Expand Up @@ -321,6 +319,12 @@ func (e *clickhouseLogsExporter) pushToClickhouse(ctx context.Context, ld plog.L
ts = ots
}

// generate the id from timestamp
id, err := ksuid.NewRandomWithTime(time.Unix(0, int64(ts)))
if err != nil {
return fmt.Errorf("IdGenError:%w", err)
}

lBucketStart := tsBucket(int64(ts/1000000000), DISTRIBUTED_LOGS_RESOURCE_V2_SECONDS)

if _, exists := resourcesSeen[int64(lBucketStart)]; !exists {
Expand All @@ -335,7 +339,7 @@ func (e *clickhouseLogsExporter) pushToClickhouse(ctx context.Context, ld plog.L
attributes := attributesToSlice(r.Attributes(), false)
attrsMap := attributesToMap(r.Attributes(), false)

err := addAttrsToTagStatement(tagStatement, "tag", attributes, e.useNewSchema)
err = addAttrsToTagStatement(tagStatement, "tag", attributes, e.useNewSchema)
if err != nil {
return err
}
Expand All @@ -348,7 +352,7 @@ func (e *clickhouseLogsExporter) pushToClickhouse(ctx context.Context, ld plog.L
fp,
ts,
ots,
e.ksuid.String(),
id.String(),
utils.TraceIDToHexOrEmptyString(r.TraceID()),
utils.SpanIDToHexOrEmptyString(r.SpanID()),
uint32(r.Flags()),
Expand All @@ -372,7 +376,7 @@ func (e *clickhouseLogsExporter) pushToClickhouse(ctx context.Context, ld plog.L
err = statement.Append(
ts,
ots,
e.ksuid.String(),
id.String(),
utils.TraceIDToHexOrEmptyString(r.TraceID()),
utils.SpanIDToHexOrEmptyString(r.SpanID()),
uint32(r.Flags()),
Expand All @@ -398,7 +402,6 @@ func (e *clickhouseLogsExporter) pushToClickhouse(ctx context.Context, ld plog.L
if err != nil {
return fmt.Errorf("StatementAppend:%w", err)
}
e.ksuid = e.ksuid.Next()
}
}
}
Expand Down

0 comments on commit 47e8ae2

Please sign in to comment.