Skip to content

Commit

Permalink
[exporter/clickhouse] Fix Nil Pointer Exception on Metrics/Traces exp…
Browse files Browse the repository at this point in the history
…ort without service.name Resource Attribute (open-telemetry#37034)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description
Fixing Nil Pointer Exception regression introduced in
open-telemetry#35725

<!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes open-telemetry#37030

<!--Describe what testing was performed and which tests were added.-->
#### Testing
Unit test adjusted to include test Metric without `service.name`
Resource Attributes

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->
  • Loading branch information
Fiery-Fenix authored Jan 13, 2025
1 parent 32d50f5 commit 7901da4
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 34 deletions.
27 changes: 27 additions & 0 deletions .chloggen/fix_clickhouseexporter-metrics-npe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: clickhouseexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix Nil Pointer Exception on Metrics/Traces export without service.name Resource Attribute

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [37030]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
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
2 changes: 1 addition & 1 deletion exporter/clickhouseexporter/exporter_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func simpleMetrics(count int) pmetric.Metrics {
}

rm = metrics.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().PutStr("service.name", "demo 2")
// Removed service.name from second metric to test both with/without ServiceName cases
rm.Resource().Attributes().PutStr("Resource Attributes 2", "value2")
rm.Resource().SetDroppedAttributesCount(20)
rm.SetSchemaUrl("Resource SchemaUrl 2")
Expand Down
6 changes: 3 additions & 3 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,7 +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())
serviceName, _ := res.Attributes().Get(conventions.AttributeServiceName)
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 All @@ -96,7 +96,7 @@ func (e *tracesExporter) pushTraceData(ctx context.Context, td ptrace.Traces) er
r.TraceState().AsRaw(),
r.Name(),
r.Kind().String(),
serviceName.AsString(),
serviceName,
resAttr,
scopeName,
scopeVersion,
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,22 @@ func (e *expHistogramMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range e.expHistogramModels {
serviceName, _ := model.metadata.ResAttr.Get(conventions.AttributeServiceName)
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.AsString(),
serviceName,
model.metricName,
model.metricDescription,
model.metricUnit,
Expand Down
11 changes: 6 additions & 5 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,22 @@ func (g *gaugeMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range g.gaugeModels {
serviceName, _ := model.metadata.ResAttr.Get(conventions.AttributeServiceName)
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.AsString(),
serviceName,
model.metricName,
model.metricDescription,
model.metricUnit,
Expand Down
11 changes: 6 additions & 5 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,22 @@ func (h *histogramMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range h.histogramModel {
serviceName, _ := model.metadata.ResAttr.Get(conventions.AttributeServiceName)
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.AsString(),
serviceName,
model.metricName,
model.metricDescription,
model.metricUnit,
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))
})
}
11 changes: 6 additions & 5 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,22 @@ func (s *sumMetrics) insert(ctx context.Context, db *sql.DB) error {
}()

for _, model := range s.sumModel {
serviceName, _ := model.metadata.ResAttr.Get(conventions.AttributeServiceName)
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.AsString(),
serviceName,
model.metricName,
model.metricDescription,
model.metricUnit,
Expand Down
11 changes: 6 additions & 5 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,23 @@ func (s *summaryMetrics) insert(ctx context.Context, db *sql.DB) error {
_ = statement.Close()
}()
for _, model := range s.summaryModel {
serviceName, _ := model.metadata.ResAttr.Get(conventions.AttributeServiceName)
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.AsString(),
serviceName,
model.metricName,
model.metricDescription,
model.metricUnit,
Expand Down

0 comments on commit 7901da4

Please sign in to comment.