Skip to content

Commit

Permalink
feat(chstorage): instrument inserter using autometric
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jul 17, 2024
1 parent 941e22c commit 368705c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 52 deletions.
61 changes: 20 additions & 41 deletions internal/chstorage/inserter.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package chstorage

import (
"github.com/go-faster/errors"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"

"github.com/go-faster/errors"

"github.com/go-faster/oteldb/internal/autometric"
"github.com/go-faster/oteldb/internal/tracestorage"
)

Expand All @@ -17,19 +17,20 @@ type Inserter struct {
ch ClickhouseClient
tables Tables

// Logs metrics.
insertedRecords metric.Int64Counter
// Metrics metrics (it counts metrics insertion).
insertedPoints metric.Int64Counter
insertedHistograms metric.Int64Counter
insertedExemplars metric.Int64Counter
insertedMetricLabels metric.Int64Counter
// Trace metrics.
insertedSpans metric.Int64Counter
insertedTags metric.Int64Counter
// Common metrics.
inserts metric.Int64Counter

stats struct {
// Logs.
InsertedRecords metric.Int64Counter `name:"logs.inserted_records" description:"Number of inserted log records"`
// Metrics.
InsertedPoints metric.Int64Counter `name:"metrics.inserted_points" description:"Number of inserted points"`
InsertedHistograms metric.Int64Counter `name:"metrics.inserted_histograms" description:"Number of inserted exponential (native) histograms"`
InsertedExemplars metric.Int64Counter `name:"metrics.inserted_exemplars" description:"Number of inserted exemplars"`
InsertedMetricLabels metric.Int64Counter `name:"metrics.inserted_metric_labels" description:"Number of inserted metric labels"`
// Traces.
InsertedSpans metric.Int64Counter `name:"traces.inserted_spans" description:"Number of inserted spans"`
InsertedTags metric.Int64Counter `name:"traces.inserted_tags" description:"Number of inserted trace attributes"`
// Common.
Inserts metric.Int64Counter `name:"inserts" description:"Number of insert invocations"`
}
tracer trace.Tracer
}

Expand Down Expand Up @@ -69,32 +70,10 @@ func NewInserter(c ClickhouseClient, opts InserterOptions) (*Inserter, error) {
}

meter := opts.MeterProvider.Meter("chstorage.Inserter")
for _, desc := range []struct {
ptr *metric.Int64Counter
name string
description string
}{
// Logs.
{&inserter.insertedRecords, "chstorage.logs.inserted_records", "Number of inserted log records"},
// Metrics.
{&inserter.insertedPoints, "chstorage.metrics.inserted_points", "Number of inserted points"},
{&inserter.insertedHistograms, "chstorage.metrics.inserted_histograms", "Number of inserted exponential (native) histograms"},
{&inserter.insertedExemplars, "chstorage.metrics.inserted_exemplars", "Number of inserted exemplars"},
{&inserter.insertedMetricLabels, "chstorage.metrics.inserted_metric_labels", "Number of inserted metric labels"},
// Traces.
{&inserter.insertedSpans, "chstorage.traces.inserted_spans", "Number of inserted spans"},
{&inserter.insertedTags, "chstorage.traces.inserted_tags", "Number of inserted trace attributes"},
// Common.
{&inserter.inserts, "chstorage.inserts", "Number of insert invocations"},
} {
counter, err := meter.Int64Counter(desc.name,
metric.WithDescription(desc.description),
metric.WithUnit("1"),
)
if err != nil {
return nil, errors.Wrapf(err, "create %q", desc.name)
}
*desc.ptr = counter
if err := autometric.Init(meter, &inserter.stats, autometric.InitOptions{
Prefix: "chstorage.",
}); err != nil {
return nil, errors.Wrap(err, "init stats")
}

return inserter, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/chstorage/inserter_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (i *Inserter) InsertRecords(ctx context.Context, records []logstorage.Recor
if rerr != nil {
span.RecordError(rerr)
} else {
i.insertedRecords.Add(ctx, int64(len(records)))
i.inserts.Add(ctx, 1,
i.stats.InsertedRecords.Add(ctx, int64(len(records)))
i.stats.Inserts.Add(ctx, 1,
metric.WithAttributes(
attribute.String("chstorage.table", table),
attribute.String("chstorage.signal", "logs"),
Expand Down
10 changes: 5 additions & 5 deletions internal/chstorage/inserter_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func (i *Inserter) insertBatch(ctx context.Context, b *metricsBatch) (rerr error
if rerr != nil {
span.RecordError(rerr)
} else {
i.insertedPoints.Add(ctx, int64(b.points.value.Rows()))
i.insertedHistograms.Add(ctx, int64(b.expHistograms.count.Rows()))
i.insertedExemplars.Add(ctx, int64(b.exemplars.value.Rows()))
i.insertedMetricLabels.Add(ctx, int64(len(b.labels)))
i.stats.InsertedPoints.Add(ctx, int64(b.points.value.Rows()))
i.stats.InsertedHistograms.Add(ctx, int64(b.expHistograms.count.Rows()))
i.stats.InsertedExemplars.Add(ctx, int64(b.exemplars.value.Rows()))
i.stats.InsertedMetricLabels.Add(ctx, int64(len(b.labels)))

i.inserts.Add(ctx, 1, metric.WithAttributes(
i.stats.Inserts.Add(ctx, 1, metric.WithAttributes(
attribute.String("chstorage.signal", "metrics"),
))
}
Expand Down
8 changes: 4 additions & 4 deletions internal/chstorage/inserter_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (i *Inserter) InsertSpans(ctx context.Context, spans []tracestorage.Span) (
if rerr != nil {
span.RecordError(rerr)
} else {
i.insertedSpans.Add(ctx, int64(len(spans)))
i.inserts.Add(ctx, 1,
i.stats.InsertedSpans.Add(ctx, int64(len(spans)))
i.stats.Inserts.Add(ctx, 1,
metric.WithAttributes(
attribute.String("chstorage.table", table),
attribute.String("chstorage.signal", "traces"),
Expand Down Expand Up @@ -58,8 +58,8 @@ func (i *Inserter) InsertTags(ctx context.Context, tags map[tracestorage.Tag]str
if rerr != nil {
span.RecordError(rerr)
} else {
i.insertedTags.Add(ctx, int64(len(tags)))
i.inserts.Add(ctx, 1,
i.stats.InsertedTags.Add(ctx, int64(len(tags)))
i.stats.Inserts.Add(ctx, 1,
metric.WithAttributes(
attribute.String("chstorage.table", table),
attribute.String("chstorage.signal", "tags"),
Expand Down

0 comments on commit 368705c

Please sign in to comment.