diff --git a/exporter/elasticsearchexporter/config_test.go b/exporter/elasticsearchexporter/config_test.go index f3f848db2c90..45efc2c62cd6 100644 --- a/exporter/elasticsearchexporter/config_test.go +++ b/exporter/elasticsearchexporter/config_test.go @@ -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, @@ -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, @@ -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, diff --git a/exporter/elasticsearchexporter/exporter.go b/exporter/elasticsearchexporter/exporter.go index ebd3800858a2..b55e92c3ac35 100644 --- a/exporter/elasticsearchexporter/exporter.go +++ b/exporter/elasticsearchexporter/exporter.go @@ -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) @@ -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) } @@ -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( @@ -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) @@ -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) diff --git a/exporter/elasticsearchexporter/factory.go b/exporter/elasticsearchexporter/factory.go index 4783d430196a..003f88f25b9b 100644 --- a/exporter/elasticsearchexporter/factory.go +++ b/exporter/elasticsearchexporter/factory.go @@ -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, diff --git a/exporter/elasticsearchexporter/go.mod b/exporter/elasticsearchexporter/go.mod index 4fccebf594ab..22a526534851 100644 --- a/exporter/elasticsearchexporter/go.mod +++ b/exporter/elasticsearchexporter/go.mod @@ -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 diff --git a/exporter/elasticsearchexporter/go.sum b/exporter/elasticsearchexporter/go.sum index 2d0cc46d182f..f3350c9fc631 100644 --- a/exporter/elasticsearchexporter/go.sum +++ b/exporter/elasticsearchexporter/go.sum @@ -66,10 +66,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8= -github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is= -github.com/lestrrat-go/strftime v1.1.0 h1:gMESpZy44/4pXLO/m+sL0yBd1W6LjgjrrD4a68Gapyg= -github.com/lestrrat-go/strftime v1.1.0/go.mod h1:uzeIB52CeUJenCo1syghlugshMysrqUT51HlxphXVeI= github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= diff --git a/exporter/elasticsearchexporter/util.go b/exporter/elasticsearchexporter/util.go index e5b398082b3b..f3dd8693c363 100644 --- a/exporter/elasticsearchexporter/util.go +++ b/exporter/elasticsearchexporter/util.go @@ -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 } diff --git a/exporter/elasticsearchexporter/utils_test.go b/exporter/elasticsearchexporter/utils_test.go index fc320b36f073..bdcc4e8ad7d4 100644 --- a/exporter/elasticsearchexporter/utils_test.go +++ b/exporter/elasticsearchexporter/utils_test.go @@ -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) }