Skip to content

Commit

Permalink
Add DB latency metric instrumentation for clickhouselogsexporter (#79)
Browse files Browse the repository at this point in the history
nityanandagohain authored Jan 11, 2023
1 parent 2ebab87 commit 7c2113f
Showing 2 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions exporter/clickhouselogsexporter/exporter.go
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ import (
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
@@ -164,10 +165,20 @@ func (e *clickhouseLogsExporter) pushLogsData(ctx context.Context, ld plog.Logs)
}
}
}

dbWriteStart := time.Now()
err = statement.Send()
stats.RecordWithTags(ctx,
[]tag.Mutator{
tag.Upsert(exporterKey, string(component.DataTypeLogs)),
tag.Upsert(tableKey, DISTRIBUTED_LOGS_TABLE),
},
writeLatencyMillis.M(int64(time.Since(dbWriteStart).Milliseconds())),
)
if err != nil {
return fmt.Errorf("StatementSend:%w", err)
}

duration := time.Since(start)
e.logger.Debug("insert logs", zap.Int("records", ld.LogRecordCount()),
zap.String("cost", duration.String()))
21 changes: 21 additions & 0 deletions exporter/clickhouselogsexporter/factory.go
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@ import (
"context"
"fmt"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/exporter/exporterhelper"
@@ -33,8 +36,26 @@ const (
migrationsFolder = "./migrations"
)

var (
writeLatencyMillis = stats.Int64("exporter_db_write_latency", "Time taken (in millis) for exporter to write batch", "ms")
exporterKey = tag.MustNewKey("exporter")
tableKey = tag.MustNewKey("table")
)

// NewFactory creates a factory for Elastic exporter.
func NewFactory() component.ExporterFactory {
writeLatencyDistribution := view.Distribution(100, 250, 500, 750, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000, 256000, 512000)

writeLatencyView := &view.View{
Name: "exporter_db_write_latency",
Measure: writeLatencyMillis,
Description: writeLatencyMillis.Description(),
TagKeys: []tag.Key{exporterKey, tableKey},
Aggregation: writeLatencyDistribution,
}

view.Register(writeLatencyView)

return component.NewExporterFactory(
typeStr,
createDefaultConfig,

0 comments on commit 7c2113f

Please sign in to comment.