Skip to content

Commit

Permalink
[exporter/elasticsearch] Drop strftime dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdaboot committed Dec 12, 2024
1 parent c2943c2 commit e83c552
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 61 deletions.
6 changes: 3 additions & 3 deletions exporter/elasticsearchexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestConfig(t *testing.T) {
LogstashFormat: LogstashFormatSettings{
Enabled: false,
PrefixSeparator: "-",
DateFormat: "%Y.%m.%d",
DateFormat: "2006.01.02",
},
Batcher: BatcherConfig{
FlushTimeout: 30 * time.Second,
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestConfig(t *testing.T) {
LogstashFormat: LogstashFormatSettings{
Enabled: false,
PrefixSeparator: "-",
DateFormat: "%Y.%m.%d",
DateFormat: "2006.01.02",
},
Batcher: BatcherConfig{
FlushTimeout: 30 * time.Second,
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestConfig(t *testing.T) {
LogstashFormat: LogstashFormatSettings{
Enabled: false,
PrefixSeparator: "-",
DateFormat: "%Y.%m.%d",
DateFormat: "2006.01.02",
},
Batcher: BatcherConfig{
FlushTimeout: 30 * time.Second,
Expand Down
33 changes: 7 additions & 26 deletions exporter/elasticsearchexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ func (e *elasticsearchExporter) pushLogRecord(
}

if e.logstashFormat.Enabled {
formattedIndex, err := generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
if err != nil {
return err
}
fIndex = formattedIndex
fIndex = generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
}

document, err := e.model.encodeLog(resource, resourceSchemaURL, record, scope, scopeSchemaURL)
Expand Down Expand Up @@ -212,10 +208,7 @@ func (e *elasticsearchExporter) pushMetricsData(
metric := scopeMetrics.Metrics().At(k)

upsertDataPoint := func(dp dataPoint) error {
fIndex, err := e.getMetricDataPointIndex(resource, scope, dp)
if err != nil {
return err
}
fIndex := e.getMetricDataPointIndex(resource, scope, dp)
if _, ok := resourceDocs[fIndex]; !ok {
resourceDocs[fIndex] = make(map[uint32]objmodel.Document)
}
Expand Down Expand Up @@ -323,20 +316,16 @@ func (e *elasticsearchExporter) getMetricDataPointIndex(
resource pcommon.Resource,
scope pcommon.InstrumentationScope,
dataPoint dataPoint,
) (string, error) {
) string {
fIndex := e.index
if e.dynamicIndex {
fIndex = routeDataPoint(dataPoint.Attributes(), scope.Attributes(), resource.Attributes(), fIndex, e.otel, scope.Name())
}

if e.logstashFormat.Enabled {
formattedIndex, err := generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
if err != nil {
return "", err
}
fIndex = formattedIndex
fIndex = generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
}
return fIndex, nil
return fIndex
}

func (e *elasticsearchExporter) pushTraceData(
Expand Down Expand Up @@ -404,11 +393,7 @@ func (e *elasticsearchExporter) pushTraceRecord(
}

if e.logstashFormat.Enabled {
formattedIndex, err := generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
if err != nil {
return err
}
fIndex = formattedIndex
fIndex = generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
}

document, err := e.model.encodeSpan(resource, resourceSchemaURL, span, scope, scopeSchemaURL)
Expand All @@ -434,11 +419,7 @@ func (e *elasticsearchExporter) pushSpanEvent(
}

if e.logstashFormat.Enabled {
formattedIndex, err := generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
if err != nil {
return err
}
fIndex = formattedIndex
fIndex = generateIndexWithLogstashFormat(fIndex, &e.logstashFormat, time.Now())
}

document := e.model.encodeSpanEvent(resource, resourceSchemaURL, span, spanEvent, scope, scopeSchemaURL)
Expand Down
2 changes: 1 addition & 1 deletion exporter/elasticsearchexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func createDefaultConfig() component.Config {
LogstashFormat: LogstashFormatSettings{
Enabled: false,
PrefixSeparator: "-",
DateFormat: "%Y.%m.%d",
DateFormat: "2006.01.02",
},
TelemetrySettings: TelemetrySettings{
LogRequestBody: false,
Expand Down
1 change: 0 additions & 1 deletion exporter/elasticsearchexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/elastic/go-structform v0.0.12
github.com/json-iterator/go v1.1.12
github.com/klauspost/compress v1.17.11
github.com/lestrrat-go/strftime v1.1.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.115.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.115.0
github.com/stretchr/testify v1.10.0
Expand Down
4 changes: 0 additions & 4 deletions exporter/elasticsearchexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 3 additions & 16 deletions exporter/elasticsearchexporter/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@
package elasticsearchexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter"

import (
"bytes"
"fmt"
"time"

"github.com/lestrrat-go/strftime"
)

func generateIndexWithLogstashFormat(index string, conf *LogstashFormatSettings, t time.Time) (string, error) {
func generateIndexWithLogstashFormat(index string, conf *LogstashFormatSettings, t time.Time) string {
if conf.Enabled {
partIndex := fmt.Sprintf("%s%s", index, conf.PrefixSeparator)
var buf bytes.Buffer
p, err := strftime.New(fmt.Sprintf("%s%s", partIndex, conf.DateFormat))
if err != nil {
return partIndex, err
}
if err = p.Format(&buf, t); err != nil {
return partIndex, err
}
index = buf.String()
return index + conf.PrefixSeparator + t.Format(conf.DateFormat)
}
return index, nil
return index
}
16 changes: 6 additions & 10 deletions exporter/elasticsearchexporter/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,23 +323,19 @@ func TestGetSuffixTime(t *testing.T) {
defaultCfg := createDefaultConfig().(*Config)
defaultCfg.LogstashFormat.Enabled = true
testTime := time.Date(2023, 12, 2, 10, 10, 10, 1, time.UTC)
index, err := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.NoError(t, err)
index := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.Equal(t, "logs-generic-default-2023.12.02", index)

defaultCfg.LogsIndex = "logstash"
defaultCfg.LogstashFormat.PrefixSeparator = "."
otelLogsIndex, err := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.NoError(t, err)
otelLogsIndex := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.Equal(t, "logstash.2023.12.02", otelLogsIndex)

defaultCfg.LogstashFormat.DateFormat = "%Y-%m-%d"
newOtelLogsIndex, err := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.NoError(t, err)
defaultCfg.LogstashFormat.DateFormat = "2006-01-02"
newOtelLogsIndex := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.Equal(t, "logstash.2023-12-02", newOtelLogsIndex)

defaultCfg.LogstashFormat.DateFormat = "%d/%m/%Y"
newOtelLogsIndexWithSpecDataFormat, err := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.NoError(t, err)
defaultCfg.LogstashFormat.DateFormat = "02/01/2006"
newOtelLogsIndexWithSpecDataFormat := generateIndexWithLogstashFormat(defaultCfg.LogsIndex, &defaultCfg.LogstashFormat, testTime)
assert.Equal(t, "logstash.02/12/2023", newOtelLogsIndexWithSpecDataFormat)
}

0 comments on commit e83c552

Please sign in to comment.