Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use map to reduce writes to clickhouse tag tables #177

Merged
merged 2 commits into from
Oct 5, 2023
Merged
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
45 changes: 36 additions & 9 deletions exporter/clickhousetracesexporter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -167,19 +168,45 @@ func (w *SpanWriter) writeTagBatch(batchSpans []*Span) error {
w.logger.Error("Could not prepare batch for span attributes key table due to error: ", zap.Error(err), zap.Any("batch", logBatch))
return err
}
// create map of span attributes of key, tagType, dataType and isColumn to avoid duplicates in batch
mapOfSpanAttributeKeys := make(map[string]struct{})

// create map of span attributes of key, tagType, dataType, isColumn and value to avoid duplicates in batch
mapOfSpanAttributeValues := make(map[string]struct{})

for _, span := range batchSpans {
for _, spanAttribute := range span.SpanAttributes {

err = tagKeyStatement.Append(
spanAttribute.Key,
spanAttribute.TagType,
spanAttribute.DataType,
spanAttribute.IsColumn,
)
if err != nil {
w.logger.Error("Could not append span to tagKey Statement to batch due to error: ", zap.Error(err), zap.Object("span", span))
return err
// form a map key of span attribute key, tagType, dataType, isColumn and value
mapOfSpanAttributeValueKey := spanAttribute.Key + spanAttribute.TagType + spanAttribute.DataType + strconv.FormatBool(spanAttribute.IsColumn) + spanAttribute.StringValue + strconv.FormatFloat(spanAttribute.NumberValue, 'f', -1, 64)

// check if mapOfSpanAttributeValueKey already exists in map
_, ok := mapOfSpanAttributeValues[mapOfSpanAttributeValueKey]
if ok {
continue
}
// add mapOfSpanAttributeValueKey to map
mapOfSpanAttributeValues[mapOfSpanAttributeValueKey] = struct{}{}

// form a map key of span attribute key, tagType, dataType and isColumn
mapOfSpanAttributeKey := spanAttribute.Key + spanAttribute.TagType + spanAttribute.DataType + strconv.FormatBool(spanAttribute.IsColumn)

// check if mapOfSpanAttributeKey already exists in map
_, ok = mapOfSpanAttributeKeys[mapOfSpanAttributeKey]
if !ok {
err = tagKeyStatement.Append(
spanAttribute.Key,
spanAttribute.TagType,
spanAttribute.DataType,
spanAttribute.IsColumn,
)
if err != nil {
w.logger.Error("Could not append span to tagKey Statement to batch due to error: ", zap.Error(err), zap.Object("span", span))
return err
}
}
// add mapOfSpanAttributeKey to map
mapOfSpanAttributeKeys[mapOfSpanAttributeKey] = struct{}{}

if spanAttribute.DataType == "string" {
err = tagStatement.Append(
Expand Down
Loading