From c20f62ebc2b5a64b18992e623b518979f8566595 Mon Sep 17 00:00:00 2001 From: sakulali Date: Fri, 22 Sep 2023 04:48:56 +0800 Subject: [PATCH] [exporter/dynatrace] rename struct to keep expected `exporter.Factory` and pass checkapi (#27048) **Description:** Rename struct to keep expected exporter.Factory and pass checkapi **Link to tracking Issue:** https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/26304 **Testing:** go run cmd/checkapi/main.go . make chlog-validate go test for dynatraceexporter **Documentation:** --- .../dynatraceexporter-checkapi-26304.yaml | 27 +++++++++++++++++++ cmd/checkapi/allowlist.txt | 1 - exporter/dynatraceexporter/factory.go | 12 ++++----- .../dynatraceexporter/metrics_exporter.go | 24 ++++++++--------- .../metrics_exporter_test.go | 24 ++++++++--------- 5 files changed, 57 insertions(+), 31 deletions(-) create mode 100755 .chloggen/dynatraceexporter-checkapi-26304.yaml diff --git a/.chloggen/dynatraceexporter-checkapi-26304.yaml b/.chloggen/dynatraceexporter-checkapi-26304.yaml new file mode 100755 index 000000000000..d6bc3a813ca9 --- /dev/null +++ b/.chloggen/dynatraceexporter-checkapi-26304.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: breaking + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: dynatraceexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Rename struct to keep expected `exporter.Factory` and pass checkapi. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [26304] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [api] \ No newline at end of file diff --git a/cmd/checkapi/allowlist.txt b/cmd/checkapi/allowlist.txt index be72499f8a68..89c9d86b9fa7 100644 --- a/cmd/checkapi/allowlist.txt +++ b/cmd/checkapi/allowlist.txt @@ -1,6 +1,5 @@ connector/servicegraphconnector exporter/coralogixexporter -exporter/dynatraceexporter exporter/kafkaexporter exporter/pulsarexporter extension/observer/ecsobserver diff --git a/exporter/dynatraceexporter/factory.go b/exporter/dynatraceexporter/factory.go index 07996b07b2ef..9692345c554c 100644 --- a/exporter/dynatraceexporter/factory.go +++ b/exporter/dynatraceexporter/factory.go @@ -8,7 +8,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/confighttp" - exp "go.opentelemetry.io/collector/exporter" + "go.opentelemetry.io/collector/exporter" "go.opentelemetry.io/collector/exporter/exporterhelper" dtconfig "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/dynatraceexporter/config" @@ -17,11 +17,11 @@ import ( ) // NewFactory creates a Dynatrace exporter factory -func NewFactory() exp.Factory { - return exp.NewFactory( +func NewFactory() exporter.Factory { + return exporter.NewFactory( metadata.Type, createDefaultConfig, - exp.WithMetrics(createMetricsExporter, metadata.MetricsStability), + exporter.WithMetrics(createMetricsExporter, metadata.MetricsStability), ) } @@ -45,9 +45,9 @@ func createDefaultConfig() component.Config { // createMetricsExporter creates a metrics exporter based on this func createMetricsExporter( ctx context.Context, - set exp.CreateSettings, + set exporter.CreateSettings, c component.Config, -) (exp.Metrics, error) { +) (exporter.Metrics, error) { cfg := c.(*dtconfig.Config) diff --git a/exporter/dynatraceexporter/metrics_exporter.go b/exporter/dynatraceexporter/metrics_exporter.go index 8044191b66ea..a4576778e996 100644 --- a/exporter/dynatraceexporter/metrics_exporter.go +++ b/exporter/dynatraceexporter/metrics_exporter.go @@ -17,7 +17,7 @@ import ( "github.com/dynatrace-oss/dynatrace-metric-utils-go/metric/dimensions" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer/consumererror" - exp "go.opentelemetry.io/collector/exporter" + "go.opentelemetry.io/collector/exporter" "go.opentelemetry.io/collector/pdata/pmetric" "go.uber.org/zap" @@ -31,8 +31,8 @@ const ( cMaxAgeSeconds = 900 ) -// NewExporter exports to a Dynatrace Metrics v2 API -func newMetricsExporter(params exp.CreateSettings, cfg *config.Config) *exporter { +// newMetricsExporter exports to a Dynatrace Metrics v2 API +func newMetricsExporter(params exporter.CreateSettings, cfg *config.Config) *metricsExporter { var confDefaultDims []dimensions.Dimension for key, value := range cfg.DefaultDimensions { confDefaultDims = append(confDefaultDims, dimensions.NewDimension(key, value)) @@ -48,7 +48,7 @@ func newMetricsExporter(params exp.CreateSettings, cfg *config.Config) *exporter prevPts := ttlmap.New(cSweepIntervalSeconds, cMaxAgeSeconds) prevPts.Start() - return &exporter{ + return &metricsExporter{ settings: params.TelemetrySettings, cfg: cfg, defaultDimensions: defaultDimensions, @@ -57,8 +57,8 @@ func newMetricsExporter(params exp.CreateSettings, cfg *config.Config) *exporter } } -// exporter forwards metrics to a Dynatrace agent -type exporter struct { +// metricsExporter forwards metrics to a Dynatrace agent +type metricsExporter struct { settings component.TelemetrySettings cfg *config.Config client *http.Client @@ -82,7 +82,7 @@ func dimensionsFromTags(tags []string) dimensions.NormalizedDimensionList { return dimensions.NewNormalizedDimensionList(dims...) } -func (e *exporter) PushMetricsData(ctx context.Context, md pmetric.Metrics) error { +func (e *metricsExporter) PushMetricsData(ctx context.Context, md pmetric.Metrics) error { if e.isDisabled { return nil } @@ -109,7 +109,7 @@ func (e *exporter) PushMetricsData(ctx context.Context, md pmetric.Metrics) erro return nil } -func (e *exporter) serializeMetrics(md pmetric.Metrics) []string { +func (e *metricsExporter) serializeMetrics(md pmetric.Metrics) []string { var lines []string resourceMetrics := md.ResourceMetrics() @@ -154,7 +154,7 @@ var lastLog int64 // send sends a serialized metric batch to Dynatrace. // An error indicates all lines were dropped regardless of the returned number. -func (e *exporter) send(ctx context.Context, lines []string) error { +func (e *metricsExporter) send(ctx context.Context, lines []string) error { e.settings.Logger.Debug("Exporting", zap.Int("lines", len(lines))) if now := time.Now().Unix(); len(lines) > apiconstants.GetPayloadLinesLimit() && now-lastLog > 60 { @@ -185,7 +185,7 @@ func (e *exporter) send(ctx context.Context, lines []string) error { // send sends a serialized metric batch to Dynatrace. // An error indicates all lines were dropped regardless of the returned number. -func (e *exporter) sendBatch(ctx context.Context, lines []string) error { +func (e *metricsExporter) sendBatch(ctx context.Context, lines []string) error { message := strings.Join(lines, "\n") e.settings.Logger.Debug( "sending a batch of metric lines", @@ -278,7 +278,7 @@ func (e *exporter) sendBatch(ctx context.Context, lines []string) error { } // start starts the exporter -func (e *exporter) start(_ context.Context, host component.Host) (err error) { +func (e *metricsExporter) start(_ context.Context, host component.Host) (err error) { client, err := e.cfg.HTTPClientSettings.ToClient(host, e.settings) if err != nil { e.settings.Logger.Error("Failed to construct HTTP client", zap.Error(err)) @@ -290,7 +290,7 @@ func (e *exporter) start(_ context.Context, host component.Host) (err error) { return nil } -func (e *exporter) unmarshalResponseBody(resp *http.Response) (metricsResponse, error) { +func (e *metricsExporter) unmarshalResponseBody(resp *http.Response) (metricsResponse, error) { bodyBytes, err := io.ReadAll(resp.Body) responseBody := metricsResponse{} if err != nil { diff --git a/exporter/dynatraceexporter/metrics_exporter_test.go b/exporter/dynatraceexporter/metrics_exporter_test.go index c7624b38214d..be468800c6e8 100644 --- a/exporter/dynatraceexporter/metrics_exporter_test.go +++ b/exporter/dynatraceexporter/metrics_exporter_test.go @@ -159,7 +159,7 @@ func Test_exporter_PushMetricsData(t *testing.T) { } t.Run(test.name, func(t *testing.T) { - e := &exporter{ + e := &metricsExporter{ settings: test.fields.settings, cfg: test.fields.cfg, client: test.fields.client, @@ -305,7 +305,7 @@ func Test_SumMetrics(t *testing.T) { prevPts := ttlmap.New(cSweepIntervalSeconds, cMaxAgeSeconds) // set up the exporter - exp := &exporter{ + exp := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ APIToken: "token", @@ -368,7 +368,7 @@ func Test_exporter_PushMetricsData_EmptyPayload(t *testing.T) { noneMetric := metrics.AppendEmpty() noneMetric.SetName("none") - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: ts.URL}, @@ -405,7 +405,7 @@ func Test_exporter_PushMetricsData_isDisabled(t *testing.T) { intGaugeDataPoint.SetIntValue(10) intGaugeDataPoint.SetTimestamp(testTimestamp) - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: ts.URL}, @@ -431,7 +431,7 @@ func Test_exporter_send_BadRequest(t *testing.T) { })) defer ts.Close() - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: ts.URL}, @@ -456,7 +456,7 @@ func Test_exporter_send_Unauthorized(t *testing.T) { })) defer ts.Close() - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: ts.URL}, @@ -481,7 +481,7 @@ func Test_exporter_send_TooLarge(t *testing.T) { })) defer ts.Close() - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: ts.URL}, @@ -506,7 +506,7 @@ func Test_exporter_send_NotFound(t *testing.T) { })) defer ts.Close() - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ APIToken: "token", @@ -534,7 +534,7 @@ func Test_exporter_send_TooManyRequests(t *testing.T) { })) defer ts.Close() - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ APIToken: "token", @@ -557,7 +557,7 @@ func Test_exporter_send_MiscellaneousErrorCode(t *testing.T) { })) defer ts.Close() - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ APIToken: "token", @@ -588,7 +588,7 @@ func Test_exporter_send_chunking(t *testing.T) { })) defer ts.Close() - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: &config.Config{ HTTPClientSettings: confighttp.HTTPClientSettings{Endpoint: ts.URL}, @@ -672,7 +672,7 @@ func Test_exporter_PushMetricsData_Error(t *testing.T) { } t.Run(test.name, func(t *testing.T) { - e := &exporter{ + e := &metricsExporter{ settings: componenttest.NewNopTelemetrySettings(), cfg: test.fields.cfg, client: test.fields.client,