You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sqliteexporter/factory.go

82 lines
2.0 KiB

// Copyright 2023 William Perron. All rights reserved. MIT License.
package sqliteexporter
import (
"context"
10 months ago
"database/sql"
_ "embed"
"fmt"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.wperron.io/sqliteexporter/internal/metadata"
)
10 months ago
//go:embed migrations/20240120195122_init.up.sql
var initScript string
func NewFactory() exporter.Factory {
return exporter.NewFactory(
metadata.Type,
createDefaultConfig,
exporter.WithTraces(createTracesExporter, metadata.TracesStability),
exporter.WithMetrics(nil, metadata.MetricsStability),
exporter.WithLogs(nil, metadata.LogsStability),
)
}
func createDefaultConfig() component.Config {
return &Config{}
}
func createTracesExporter(
ctx context.Context,
set exporter.CreateSettings,
cfg component.Config,
) (exporter.Traces, error) {
conf := cfg.(*Config)
10 months ago
se, err := newSqliteExporter(conf)
if err != nil {
return nil, fmt.Errorf("failed to create sqlite exporter: %w", err)
}
return exporterhelper.NewTracesExporter(
ctx, set, cfg,
se.ConsumeTraces,
exporterhelper.WithStart(se.Start),
exporterhelper.WithShutdown(se.Shutdown),
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}),
)
}
10 months ago
func newSqliteExporter(cfg *Config) (*sqliteExporter, error) {
db, err := sql.Open("sqlite3", cfg.Path)
if err != nil {
return nil, fmt.Errorf("couldn't open sqlite3 database: %w", err)
}
// IMPORTANT: database/sql opens a connection pool by default, but sqlite
// only allows a single connection to be open at the same time.
db.SetMaxOpenConns(1)
10 months ago
tx, _ := db.Begin()
defer tx.Commit()
if _, err := tx.Exec(initScript); err != nil {
return nil, fmt.Errorf("failed to run migrations: %w", err)
}
return &sqliteExporter{
db: db,
}, nil
}
func NewSqliteSDKTraceExporter(cfg *Config) (sdktrace.SpanExporter, error) {
return newSqliteExporter(cfg)
}