<commit_before>// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package splunkhecexporter

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"go.opentelemetry.io/collector/consumer/pdata"
	"go.uber.org/zap"

	"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)

func Test_metricDataToSplunk(t *testing.T) {
	logger := zap.NewNop()

	unixSecs := int64(1574092046)
	unixNSecs := int64(11 * time.Millisecond)
	tsUnix := time.Unix(unixSecs, unixNSecs)
	ts := pdata.TimestampUnixNano(tsUnix.UnixNano())
	tsMSecs := timestampToSecondsWithMillisecondPrecision(ts)

	doubleVal := 1234.5678
	int64Val := int64(123)

	distributionBounds := []float64{1, 2, 4}
	distributionCounts := []uint64{4, 2, 3, 5}

	tests := []struct {
		name                     string
		metricsDataFn            func() pdata.Metrics
		wantSplunkMetrics        []splunk.Event
		wantNumDroppedTimeseries int
	}{
		{
			name: "nil_resource_metrics",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				metrics.ResourceMetrics().Append(rm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_instrumentation_library_metrics",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_metric",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				ilm.Metrics().Append(doubleGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_double_gauge_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				doubleGauge.InitEmpty()
				doubleGauge.SetName("gauge_double_with_dims")
				doubleGauge.SetDataType(pdata.MetricDataTypeDoubleGauge)
				ilm.Metrics().Append(doubleGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_int_gauge_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intGauge := pdata.NewMetric()
				intGauge.InitEmpty()
				intGauge.SetName("gauge_int_with_dims")
				intGauge.SetDataType(pdata.MetricDataTypeIntGauge)
				ilm.Metrics().Append(intGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_int_histogram_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intHistogram := pdata.NewMetric()
				intHistogram.InitEmpty()
				intHistogram.SetName("hist_int_with_dims")
				intHistogram.SetDataType(pdata.MetricDataTypeIntHistogram)
				ilm.Metrics().Append(intHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_double_histogram_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("hist_double_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_double_sum_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleSum := pdata.NewMetric()
				doubleSum.InitEmpty()
				doubleSum.SetName("double_sum_with_dims")
				doubleSum.SetDataType(pdata.MetricDataTypeDoubleSum)
				ilm.Metrics().Append(doubleSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_int_sum_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intSum := pdata.NewMetric()
				intSum.InitEmpty()
				intSum.SetName("int_sum_with_dims")
				intSum.SetDataType(pdata.MetricDataTypeIntSum)
				ilm.Metrics().Append(intSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_gauge_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				doubleGauge.InitEmpty()
				doubleGauge.SetName("gauge_double_with_dims")
				doubleGauge.SetDataType(pdata.MetricDataTypeDoubleGauge)
				doubleGauge.DoubleGauge().InitEmpty()
				doubleDataPt := pdata.NewDoubleDataPoint()
				doubleGauge.DoubleGauge().DataPoints().Append(doubleDataPt)
				ilm.Metrics().Append(doubleGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "int_gauge_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intGauge := pdata.NewMetric()
				intGauge.InitEmpty()
				intGauge.SetName("gauge_int_with_dims")
				intGauge.SetDataType(pdata.MetricDataTypeIntGauge)
				intGauge.IntGauge().InitEmpty()
				intDataPt := pdata.NewIntDataPoint()
				intGauge.IntGauge().DataPoints().Append(intDataPt)
				ilm.Metrics().Append(intGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "int_histogram_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intHistogram := pdata.NewMetric()
				intHistogram.InitEmpty()
				intHistogram.SetName("int_histogram_with_dims")
				intHistogram.SetDataType(pdata.MetricDataTypeIntHistogram)
				intHistogram.IntHistogram().InitEmpty()
				histogramDataPt := pdata.NewIntHistogramDataPoint()
				intHistogram.IntHistogram().DataPoints().Append(histogramDataPt)
				ilm.Metrics().Append(intHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_histogram_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("double_histogram_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				doubleHistogram.DoubleHistogram().InitEmpty()
				doubleHistogramDataPt := pdata.NewDoubleHistogramDataPoint()
				doubleHistogram.DoubleHistogram().DataPoints().Append(doubleHistogramDataPt)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "int_sum_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intSum := pdata.NewMetric()
				intSum.InitEmpty()
				intSum.SetName("int_sum_with_dims")
				intSum.SetDataType(pdata.MetricDataTypeIntSum)
				intSum.IntSum().InitEmpty()
				sumDataPt := pdata.NewIntDataPoint()
				intSum.IntSum().DataPoints().Append(sumDataPt)
				ilm.Metrics().Append(intSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_sum_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleSum := pdata.NewMetric()
				doubleSum.InitEmpty()
				doubleSum.SetName("double_sum_with_dims")
				doubleSum.SetDataType(pdata.MetricDataTypeDoubleSum)
				doubleSum.DoubleSum().InitEmpty()
				doubleDataPt := pdata.NewDoubleDataPoint()
				doubleSum.DoubleSum().DataPoints().Append(doubleDataPt)
				ilm.Metrics().Append(doubleSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "gauges",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				rm.Resource().Attributes().InsertString("service.name", "mysource")
				rm.Resource().Attributes().InsertString("host.hostname", "myhost")
				rm.Resource().Attributes().InsertString("com.splunk.sourcetype", "mysourcetype")
				rm.Resource().Attributes().InsertString("com.splunk.index", "myindex")

				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				doubleGauge.InitEmpty()
				doubleGauge.SetName("gauge_double_with_dims")
				doubleGauge.SetDataType(pdata.MetricDataTypeDoubleGauge)
				doubleGauge.DoubleGauge().InitEmpty()
				doubleDataPt := pdata.NewDoubleDataPoint()
				doubleDataPt.InitEmpty()
				doubleDataPt.SetValue(doubleVal)
				doubleDataPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				doubleGauge.DoubleGauge().DataPoints().Append(doubleDataPt)
				ilm.Metrics().Append(doubleGauge)

				intGauge := pdata.NewMetric()
				intGauge.InitEmpty()
				intGauge.SetName("gauge_int_with_dims")
				intGauge.SetDataType(pdata.MetricDataTypeIntGauge)
				intGauge.IntGauge().InitEmpty()
				intDataPt := pdata.NewIntDataPoint()
				intDataPt.InitEmpty()
				intDataPt.SetValue(int64Val)
				intDataPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				intDataPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				intGauge.IntGauge().DataPoints().Append(intDataPt)
				ilm.Metrics().Append(intGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{
				commonSplunkMetric("gauge_double_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.hostname", "service.name", "k0", "k1"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1"}, doubleVal, "mysource", "mysourcetype", "myindex", "myhost"),
				commonSplunkMetric("gauge_int_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.hostname", "service.name", "k0", "k1"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1"}, int64Val, "mysource", "mysourcetype", "myindex", "myhost"),
			},
		},

		{
			name: "double_histogram_no_upper_bound",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().InitEmpty()
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("double_histogram_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				doubleHistogram.DoubleHistogram().InitEmpty()
				doubleHistogramPt := pdata.NewDoubleHistogramDataPoint()
				doubleHistogramPt.InitEmpty()
				doubleHistogramPt.SetExplicitBounds(distributionBounds)
				doubleHistogramPt.SetBucketCounts([]uint64{4, 2, 3})
				doubleHistogramPt.SetSum(23)
				doubleHistogramPt.SetCount(7)
				doubleHistogramPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				doubleHistogram.DoubleHistogram().DataPoints().Append(doubleHistogramPt)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_histogram",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("double_histogram_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				doubleHistogram.DoubleHistogram().InitEmpty()
				doubleHistogramPt := pdata.NewDoubleHistogramDataPoint()
				doubleHistogramPt.InitEmpty()
				doubleHistogramPt.SetExplicitBounds(distributionBounds)
				doubleHistogramPt.SetBucketCounts(distributionCounts)
				doubleHistogramPt.SetSum(23)
				doubleHistogramPt.SetCount(7)
				doubleHistogramPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				doubleHistogram.DoubleHistogram().DataPoints().Append(doubleHistogramPt)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"metric_name:double_histogram_with_dims_sum": float64(23),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"metric_name:double_histogram_with_dims_count": uint64(7),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "1",
						"metric_name:double_histogram_with_dims_bucket": uint64(4),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "2",
						"metric_name:double_histogram_with_dims_bucket": uint64(6),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "4",
						"metric_name:double_histogram_with_dims_bucket": uint64(9),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "+Inf",
						"metric_name:double_histogram_with_dims_bucket": uint64(14),
					},
				},
			},
		},
		{
			name: "int_histogram_no_upper_bound",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().InitEmpty()
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()<commit_msg>splunkhecexporter: Fix lint (#1555)<commit_after>// Copyright 2020, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package splunkhecexporter

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"go.opentelemetry.io/collector/consumer/pdata"
	"go.uber.org/zap"

	"github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk"
)

func Test_metricDataToSplunk(t *testing.T) {
	logger := zap.NewNop()

	unixSecs := int64(1574092046)
	unixNSecs := int64(11 * time.Millisecond)
	tsUnix := time.Unix(unixSecs, unixNSecs)
	ts := pdata.TimestampUnixNano(tsUnix.UnixNano())
	tsMSecs := timestampToSecondsWithMillisecondPrecision(ts)

	doubleVal := 1234.5678
	int64Val := int64(123)

	distributionBounds := []float64{1, 2, 4}
	distributionCounts := []uint64{4, 2, 3, 5}

	tests := []struct {
		name                     string
		metricsDataFn            func() pdata.Metrics
		wantSplunkMetrics        []splunk.Event
		wantNumDroppedTimeseries int
	}{
		{
			name: "nil_resource_metrics",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				metrics.ResourceMetrics().Append(rm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_instrumentation_library_metrics",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_metric",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				ilm.Metrics().Append(doubleGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_double_gauge_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				doubleGauge.InitEmpty()
				doubleGauge.SetName("gauge_double_with_dims")
				doubleGauge.SetDataType(pdata.MetricDataTypeDoubleGauge)
				ilm.Metrics().Append(doubleGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_int_gauge_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intGauge := pdata.NewMetric()
				intGauge.InitEmpty()
				intGauge.SetName("gauge_int_with_dims")
				intGauge.SetDataType(pdata.MetricDataTypeIntGauge)
				ilm.Metrics().Append(intGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_int_histogram_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intHistogram := pdata.NewMetric()
				intHistogram.InitEmpty()
				intHistogram.SetName("hist_int_with_dims")
				intHistogram.SetDataType(pdata.MetricDataTypeIntHistogram)
				ilm.Metrics().Append(intHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_double_histogram_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("hist_double_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_double_sum_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleSum := pdata.NewMetric()
				doubleSum.InitEmpty()
				doubleSum.SetName("double_sum_with_dims")
				doubleSum.SetDataType(pdata.MetricDataTypeDoubleSum)
				ilm.Metrics().Append(doubleSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "nil_int_sum_value",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intSum := pdata.NewMetric()
				intSum.InitEmpty()
				intSum.SetName("int_sum_with_dims")
				intSum.SetDataType(pdata.MetricDataTypeIntSum)
				ilm.Metrics().Append(intSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_gauge_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				doubleGauge.InitEmpty()
				doubleGauge.SetName("gauge_double_with_dims")
				doubleGauge.SetDataType(pdata.MetricDataTypeDoubleGauge)
				doubleGauge.DoubleGauge().InitEmpty()
				doubleDataPt := pdata.NewDoubleDataPoint()
				doubleGauge.DoubleGauge().DataPoints().Append(doubleDataPt)
				ilm.Metrics().Append(doubleGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "int_gauge_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intGauge := pdata.NewMetric()
				intGauge.InitEmpty()
				intGauge.SetName("gauge_int_with_dims")
				intGauge.SetDataType(pdata.MetricDataTypeIntGauge)
				intGauge.IntGauge().InitEmpty()
				intDataPt := pdata.NewIntDataPoint()
				intGauge.IntGauge().DataPoints().Append(intDataPt)
				ilm.Metrics().Append(intGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "int_histogram_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intHistogram := pdata.NewMetric()
				intHistogram.InitEmpty()
				intHistogram.SetName("int_histogram_with_dims")
				intHistogram.SetDataType(pdata.MetricDataTypeIntHistogram)
				intHistogram.IntHistogram().InitEmpty()
				histogramDataPt := pdata.NewIntHistogramDataPoint()
				intHistogram.IntHistogram().DataPoints().Append(histogramDataPt)
				ilm.Metrics().Append(intHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_histogram_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("double_histogram_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				doubleHistogram.DoubleHistogram().InitEmpty()
				doubleHistogramDataPt := pdata.NewDoubleHistogramDataPoint()
				doubleHistogram.DoubleHistogram().DataPoints().Append(doubleHistogramDataPt)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "int_sum_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				intSum := pdata.NewMetric()
				intSum.InitEmpty()
				intSum.SetName("int_sum_with_dims")
				intSum.SetDataType(pdata.MetricDataTypeIntSum)
				intSum.IntSum().InitEmpty()
				sumDataPt := pdata.NewIntDataPoint()
				intSum.IntSum().DataPoints().Append(sumDataPt)
				ilm.Metrics().Append(intSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_sum_nil_data_point",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleSum := pdata.NewMetric()
				doubleSum.InitEmpty()
				doubleSum.SetName("double_sum_with_dims")
				doubleSum.SetDataType(pdata.MetricDataTypeDoubleSum)
				doubleSum.DoubleSum().InitEmpty()
				doubleDataPt := pdata.NewDoubleDataPoint()
				doubleSum.DoubleSum().DataPoints().Append(doubleDataPt)
				ilm.Metrics().Append(doubleSum)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "gauges",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				rm.Resource().Attributes().InsertString("service.name", "mysource")
				rm.Resource().Attributes().InsertString("host.hostname", "myhost")
				rm.Resource().Attributes().InsertString("com.splunk.sourcetype", "mysourcetype")
				rm.Resource().Attributes().InsertString("com.splunk.index", "myindex")

				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleGauge := pdata.NewMetric()
				doubleGauge.InitEmpty()
				doubleGauge.SetName("gauge_double_with_dims")
				doubleGauge.SetDataType(pdata.MetricDataTypeDoubleGauge)
				doubleGauge.DoubleGauge().InitEmpty()
				doubleDataPt := pdata.NewDoubleDataPoint()
				doubleDataPt.InitEmpty()
				doubleDataPt.SetValue(doubleVal)
				doubleDataPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				doubleGauge.DoubleGauge().DataPoints().Append(doubleDataPt)
				ilm.Metrics().Append(doubleGauge)

				intGauge := pdata.NewMetric()
				intGauge.InitEmpty()
				intGauge.SetName("gauge_int_with_dims")
				intGauge.SetDataType(pdata.MetricDataTypeIntGauge)
				intGauge.IntGauge().InitEmpty()
				intDataPt := pdata.NewIntDataPoint()
				intDataPt.InitEmpty()
				intDataPt.SetValue(int64Val)
				intDataPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				intDataPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				intGauge.IntGauge().DataPoints().Append(intDataPt)
				ilm.Metrics().Append(intGauge)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{
				commonSplunkMetric("gauge_double_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.hostname", "service.name", "k0", "k1"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1"}, doubleVal, "mysource", "mysourcetype", "myindex", "myhost"),
				commonSplunkMetric("gauge_int_with_dims", tsMSecs, []string{"com.splunk.index", "com.splunk.sourcetype", "host.hostname", "service.name", "k0", "k1"}, []interface{}{"myindex", "mysourcetype", "myhost", "mysource", "v0", "v1"}, int64Val, "mysource", "mysourcetype", "myindex", "myhost"),
			},
		},

		{
			name: "double_histogram_no_upper_bound",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("double_histogram_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				doubleHistogram.DoubleHistogram().InitEmpty()
				doubleHistogramPt := pdata.NewDoubleHistogramDataPoint()
				doubleHistogramPt.InitEmpty()
				doubleHistogramPt.SetExplicitBounds(distributionBounds)
				doubleHistogramPt.SetBucketCounts([]uint64{4, 2, 3})
				doubleHistogramPt.SetSum(23)
				doubleHistogramPt.SetCount(7)
				doubleHistogramPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				doubleHistogram.DoubleHistogram().DataPoints().Append(doubleHistogramPt)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{},
		},
		{
			name: "double_histogram",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
				ilm.InitEmpty()

				doubleHistogram := pdata.NewMetric()
				doubleHistogram.InitEmpty()
				doubleHistogram.SetName("double_histogram_with_dims")
				doubleHistogram.SetDataType(pdata.MetricDataTypeDoubleHistogram)
				doubleHistogram.DoubleHistogram().InitEmpty()
				doubleHistogramPt := pdata.NewDoubleHistogramDataPoint()
				doubleHistogramPt.InitEmpty()
				doubleHistogramPt.SetExplicitBounds(distributionBounds)
				doubleHistogramPt.SetBucketCounts(distributionCounts)
				doubleHistogramPt.SetSum(23)
				doubleHistogramPt.SetCount(7)
				doubleHistogramPt.SetTimestamp(pdata.TimestampUnixNano(tsUnix.UnixNano()))
				doubleHistogram.DoubleHistogram().DataPoints().Append(doubleHistogramPt)
				ilm.Metrics().Append(doubleHistogram)

				rm.InstrumentationLibraryMetrics().Append(ilm)
				return metrics
			},
			wantSplunkMetrics: []splunk.Event{
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"metric_name:double_histogram_with_dims_sum": float64(23),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"metric_name:double_histogram_with_dims_count": uint64(7),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "1",
						"metric_name:double_histogram_with_dims_bucket": uint64(4),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "2",
						"metric_name:double_histogram_with_dims_bucket": uint64(6),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "4",
						"metric_name:double_histogram_with_dims_bucket": uint64(9),
					},
				},
				{
					Host:       "unknown",
					Source:     "",
					SourceType: "",
					Event:      "metric",
					Time:       tsMSecs,
					Fields: map[string]interface{}{
						"k0": "v0",
						"k1": "v1",
						"le": "+Inf",
						"metric_name:double_histogram_with_dims_bucket": uint64(14),
					},
				},
			},
		},
		{
			name: "int_histogram_no_upper_bound",
			metricsDataFn: func() pdata.Metrics {

				metrics := pdata.NewMetrics()
				rm := pdata.NewResourceMetrics()
				rm.InitEmpty()
				metrics.ResourceMetrics().Append(rm)
				rm.Resource().Attributes().InsertString("k0", "v0")
				rm.Resource().Attributes().InsertString("k1", "v1")
				ilm := pdata.NewInstrumentationLibraryMetrics()
{'fraction_non_alphanumeric': 0.14537669487205626, 'fraction_numerical': 0.009888877561423183, 'mean_word_length': 4.008552463620117, 'pattern_counts': {'":': 44, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3628742', 'n_tokens_mistral': 15875, 'n_tokens_neox': 12813, 'n_words': 1734}