From 5d808ec079b307e66c7ba9907042b9138f078248 Mon Sep 17 00:00:00 2001 From: Remco Beckers Date: Wed, 17 Apr 2024 11:51:59 +0200 Subject: [PATCH] STAC-21017 No Service name in resource table --- exporter/clickhousestsexporter/exporter_resources.go | 11 +---------- .../clickhousestsexporter/exporter_resources_test.go | 3 +-- exporter/clickhousestsexporter/exporter_traces.go | 7 ++++++- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/exporter/clickhousestsexporter/exporter_resources.go b/exporter/clickhousestsexporter/exporter_resources.go index dabcde6..17e2e52 100644 --- a/exporter/clickhousestsexporter/exporter_resources.go +++ b/exporter/clickhousestsexporter/exporter_resources.go @@ -12,7 +12,6 @@ import ( "github.com/stackvista/sts-opentelemetry-collector/exporter/clickhousestsexporter/internal" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/pcommon" - conventions "go.opentelemetry.io/collector/semconv/v1.18.0" "go.uber.org/zap" ) @@ -26,15 +25,10 @@ type resourcesExporter struct { type resourceModel struct { resourceRef uuid.UUID - serviceName string attributes map[string]string } func newResourceModel(resource pcommon.Resource) (*resourceModel, error) { - var serviceName string - if v, ok := resource.Attributes().Get(conventions.AttributeServiceName); ok { - serviceName = v.Str() - } resourceRef := pdatautil.MapHash(resource.Attributes()) refUUID, err := uuid.FromBytes(resourceRef[:]) if err != nil { @@ -44,7 +38,6 @@ func newResourceModel(resource pcommon.Resource) (*resourceModel, error) { resAttr := attributesToMap(resource.Attributes()) return &resourceModel{ resourceRef: refUUID, - serviceName: serviceName, attributes: resAttr, }, nil } @@ -101,7 +94,6 @@ func (e *resourcesExporter) InsertResources(ctx context.Context, resources []*re _, err := resourceStatement.ExecContext(ctx, time.Now(), resource.resourceRef, - resource.serviceName, resource.attributes, ) if err != nil { @@ -123,7 +115,6 @@ const ( CREATE TABLE IF NOT EXISTS %s ( Timestamp DateTime64(9) CODEC(Delta, ZSTD(1)), ResourceRef UUID, - ServiceName LowCardinality(String) CODEC(ZSTD(1)), ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)), ) ENGINE = ReplacingMergeTree %s @@ -131,7 +122,7 @@ ORDER BY (ResourceRef, toUnixTimestamp(Timestamp)) SETTINGS index_granularity=512, ttl_only_drop_parts = 1; ` // language=ClickHouse SQL - insertResourcesSQLTemplate = `INSERT INTO %s (Timestamp, ResourceRef, ServiceName, ResourceAttributes) VALUES (?, ?, ?, ?)` + insertResourcesSQLTemplate = `INSERT INTO %s (Timestamp, ResourceRef, ResourceAttributes) VALUES (?, ?, ?)` ) func createResourcesTable(ctx context.Context, ttlDays uint, ttl time.Duration, tableName string, db *sql.DB) error { diff --git a/exporter/clickhousestsexporter/exporter_resources_test.go b/exporter/clickhousestsexporter/exporter_resources_test.go index 9474f65..441953a 100644 --- a/exporter/clickhousestsexporter/exporter_resources_test.go +++ b/exporter/clickhousestsexporter/exporter_resources_test.go @@ -39,8 +39,7 @@ func TestExporter_pushResourcesData(t *testing.T) { t.Run("check insert resources with service name and attributes", func(t *testing.T) { initClickhouseTestServer(t, func(query string, values []driver.Value) error { if strings.HasPrefix(query, "INSERT") && strings.Contains(query, "otel_resources") { - require.Equal(t, "test-service-0", values[2]) - require.Equal(t, map[string]string{"key": "value", "service.name": "test-service-0"}, values[3]) + require.Equal(t, map[string]string{"key": "value", "service.name": "test-service-0"}, values[2]) } return nil }) diff --git a/exporter/clickhousestsexporter/exporter_traces.go b/exporter/clickhousestsexporter/exporter_traces.go index 93f84dc..4854aae 100644 --- a/exporter/clickhousestsexporter/exporter_traces.go +++ b/exporter/clickhousestsexporter/exporter_traces.go @@ -14,6 +14,7 @@ import ( "github.com/stackvista/sts-opentelemetry-collector/exporter/clickhousestsexporter/internal" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/ptrace" + conventions "go.opentelemetry.io/collector/semconv/v1.18.0" "go.uber.org/zap" ) @@ -104,6 +105,10 @@ func (e *tracesExporter) pushTraceData(ctx context.Context, td ptrace.Traces) er return err } resources = append(resources, res) + var serviceName string + if v, ok := spans.Resource().Attributes().Get(conventions.AttributeServiceName); ok { + serviceName = v.Str() + } for j := 0; j < spans.ScopeSpans().Len(); j++ { rs := spans.ScopeSpans().At(j).Spans() @@ -125,7 +130,7 @@ func (e *tracesExporter) pushTraceData(ctx context.Context, td ptrace.Traces) er r.TraceState().AsRaw(), r.Name(), SpanKindStr(r.Kind()), - res.serviceName, + serviceName, scopeName, scopeVersion, spanAttr,