Skip to content

Commit

Permalink
Introduce GetServiceName helper function
Browse files Browse the repository at this point in the history
Small optimizations on metrics export
  • Loading branch information
Fiery-Fenix committed Jan 7, 2025
1 parent 071892d commit 6aa3fc4
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 45 deletions.
6 changes: 1 addition & 5 deletions exporter/clickhouseexporter/exporter_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
_ "github.com/ClickHouse/clickhouse-go/v2" // For register database driver.
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/plog"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter/internal"
Expand Down Expand Up @@ -77,10 +76,7 @@ func (e *logsExporter) pushLogsData(ctx context.Context, ld plog.Logs) error {
res := logs.Resource()
resURL := logs.SchemaUrl()
resAttr := internal.AttributesToMap(res.Attributes())
var serviceName string
if v, ok := res.Attributes().Get(conventions.AttributeServiceName); ok {
serviceName = v.Str()
}
serviceName := internal.GetServiceName(res.Attributes())

for j := 0; j < logs.ScopeLogs().Len(); j++ {
rs := logs.ScopeLogs().At(j).LogRecords()
Expand Down
7 changes: 2 additions & 5 deletions exporter/clickhouseexporter/exporter_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/ptrace"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter/internal"
Expand Down Expand Up @@ -77,10 +76,8 @@ func (e *tracesExporter) pushTraceData(ctx context.Context, td ptrace.Traces) er
spans := td.ResourceSpans().At(i)
res := spans.Resource()
resAttr := internal.AttributesToMap(res.Attributes())
var serviceName string
if v, ok := res.Attributes().Get(conventions.AttributeServiceName); ok {
serviceName = v.AsString()
}
serviceName := internal.GetServiceName(res.Attributes())

for j := 0; j < spans.ScopeSpans().Len(); j++ {
rs := spans.ScopeSpans().At(j).Spans()
scopeName := spans.ScopeSpans().At(j).Scope().Name()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -130,20 +129,19 @@ func (e *expHistogramMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range e.expHistogramModels {
var serviceName string
if v, ok := model.metadata.ResAttr.Get(conventions.AttributeServiceName); ok {
serviceName = v.AsString()
}
resAttr := AttributesToMap(model.metadata.ResAttr)
scopeAttr := AttributesToMap(model.metadata.ScopeInstr.Attributes())
serviceName := GetServiceName(model.metadata.ResAttr)

for i := 0; i < model.expHistogram.DataPoints().Len(); i++ {
dp := model.expHistogram.DataPoints().At(i)
attrs, times, values, traceIDs, spanIDs := convertExemplars(dp.Exemplars())
_, err = statement.ExecContext(ctx,
AttributesToMap(model.metadata.ResAttr),
resAttr,
model.metadata.ResURL,
model.metadata.ScopeInstr.Name(),
model.metadata.ScopeInstr.Version(),
AttributesToMap(model.metadata.ScopeInstr.Attributes()),
scopeAttr,
model.metadata.ScopeInstr.DroppedAttributesCount(),
model.metadata.ScopeURL,
serviceName,
Expand Down
12 changes: 5 additions & 7 deletions exporter/clickhouseexporter/internal/gauge_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -109,20 +108,19 @@ func (g *gaugeMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range g.gaugeModels {
var serviceName string
if v, ok := model.metadata.ResAttr.Get(conventions.AttributeServiceName); ok {
serviceName = v.AsString()
}
resAttr := AttributesToMap(model.metadata.ResAttr)
scopeAttr := AttributesToMap(model.metadata.ScopeInstr.Attributes())
serviceName := GetServiceName(model.metadata.ResAttr)

for i := 0; i < model.gauge.DataPoints().Len(); i++ {
dp := model.gauge.DataPoints().At(i)
attrs, times, values, traceIDs, spanIDs := convertExemplars(dp.Exemplars())
_, err = statement.ExecContext(ctx,
AttributesToMap(model.metadata.ResAttr),
resAttr,
model.metadata.ResURL,
model.metadata.ScopeInstr.Name(),
model.metadata.ScopeInstr.Version(),
AttributesToMap(model.metadata.ScopeInstr.Attributes()),
scopeAttr,
model.metadata.ScopeInstr.DroppedAttributesCount(),
model.metadata.ScopeURL,
serviceName,
Expand Down
12 changes: 5 additions & 7 deletions exporter/clickhouseexporter/internal/histogram_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -121,20 +120,19 @@ func (h *histogramMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range h.histogramModel {
var serviceName string
if v, ok := model.metadata.ResAttr.Get(conventions.AttributeServiceName); ok {
serviceName = v.AsString()
}
resAttr := AttributesToMap(model.metadata.ResAttr)
scopeAttr := AttributesToMap(model.metadata.ScopeInstr.Attributes())
serviceName := GetServiceName(model.metadata.ResAttr)

for i := 0; i < model.histogram.DataPoints().Len(); i++ {
dp := model.histogram.DataPoints().At(i)
attrs, times, values, traceIDs, spanIDs := convertExemplars(dp.Exemplars())
_, err = statement.ExecContext(ctx,
AttributesToMap(model.metadata.ResAttr),
resAttr,
model.metadata.ResURL,
model.metadata.ScopeInstr.Name(),
model.metadata.ScopeInstr.Version(),
AttributesToMap(model.metadata.ScopeInstr.Attributes()),
scopeAttr,
model.metadata.ScopeInstr.DroppedAttributesCount(),
model.metadata.ScopeURL,
serviceName,
Expand Down
10 changes: 10 additions & 0 deletions exporter/clickhouseexporter/internal/metrics_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ClickHouse/clickhouse-go/v2/lib/column/orderedmap"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -175,6 +176,15 @@ func AttributesToMap(attributes pcommon.Map) column.IterableOrderedMap {
}, attributes.Len())
}

func GetServiceName(resAttr pcommon.Map) string {
var serviceName string
if v, ok := resAttr.Get(conventions.AttributeServiceName); ok {
serviceName = v.AsString()
}

return serviceName
}

func convertSliceToArraySet[T any](slice []T) clickhouse.ArraySet {
var set clickhouse.ArraySet
for _, item := range slice {
Expand Down
22 changes: 22 additions & 0 deletions exporter/clickhouseexporter/internal/metrics_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap/zaptest"
)

Expand Down Expand Up @@ -225,3 +226,24 @@ func Test_newPlaceholder(t *testing.T) {
expectStr := "(?,?,?,?,?),"
require.Equal(t, newPlaceholder(5), &expectStr)
}

func Test_GetServiceName(t *testing.T) {
t.Run("should return empty string on unset service.name", func(t *testing.T) {
require.Equal(t, "", GetServiceName(pcommon.NewMap()))
})
t.Run("should return correct string from service.name", func(t *testing.T) {
resAttr := pcommon.NewMap()
resAttr.PutStr(conventions.AttributeServiceName, "test-service")
require.Equal(t, "test-service", GetServiceName(resAttr))
})
t.Run("should return empty string on empty service.name", func(t *testing.T) {
resAttr := pcommon.NewMap()
resAttr.PutEmpty(conventions.AttributeServiceName)
require.Equal(t, "", GetServiceName(resAttr))
})
t.Run("should return string from non-string service.name", func(t *testing.T) {
resAttr := pcommon.NewMap()
resAttr.PutBool(conventions.AttributeServiceName, true)
require.Equal(t, "true", GetServiceName(resAttr))
})
}
12 changes: 5 additions & 7 deletions exporter/clickhouseexporter/internal/sum_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -113,20 +112,19 @@ func (s *sumMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range s.sumModel {
var serviceName string
if v, ok := model.metadata.ResAttr.Get(conventions.AttributeServiceName); ok {
serviceName = v.AsString()
}
resAttr := AttributesToMap(model.metadata.ResAttr)
scopeAttr := AttributesToMap(model.metadata.ScopeInstr.Attributes())
serviceName := GetServiceName(model.metadata.ResAttr)

for i := 0; i < model.sum.DataPoints().Len(); i++ {
dp := model.sum.DataPoints().At(i)
attrs, times, values, traceIDs, spanIDs := convertExemplars(dp.Exemplars())
_, err = statement.ExecContext(ctx,
AttributesToMap(model.metadata.ResAttr),
resAttr,
model.metadata.ResURL,
model.metadata.ScopeInstr.Name(),
model.metadata.ScopeInstr.Version(),
AttributesToMap(model.metadata.ScopeInstr.Attributes()),
scopeAttr,
model.metadata.ScopeInstr.DroppedAttributesCount(),
model.metadata.ScopeURL,
serviceName,
Expand Down
12 changes: 5 additions & 7 deletions exporter/clickhouseexporter/internal/summary_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
conventions "go.opentelemetry.io/collector/semconv/v1.27.0"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -103,21 +102,20 @@ func (s *summaryMetrics) insert(ctx context.Context, db *sql.DB) error {
_ = statement.Close()
}()
for _, model := range s.summaryModel {
var serviceName string
if v, ok := model.metadata.ResAttr.Get(conventions.AttributeServiceName); ok {
serviceName = v.AsString()
}
resAttr := AttributesToMap(model.metadata.ResAttr)
scopeAttr := AttributesToMap(model.metadata.ScopeInstr.Attributes())
serviceName := GetServiceName(model.metadata.ResAttr)

for i := 0; i < model.summary.DataPoints().Len(); i++ {
dp := model.summary.DataPoints().At(i)
quantiles, values := convertValueAtQuantile(dp.QuantileValues())

_, err = statement.ExecContext(ctx,
AttributesToMap(model.metadata.ResAttr),
resAttr,
model.metadata.ResURL,
model.metadata.ScopeInstr.Name(),
model.metadata.ScopeInstr.Version(),
AttributesToMap(model.metadata.ScopeInstr.Attributes()),
scopeAttr,
model.metadata.ScopeInstr.DroppedAttributesCount(),
model.metadata.ScopeURL,
serviceName,
Expand Down

0 comments on commit 6aa3fc4

Please sign in to comment.