Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/storage/v2/clickhouse/sql/create_spans_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,10 @@ CREATE TABLE
resource_str_attributes Nested (key String, value String),
resource_complex_attributes Nested (key String, value String),
scope_name String,
scope_version String
scope_version String,
scope_bool_attributes Nested (key String, value Bool),
scope_double_attributes Nested (key String, value Float64),
scope_int_attributes Nested (key String, value Int64),
scope_str_attributes Nested (key String, value String),
scope_complex_attributes Nested (key String, value String),
) ENGINE = MergeTree PRIMARY KEY (trace_id)
32 changes: 31 additions & 1 deletion internal/storage/v2/clickhouse/sql/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ INSERT INTO
resource_complex_attributes.value,
scope_name,
scope_version,
scope_bool_attributes.key,
scope_bool_attributes.value,
scope_double_attributes.key,
scope_double_attributes.value,
scope_int_attributes.key,
scope_int_attributes.value,
scope_str_attributes.key,
scope_str_attributes.value,
scope_complex_attributes.key,
scope_complex_attributes.value
)
VALUES
(
Expand Down Expand Up @@ -106,6 +116,16 @@ VALUES
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)
`
Expand Down Expand Up @@ -169,7 +189,17 @@ SELECT
resource_complex_attributes.key,
resource_complex_attributes.value,
scope_name,
scope_version
scope_version,
scope_bool_attributes.key,
scope_bool_attributes.value,
scope_double_attributes.key,
scope_double_attributes.value,
scope_int_attributes.key,
scope_int_attributes.value,
scope_str_attributes.key,
scope_str_attributes.value,
scope_complex_attributes.key,
scope_complex_attributes.value,
FROM
spans
WHERE
Expand Down
102 changes: 51 additions & 51 deletions internal/storage/v2/clickhouse/tracestore/dbmodel/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func FromRow(storedSpan *SpanRow) ptrace.Traces {
rs.CopyTo(resource)

scope := scopeSpans.Scope()
sc := convertScope(storedSpan)
sc := convertScope(storedSpan, span)
sc.CopyTo(scope)

return trace
Expand All @@ -46,21 +46,21 @@ func convertResource(sr *SpanRow, spanForWarnings ptrace.Span) pcommon.Resource
resource.Attributes().PutStr(otelsemconv.ServiceNameKey, sr.ServiceName)
putAttributes(
resource.Attributes(),
&sr.ResourceAttributes,
spanForWarnings,
sr.ResourceAttributes.BoolKeys, sr.ResourceAttributes.BoolValues,
sr.ResourceAttributes.DoubleKeys, sr.ResourceAttributes.DoubleValues,
sr.ResourceAttributes.IntKeys, sr.ResourceAttributes.IntValues,
sr.ResourceAttributes.StrKeys, sr.ResourceAttributes.StrValues,
sr.ResourceAttributes.ComplexKeys, sr.ResourceAttributes.ComplexValues,
)
return resource
}

func convertScope(s *SpanRow) pcommon.InstrumentationScope {
func convertScope(sr *SpanRow, spanForWarnings ptrace.Span) pcommon.InstrumentationScope {
scope := ptrace.NewScopeSpans().Scope()
scope.SetName(s.ScopeName)
scope.SetVersion(s.ScopeVersion)
// TODO: populate attributes
scope.SetName(sr.ScopeName)
scope.SetVersion(sr.ScopeVersion)
putAttributes(
scope.Attributes(),
&sr.ScopeAttributes,
spanForWarnings,
)

return scope
}
Expand Down Expand Up @@ -94,27 +94,15 @@ func convertSpan(sr *SpanRow) (ptrace.Span, error) {

putAttributes(
span.Attributes(),
&sr.Attributes,
span,
sr.Attributes.BoolKeys, sr.Attributes.BoolValues,
sr.Attributes.DoubleKeys, sr.Attributes.DoubleValues,
sr.Attributes.IntKeys, sr.Attributes.IntValues,
sr.Attributes.StrKeys, sr.Attributes.StrValues,
sr.Attributes.ComplexKeys, sr.Attributes.ComplexValues,
)

for i, e := range sr.EventNames {
event := span.Events().AppendEmpty()
event.SetName(e)
event.SetTimestamp(pcommon.NewTimestampFromTime(sr.EventTimestamps[i]))
putAttributes(
event.Attributes(),
span,
sr.EventAttributes.BoolKeys[i], sr.EventAttributes.BoolValues[i],
sr.EventAttributes.DoubleKeys[i], sr.EventAttributes.DoubleValues[i],
sr.EventAttributes.IntKeys[i], sr.EventAttributes.IntValues[i],
sr.EventAttributes.StrKeys[i], sr.EventAttributes.StrValues[i],
sr.EventAttributes.ComplexKeys[i], sr.EventAttributes.ComplexValues[i],
)
putAttributes2D(event.Attributes(), &sr.EventAttributes, i, span)
}

for i, l := range sr.LinkTraceIDs {
Expand All @@ -133,49 +121,61 @@ func convertSpan(sr *SpanRow) (ptrace.Span, error) {
link.SetSpanID(pcommon.SpanID(spanID))
link.TraceState().FromRaw(sr.LinkTraceStates[i])

putAttributes(
link.Attributes(),
span,
sr.LinkAttributes.BoolKeys[i], sr.LinkAttributes.BoolValues[i],
sr.LinkAttributes.DoubleKeys[i], sr.LinkAttributes.DoubleValues[i],
sr.LinkAttributes.IntKeys[i], sr.LinkAttributes.IntValues[i],
sr.LinkAttributes.StrKeys[i], sr.LinkAttributes.StrValues[i],
sr.LinkAttributes.ComplexKeys[i], sr.LinkAttributes.ComplexValues[i],
)
putAttributes2D(link.Attributes(), &sr.LinkAttributes, i, span)
}

return span, nil
}

func putAttributes2D(
attrs pcommon.Map,
storedAttrs *Attributes2D,
idx int,
spanForWarnings ptrace.Span,
) {
putAttributes(
attrs,
&Attributes{
BoolKeys: storedAttrs.BoolKeys[idx],
BoolValues: storedAttrs.BoolValues[idx],
DoubleKeys: storedAttrs.DoubleKeys[idx],
DoubleValues: storedAttrs.DoubleValues[idx],
IntKeys: storedAttrs.IntKeys[idx],
IntValues: storedAttrs.IntValues[idx],
StrKeys: storedAttrs.StrKeys[idx],
StrValues: storedAttrs.StrValues[idx],
ComplexKeys: storedAttrs.ComplexKeys[idx],
ComplexValues: storedAttrs.ComplexValues[idx],
},
spanForWarnings,
)
}

func putAttributes(
attrs pcommon.Map,
storedAttrs *Attributes,
spanForWarnings ptrace.Span,
boolKeys []string, boolValues []bool,
doubleKeys []string, doubleValues []float64,
intKeys []string, intValues []int64,
strKeys []string, strValues []string,
complexKeys []string, complexValues []string,
) {
for i := 0; i < len(boolKeys); i++ {
attrs.PutBool(boolKeys[i], boolValues[i])
for i := 0; i < len(storedAttrs.BoolKeys); i++ {
attrs.PutBool(storedAttrs.BoolKeys[i], storedAttrs.BoolValues[i])
}
for i := 0; i < len(doubleKeys); i++ {
attrs.PutDouble(doubleKeys[i], doubleValues[i])
for i := 0; i < len(storedAttrs.DoubleKeys); i++ {
attrs.PutDouble(storedAttrs.DoubleKeys[i], storedAttrs.DoubleValues[i])
}
for i := 0; i < len(intKeys); i++ {
attrs.PutInt(intKeys[i], intValues[i])
for i := 0; i < len(storedAttrs.IntKeys); i++ {
attrs.PutInt(storedAttrs.IntKeys[i], storedAttrs.IntValues[i])
}
for i := 0; i < len(strKeys); i++ {
attrs.PutStr(strKeys[i], strValues[i])
for i := 0; i < len(storedAttrs.StrKeys); i++ {
attrs.PutStr(storedAttrs.StrKeys[i], storedAttrs.StrValues[i])
}
for i := 0; i < len(complexKeys); i++ {
if strings.HasPrefix(complexKeys[i], "@bytes@") {
decoded, err := base64.StdEncoding.DecodeString(complexValues[i])
for i := 0; i < len(storedAttrs.ComplexKeys); i++ {
if strings.HasPrefix(storedAttrs.ComplexKeys[i], "@bytes@") {
decoded, err := base64.StdEncoding.DecodeString(storedAttrs.ComplexValues[i])
if err != nil {
jptrace.AddWarnings(spanForWarnings, fmt.Sprintf("failed to decode bytes attribute %q: %s", complexKeys[i], err.Error()))
jptrace.AddWarnings(spanForWarnings, fmt.Sprintf("failed to decode bytes attribute %q: %s", storedAttrs.ComplexKeys[i], err.Error()))
continue
}
k := strings.TrimPrefix(complexKeys[i], "@bytes@")
k := strings.TrimPrefix(storedAttrs.ComplexKeys[i], "@bytes@")
attrs.PutEmptyBytes(k).FromRaw(decoded)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ func TestPutAttributes_Warnings(t *testing.T) {

putAttributes(
attributes,
&Attributes{
ComplexKeys: []string{"@bytes@bytes-key"},
ComplexValues: []string{"invalid-base64"},
},
span,
nil, nil,
nil, nil,
nil, nil,
nil, nil,
[]string{"@bytes@bytes-key"}, []string{"invalid-base64"},
)

_, ok := attributes.Get("bytes-key")
Expand Down
15 changes: 13 additions & 2 deletions internal/storage/v2/clickhouse/tracestore/dbmodel/spanrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ type SpanRow struct {
ResourceAttributes Attributes

// --- Scope ---
ScopeName string
ScopeVersion string
ScopeName string
ScopeVersion string
ScopeAttributes Attributes
}

type Attributes struct {
Expand Down Expand Up @@ -136,6 +137,16 @@ func ScanRow(rows driver.Rows) (*SpanRow, error) {
&sr.ResourceAttributes.ComplexValues,
&sr.ScopeName,
&sr.ScopeVersion,
&sr.ScopeAttributes.BoolKeys,
&sr.ScopeAttributes.BoolValues,
&sr.ScopeAttributes.DoubleKeys,
&sr.ScopeAttributes.DoubleValues,
&sr.ScopeAttributes.IntKeys,
&sr.ScopeAttributes.IntValues,
&sr.ScopeAttributes.StrKeys,
&sr.ScopeAttributes.StrValues,
&sr.ScopeAttributes.ComplexKeys,
&sr.ScopeAttributes.ComplexValues,
)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@
"ComplexValues": [["eyJkYl9jb250ZXh0IjoidXNlcmRiIn0="]]
},
"ServiceName": "db-service",
"ResourceAttributes": {
"BoolKeys": ["resource.persistent", "resource.pooled"],
"BoolValues": [true, true],
"DoubleKeys": ["resource.cpu_limit", "resource.memory_limit"],
"DoubleValues": [1.5, 512.0],
"IntKeys": ["resource.instance_id", "resource.max_connections"],
"IntValues": [67890, 100],
"StrKeys": ["service.name", "resource.host", "resource.database_type"],
"StrValues": ["db-service", "db-host-1", "postgresql"],
"ComplexKeys": ["@[email protected]"],
"ComplexValues": ["eyJkYl90eXBlIjoicG9zdGdyZXNxbCJ9"]
},
"ScopeName": "db-scope",
"ScopeVersion": "v1.0.0"
"ScopeVersion": "v1.0.0",
"ScopeAttributes": {
"BoolKeys": ["scope.enabled", "scope.persistent"],
"BoolValues": [true, false],
"DoubleKeys": ["scope.version_number", "scope.priority"],
"DoubleValues": [1.0, 0.8],
"IntKeys": ["scope.instance_count", "scope.max_spans"],
"IntValues": [5, 1000],
"StrKeys": ["scope.environment", "scope.component"],
"StrValues": ["production", "database"],
"ComplexKeys": ["@[email protected]"],
"ComplexValues": ["eyJzY29wZV90eXBlIjoiZGF0YWJhc2UifQ=="]
}
}
1 change: 1 addition & 0 deletions internal/storage/v2/clickhouse/tracestore/dbmodel/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func ToRow(
sr.appendLink(link)
}
appendAttributes(&sr.ResourceAttributes, resource.Attributes())
appendAttributes(&sr.ScopeAttributes, scope.Attributes())

return sr
}
Expand Down
13 changes: 13 additions & 0 deletions internal/storage/v2/clickhouse/tracestore/dbmodel/to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func createTestScope() pcommon.InstrumentationScope {
sc := pcommon.NewInstrumentationScope()
sc.SetName("test-scope")
sc.SetVersion("v1.0.0")
addTestAttributes(sc.Attributes())
return sc
}

Expand Down Expand Up @@ -155,5 +156,17 @@ func createExpectedSpanRow(now time.Time, duration time.Duration) *SpanRow {
},
ScopeName: "test-scope",
ScopeVersion: "v1.0.0",
ScopeAttributes: Attributes{
BoolKeys: []string{"bool_attr"},
BoolValues: []bool{true},
DoubleKeys: []string{"double_attr"},
DoubleValues: []float64{3.14},
IntKeys: []string{"int_attr"},
IntValues: []int64{42},
StrKeys: []string{"string_attr"},
StrValues: []string{"string_value"},
ComplexKeys: []string{"@bytes@bytes_attr"},
ComplexValues: []string{encodedBytes},
},
}
}
14 changes: 12 additions & 2 deletions internal/storage/v2/clickhouse/tracestore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func scanSpanRowFn() func(dest any, src *dbmodel.SpanRow) error {
if !ok {
return fmt.Errorf("expected []any for dest, got %T", dest)
}
if len(ptrs) != 58 {
return fmt.Errorf("expected 58 destination arguments, got %d", len(ptrs))
if len(ptrs) != 68 {
return fmt.Errorf("expected 68 destination arguments, got %d", len(ptrs))
}

values := []any{
Expand Down Expand Up @@ -89,6 +89,16 @@ func scanSpanRowFn() func(dest any, src *dbmodel.SpanRow) error {
&src.ResourceAttributes.ComplexValues,
&src.ScopeName,
&src.ScopeVersion,
&src.ScopeAttributes.BoolKeys,
&src.ScopeAttributes.BoolValues,
&src.ScopeAttributes.DoubleKeys,
&src.ScopeAttributes.DoubleValues,
&src.ScopeAttributes.IntKeys,
&src.ScopeAttributes.IntValues,
&src.ScopeAttributes.StrKeys,
&src.ScopeAttributes.StrValues,
&src.ScopeAttributes.ComplexKeys,
&src.ScopeAttributes.ComplexValues,
}

for i := range ptrs {
Expand Down
Loading
Loading