Skip to content

Commit

Permalink
perf(chstorage): compute column list only once
Browse files Browse the repository at this point in the history
```
cpu: AMD Ryzen 9 5950X 16-Core Processor
                │   old.txt   │               new.txt               │
                │   sec/op    │   sec/op     vs base                │
InserterLogs-32   16.30m ± 1%   14.32m ± 1%  -12.16% (p=0.000 n=15)

                │   old.txt    │               new.txt                │
                │     B/op     │     B/op      vs base                │
InserterLogs-32   7.333Mi ± 0%   4.301Mi ± 0%  -41.34% (p=0.000 n=15)

                │   old.txt   │              new.txt               │
                │  allocs/op  │  allocs/op   vs base               │
InserterLogs-32   78.96k ± 0%   73.94k ± 0%  -6.36% (p=0.000 n=15)
```
  • Loading branch information
tdakkota committed Jul 19, 2024
1 parent 6eecac1 commit 753e84d
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions internal/chstorage/columns_logs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package chstorage

import (
"sync"

"github.com/ClickHouse/ch-go/proto"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
Expand Down Expand Up @@ -38,10 +40,12 @@ type logColumns struct {
scopeName *proto.ColLowCardinality[string]
scopeVersion *proto.ColLowCardinality[string]
scopeAttributes *Attributes

columns func() Columns
}

func newLogColumns() *logColumns {
return &logColumns{
c := &logColumns{
serviceName: new(proto.ColStr).LowCardinality(),
serviceInstanceID: new(proto.ColStr).LowCardinality(),
serviceNamespace: new(proto.ColStr).LowCardinality(),
Expand All @@ -53,6 +57,32 @@ func newLogColumns() *logColumns {
scopeVersion: new(proto.ColStr).LowCardinality(),
scopeAttributes: NewAttributes(colScope),
}
c.columns = sync.OnceValue(func() Columns {
return MergeColumns(Columns{
{Name: "service_instance_id", Data: c.serviceInstanceID},
{Name: "service_name", Data: c.serviceName},
{Name: "service_namespace", Data: c.serviceNamespace},

{Name: "timestamp", Data: c.timestamp},

{Name: "severity_number", Data: &c.severityNumber},
{Name: "severity_text", Data: c.severityText},

{Name: "trace_id", Data: &c.traceID},
{Name: "span_id", Data: &c.spanID},
{Name: "trace_flags", Data: &c.traceFlags},

{Name: "body", Data: &c.body},

{Name: "scope_name", Data: c.scopeName},
{Name: "scope_version", Data: c.scopeVersion},
},
c.attributes.Columns(),
c.scopeAttributes.Columns(),
c.resource.Columns(),
)
})
return c
}

func (c *logColumns) StaticColumns() []string {
Expand Down Expand Up @@ -153,32 +183,6 @@ func (c *logColumns) AddRow(r logstorage.Record) {
c.scopeAttributes.Append(r.ScopeAttrs)
}

func (c *logColumns) columns() Columns {
return MergeColumns(Columns{
{Name: "service_instance_id", Data: c.serviceInstanceID},
{Name: "service_name", Data: c.serviceName},
{Name: "service_namespace", Data: c.serviceNamespace},

{Name: "timestamp", Data: c.timestamp},

{Name: "severity_number", Data: &c.severityNumber},
{Name: "severity_text", Data: c.severityText},

{Name: "trace_id", Data: &c.traceID},
{Name: "span_id", Data: &c.spanID},
{Name: "trace_flags", Data: &c.traceFlags},

{Name: "body", Data: &c.body},

{Name: "scope_name", Data: c.scopeName},
{Name: "scope_version", Data: c.scopeVersion},
},
c.attributes.Columns(),
c.scopeAttributes.Columns(),
c.resource.Columns(),
)
}

func (c *logColumns) Input() proto.Input { return c.columns().Input() }
func (c *logColumns) Result() proto.Results { return c.columns().Result() }
func (c *logColumns) ChsqlResult() []chsql.ResultColumn { return c.columns().ChsqlResult() }
Expand All @@ -187,17 +191,19 @@ func (c *logColumns) Reset() { c.columns().Reset() }
type logAttrMapColumns struct {
name proto.ColStr // http_method
key proto.ColStr // http.method
}

func newLogAttrMapColumns() *logAttrMapColumns {
return &logAttrMapColumns{}
columns func() Columns
}

func (c *logAttrMapColumns) columns() Columns {
return []Column{
{Name: "name", Data: &c.name},
{Name: "key", Data: &c.key},
}
func newLogAttrMapColumns() *logAttrMapColumns {
c := &logAttrMapColumns{}
c.columns = sync.OnceValue(func() Columns {
return []Column{
{Name: "name", Data: &c.name},
{Name: "key", Data: &c.key},
}
})
return c
}

func (c *logAttrMapColumns) Input() proto.Input { return c.columns().Input() }
Expand Down

0 comments on commit 753e84d

Please sign in to comment.