-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.go
118 lines (97 loc) · 2.9 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2024 William Perron. All rights reserved. MIT License.
package sqliteexporter
import (
"context"
"database/sql"
"embed"
"errors"
"fmt"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source/iofs"
"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"
)
//go:embed migrations/*.sql
var migrations embed.FS
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)
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}),
)
}
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)
if err := doMigrate(db); err != nil {
return nil, err
}
return &sqliteExporter{
db: db,
}, nil
}
func NewSqliteSDKTraceExporter(cfg *Config) (sdktrace.SpanExporter, error) {
return newSqliteExporter(cfg)
}
func NewSqliteSDKTraceExporterWithDB(db *sql.DB) (sdktrace.SpanExporter, error) {
if err := doMigrate(db); err != nil {
return nil, err
}
return &sqliteExporter{
db: db,
}, nil
}
func doMigrate(db *sql.DB) error {
d, err := iofs.New(migrations, "migrations")
if err != nil {
return fmt.Errorf("failed to open iofs migration source: %w", err)
}
dr, err := sqlite3.WithInstance(db, &sqlite3.Config{
MigrationsTable: "schema_migrations_sqliteexporter",
})
if err != nil {
return fmt.Errorf("failed to initialize sqlite3 migrate driver: %w", err)
}
m, err := migrate.NewWithInstance("iofs", d, "sqliteexporter", dr)
if err != nil {
return fmt.Errorf("failed to initialize db migrate: %w", err)
}
err = m.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
return fmt.Errorf("failed to roll up migrations: %w", err)
}
return nil
}