diff --git a/.chloggen/codeboten_rm-census-loki.yaml b/.chloggen/codeboten_rm-census-loki.yaml new file mode 100755 index 000000000000..0a970455fe40 --- /dev/null +++ b/.chloggen/codeboten_rm-census-loki.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: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: lokiexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: migrate metrics to use OpenTelemetry + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [30170] + +# (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: [] diff --git a/exporter/lokiexporter/exporter.go b/exporter/lokiexporter/exporter.go index dddf72606099..fb9c3106a3d5 100644 --- a/exporter/lokiexporter/exporter.go +++ b/exporter/lokiexporter/exporter.go @@ -15,13 +15,14 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/snappy" - "go.opencensus.io/stats" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer/consumererror" "go.opentelemetry.io/collector/pdata/plog" + "go.opentelemetry.io/otel/metric" "go.uber.org/multierr" "go.uber.org/zap" + "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/lokiexporter/internal/metadata" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/loki" ) @@ -35,15 +36,28 @@ type lokiExporter struct { settings component.TelemetrySettings client *http.Client wg sync.WaitGroup + + lokiExporterFailedToSendLogRecordsDueToMissingLabels metric.Int64Counter } -func newExporter(config *Config, settings component.TelemetrySettings) *lokiExporter { +func newExporter(config *Config, settings component.TelemetrySettings) (*lokiExporter, error) { settings.Logger.Info("using the new Loki exporter") + count, err := metadata.Meter(settings).Int64Counter( + "lokiexporter_send_failed_due_to_missing_labels", + metric.WithDescription("Number of log records failed to send because labels were missing"), + metric.WithUnit("1"), + ) + + if err != nil { + return nil, err + } + return &lokiExporter{ config: config, settings: settings, - } + lokiExporterFailedToSendLogRecordsDueToMissingLabels: count, + }, nil } func (l *lokiExporter) pushLogData(ctx context.Context, ld plog.Logs) error { @@ -53,7 +67,7 @@ func (l *lokiExporter) pushLogData(ctx context.Context, ld plog.Logs) error { for tenant, request := range requests { err := l.sendPushRequest(ctx, tenant, request, ld) if isErrMissingLabels(err) { - stats.Record(ctx, lokiExporterFailedToSendLogRecordsDueToMissingLabels.M(int64(ld.LogRecordCount()))) + l.lokiExporterFailedToSendLogRecordsDueToMissingLabels.Add(ctx, int64(ld.LogRecordCount())) } errs = multierr.Append(errs, err) diff --git a/exporter/lokiexporter/factory.go b/exporter/lokiexporter/factory.go index d30a39f1c4c3..ea28b8b808f3 100644 --- a/exporter/lokiexporter/factory.go +++ b/exporter/lokiexporter/factory.go @@ -9,7 +9,6 @@ import ( "context" "time" - "go.opencensus.io/stats/view" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/confighttp" "go.opentelemetry.io/collector/config/configopaque" @@ -22,8 +21,6 @@ import ( // NewFactory creates a factory for the legacy Loki exporter. func NewFactory() exporter.Factory { - _ = view.Register(metricViews()...) - return exporter.NewFactory( metadata.Type, createDefaultConfig, @@ -53,7 +50,11 @@ func createDefaultConfig() component.Config { func createLogsExporter(ctx context.Context, set exporter.CreateSettings, config component.Config) (exporter.Logs, error) { exporterConfig := config.(*Config) - exp := newExporter(exporterConfig, set.TelemetrySettings) + exp, err := newExporter(exporterConfig, set.TelemetrySettings) + + if err != nil { + return nil, err + } return exporterhelper.NewLogsExporter( ctx, diff --git a/exporter/lokiexporter/factory_test.go b/exporter/lokiexporter/factory_test.go index 1e99fe0f71e6..66b6f9f1fc33 100644 --- a/exporter/lokiexporter/factory_test.go +++ b/exporter/lokiexporter/factory_test.go @@ -25,7 +25,8 @@ func TestExporter_new(t *testing.T) { Endpoint: validEndpoint, }, } - exp := newExporter(config, componenttest.NewNopTelemetrySettings()) + exp, err := newExporter(config, componenttest.NewNopTelemetrySettings()) + require.NoError(t, err) require.NotNil(t, exp) }) } @@ -36,7 +37,8 @@ func TestExporter_startReturnsNillWhenValidConfig(t *testing.T) { Endpoint: validEndpoint, }, } - exp := newExporter(config, componenttest.NewNopTelemetrySettings()) + exp, err := newExporter(config, componenttest.NewNopTelemetrySettings()) + require.NoError(t, err) require.NotNil(t, exp) require.NoError(t, exp.start(context.Background(), componenttest.NewNopHost())) } @@ -50,7 +52,8 @@ func TestExporter_startReturnsErrorWhenInvalidHttpClientSettings(t *testing.T) { }, }, } - exp := newExporter(config, componenttest.NewNopTelemetrySettings()) + exp, err := newExporter(config, componenttest.NewNopTelemetrySettings()) + require.NoError(t, err) require.NotNil(t, exp) require.Error(t, exp.start(context.Background(), componenttest.NewNopHost())) } @@ -61,7 +64,8 @@ func TestExporter_stopAlwaysReturnsNil(t *testing.T) { Endpoint: validEndpoint, }, } - exp := newExporter(config, componenttest.NewNopTelemetrySettings()) + exp, err := newExporter(config, componenttest.NewNopTelemetrySettings()) + require.NoError(t, err) require.NotNil(t, exp) require.NoError(t, exp.stop(context.Background())) } diff --git a/exporter/lokiexporter/go.mod b/exporter/lokiexporter/go.mod index b1ba978cf74b..cc1586036a55 100644 --- a/exporter/lokiexporter/go.mod +++ b/exporter/lokiexporter/go.mod @@ -10,7 +10,6 @@ require ( github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/loki v0.92.0 github.com/prometheus/common v0.46.0 github.com/stretchr/testify v1.8.4 - go.opencensus.io v0.24.0 go.opentelemetry.io/collector v0.92.1-0.20240118172122-8131d31601b8 go.opentelemetry.io/collector/component v0.92.1-0.20240118172122-8131d31601b8 go.opentelemetry.io/collector/config/confighttp v0.92.1-0.20240118172122-8131d31601b8 @@ -64,6 +63,7 @@ require ( github.com/prometheus/prometheus v0.48.1 // indirect github.com/prometheus/statsd_exporter v0.22.7 // indirect github.com/rs/cors v1.10.1 // indirect + go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/collector/config/configauth v0.92.1-0.20240118172122-8131d31601b8 // indirect go.opentelemetry.io/collector/config/configcompression v0.92.1-0.20240118172122-8131d31601b8 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.92.1-0.20240118172122-8131d31601b8 // indirect diff --git a/exporter/lokiexporter/metrics.go b/exporter/lokiexporter/metrics.go deleted file mode 100644 index 456bbe2307d4..000000000000 --- a/exporter/lokiexporter/metrics.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package lokiexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/lokiexporter" - -import ( - "go.opencensus.io/stats" - "go.opencensus.io/stats/view" -) - -var ( - lokiExporterFailedToSendLogRecordsDueToMissingLabels = stats.Int64("lokiexporter_send_failed_due_to_missing_labels", "Number of log records failed to send because labels were missing", stats.UnitDimensionless) -) - -func metricViews() []*view.View { - return []*view.View{ - { - Name: "lokiexporter_send_failed_due_to_missing_labels", - Description: "Number of log records failed to send because labels were missing", - Measure: lokiExporterFailedToSendLogRecordsDueToMissingLabels, - Aggregation: view.Count(), - }, - } -}