convert to microsecond precision timestamps

main
William Perron 10 months ago
parent 2d2ce95fb8
commit 079fb8a16d
Signed by: wperron
GPG Key ID: BFDB4EF72D73C5F2

@ -8,8 +8,8 @@ CREATE TABLE IF NOT EXISTS spans(
"__duration" INTEGER, "__duration" INTEGER,
"name" TEXT, "name" TEXT,
"kind" TEXT, "kind" TEXT,
"start_time" INTEGER, "start_time" INTEGER, -- start_time is a microsecond precision unix timestamp
"end_time" INTEGER, "end_time" INTEGER, -- end_time is a microsecond precision unix timestamp
"status_code" INTEGER, "status_code" INTEGER,
"status_description" TEXT, "status_description" TEXT,
"attributes" TEXT, "attributes" TEXT,
@ -23,7 +23,7 @@ CREATE TABLE IF NOT EXISTS spans(
CREATE TABLE IF NOT EXISTS events( CREATE TABLE IF NOT EXISTS events(
"span_id" BLOB, "span_id" BLOB,
"timestamp" INTEGER, "timestamp" INTEGER, -- timestamp is a microsecond precision unix timestamp
"name" TEXT, "name" TEXT,
"attributes" TEXT, "attributes" TEXT,
"dropped_attributes_count" INTEGER, "dropped_attributes_count" INTEGER,

@ -0,0 +1,4 @@
ALTER TABLE spans DROP COLUMN start_time;
ALTER TABLE spans DROP COLUMN end_time;
ALTER TABLE spans RENAME COLUMN start_time_bak TO start_time;
ALTER TABLE spans RENAME COLUMN end_time_back TO end_time;

@ -0,0 +1,7 @@
-- Backup the start_time and end_time column values and convert existing
-- timestamps to microsecond precision
-- TODO(wperron): remove this before publishing
ALTER TABLE spans RENAME COLUMN start_time TO start_time_bak;
ALTER TABLE spans RENAME COLUMN end_time TO end_time;
ALTER TABLE spans ADD COLUMN (start_time INTEGER ) DEFAULT (start_time_bak * 1000);
ALTER TABLE spans ADD COLUMN (end_time INTEGER ) DEFAULT (end_time * 1000);

@ -6,6 +6,7 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"time"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component"
@ -198,8 +199,9 @@ func (e *sqliteExporter) ConsumeTraces(ctx context.Context, traces ptrace.Traces
dur.Microseconds(), dur.Microseconds(),
span.Name(), span.Name(),
span.Kind().String(), span.Kind().String(),
span.StartTimestamp().AsTime().Unix(), // Use microsecond precision for start and timestamps
span.EndTimestamp().AsTime().Unix(), unixMicro(span.StartTimestamp().AsTime()),
unixMicro(span.EndTimestamp().AsTime()),
span.Status().Code(), span.Status().Code(),
span.Status().Message(), span.Status().Message(),
attrs, attrs,
@ -228,7 +230,7 @@ func (e *sqliteExporter) ConsumeTraces(ctx context.Context, traces ptrace.Traces
_, err = stmt.ExecContext(ctx, _, err = stmt.ExecContext(ctx,
spanidbs, spanidbs,
event.Timestamp().AsTime().Unix(), unixMicro(event.Timestamp().AsTime()),
event.Name(), event.Name(),
attrs, attrs,
event.DroppedAttributesCount(), event.DroppedAttributesCount(),
@ -278,3 +280,7 @@ func (e *sqliteExporter) ConsumeTraces(ctx context.Context, traces ptrace.Traces
func pcommonMapAsJSON(m pcommon.Map) ([]byte, error) { func pcommonMapAsJSON(m pcommon.Map) ([]byte, error) {
return json.Marshal(m.AsRaw()) return json.Marshal(m.AsRaw())
} }
func unixMicro(t time.Time) int64 {
return t.UnixNano() / 1000
}

@ -152,8 +152,8 @@ func Test_ExporterExportSpan(t *testing.T) {
assert.Equal(t, 5000, duration) assert.Equal(t, 5000, duration)
assert.Equal(t, "span1", name) assert.Equal(t, "span1", name)
assert.Equal(t, ptrace.SpanKindServer.String(), kind) assert.Equal(t, ptrace.SpanKindServer.String(), kind)
assert.Equal(t, now.Add(-5*time.Millisecond).Unix(), int64(startTime)) assert.Equal(t, unixMicro(now.Add(-5*time.Millisecond)), int64(startTime))
assert.Equal(t, now.Unix(), int64(endTime)) assert.Equal(t, unixMicro(now), int64(endTime))
assert.Equal(t, int(ptrace.StatusCodeOk), statusCode) assert.Equal(t, int(ptrace.StatusCodeOk), statusCode)
assert.Equal(t, ptrace.StatusCodeOk.String(), statusDescription) assert.Equal(t, ptrace.StatusCodeOk.String(), statusDescription)
assert.Equal(t, "{\"http.method\":\"GET\",\"http.status_code\":200}", attributes) assert.Equal(t, "{\"http.method\":\"GET\",\"http.status_code\":200}", attributes)
@ -191,8 +191,8 @@ func Test_ExporterExportSpan(t *testing.T) {
assert.Equal(t, 3000, duration) assert.Equal(t, 3000, duration)
assert.Equal(t, "span2", name) assert.Equal(t, "span2", name)
assert.Equal(t, ptrace.SpanKindInternal.String(), kind) assert.Equal(t, ptrace.SpanKindInternal.String(), kind)
assert.Equal(t, now.Add(-4*time.Millisecond).Unix(), int64(startTime)) assert.Equal(t, unixMicro(now.Add(-4*time.Millisecond)), int64(startTime))
assert.Equal(t, now.Add(-1*time.Millisecond).Unix(), int64(endTime)) assert.Equal(t, unixMicro(now.Add(-1*time.Millisecond)), int64(endTime))
assert.Equal(t, int(ptrace.StatusCodeOk), statusCode) assert.Equal(t, int(ptrace.StatusCodeOk), statusCode)
assert.Equal(t, ptrace.StatusCodeOk.String(), statusDescription) assert.Equal(t, ptrace.StatusCodeOk.String(), statusDescription)
assert.Equal(t, "{\"custom.key\":\"custom-value\"}", attributes) assert.Equal(t, "{\"custom.key\":\"custom-value\"}", attributes)

Loading…
Cancel
Save