Skip to content

Commit

Permalink
STAC-21017 No Service name in resource table
Browse files Browse the repository at this point in the history
  • Loading branch information
rb3ckers committed Apr 17, 2024
1 parent 9664811 commit 5d808ec
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
11 changes: 1 addition & 10 deletions exporter/clickhousestsexporter/exporter_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -44,7 +38,6 @@ func newResourceModel(resource pcommon.Resource) (*resourceModel, error) {
resAttr := attributesToMap(resource.Attributes())
return &resourceModel{
resourceRef: refUUID,
serviceName: serviceName,
attributes: resAttr,
}, nil
}
Expand Down Expand Up @@ -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 {
Expand All @@ -123,15 +115,14 @@ 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
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 {
Expand Down
3 changes: 1 addition & 2 deletions exporter/clickhousestsexporter/exporter_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
7 changes: 6 additions & 1 deletion exporter/clickhousestsexporter/exporter_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down

0 comments on commit 5d808ec

Please sign in to comment.