Skip to content

Commit

Permalink
[exporter/loki] remove opencensus usage (#30171)
Browse files Browse the repository at this point in the history
Fixes #30170

---------

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
Alex Boten authored Jan 22, 2024
1 parent 187c88a commit fc3966c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 37 deletions.
27 changes: 27 additions & 0 deletions .chloggen/codeboten_rm-census-loki.yaml
Original file line number Diff line number Diff line change
@@ -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: []
22 changes: 18 additions & 4 deletions exporter/lokiexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions exporter/lokiexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions exporter/lokiexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand All @@ -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()))
}
Expand All @@ -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()))
}
Expand All @@ -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()))
}
2 changes: 1 addition & 1 deletion exporter/lokiexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions exporter/lokiexporter/metrics.go

This file was deleted.

0 comments on commit fc3966c

Please sign in to comment.