Skip to content

Commit 2ed5ecb

Browse files
authored
Merge pull request #10 from moov-io/feat-add-telemetry
meta: setup telemetry
2 parents 850365f + 1416fb9 commit 2ed5ecb

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

configs/config.default.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ RailMsgSQL:
66
Admin:
77
Bind:
88
Address: ":9696"
9+
Telemetry:
10+
ServiceName: "rail-msg-sql"

internal/search/service.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import (
99
"github.com/moov-io/ach"
1010
"github.com/moov-io/ach/cmd/achcli/describe/mask"
1111
"github.com/moov-io/base/log"
12+
"github.com/moov-io/base/telemetry"
1213
railmsgsql "github.com/moov-io/rail-msg-sql"
1314
"github.com/moov-io/rail-msg-sql/internal/achhelp"
1415
"github.com/moov-io/rail-msg-sql/internal/storage"
1516

1617
_ "github.com/ncruces/go-sqlite3/driver"
1718
_ "github.com/ncruces/go-sqlite3/embed"
19+
"go.opentelemetry.io/otel/attribute"
20+
"go.opentelemetry.io/otel/trace"
1821
)
1922

2023
type Service interface {
@@ -71,19 +74,24 @@ func (s *service) Close() error {
7174
}
7275

7376
func (s *service) IngestACHFiles(ctx context.Context, params storage.FilterParams) error {
77+
ctx, span := telemetry.StartSpan(ctx, "ingest-ach-files")
78+
defer span.End()
79+
7480
files, err := s.fileStorage.ListAchFiles(ctx, params)
7581
if err != nil {
7682
return fmt.Errorf("ingesting ach files: %w", err)
7783
}
7884

7985
for idx := range files {
80-
if files[idx].File == nil {
86+
file := files[idx]
87+
88+
if file.File == nil {
8189
continue
8290
}
8391

84-
err := s.IngestACHFile(ctx, files[idx].Filename, files[idx].File)
92+
err := s.IngestACHFile(ctx, file.Filename, file.File)
8593
if err != nil {
86-
return fmt.Errorf("ingesting %s failed: %w", files[idx].Filename, err)
94+
return fmt.Errorf("ingesting %s failed: %w", file.Filename, err)
8795
}
8896
}
8997

@@ -92,6 +100,11 @@ func (s *service) IngestACHFiles(ctx context.Context, params storage.FilterParam
92100

93101
// insertFile inserts an ACH file's header and control into ach_files.
94102
func (s *service) insertFile(ctx context.Context, tx *sql.Tx, filename string, file *ach.File) error {
103+
ctx, span := telemetry.StartSpan(ctx, "sql-insert-file", trace.WithAttributes(
104+
attribute.String("filename", filename),
105+
))
106+
defer span.End()
107+
95108
query := `
96109
INSERT OR IGNORE INTO ach_files (
97110
file_id,
@@ -412,6 +425,11 @@ func (s *service) IngestACHFile(ctx context.Context, filename string, file *ach.
412425
return errors.New("nil File")
413426
}
414427

428+
ctx, span := telemetry.StartSpan(ctx, "ingest-ach-file", trace.WithAttributes(
429+
attribute.String("filename", filename),
430+
))
431+
defer span.End()
432+
415433
// Make sure to normalize the IDs
416434
file = achhelp.PopulateIDs(file)
417435

@@ -493,6 +511,11 @@ func (s *service) Search(ctx context.Context, query string, params storage.Filte
493511
return nil, fmt.Errorf("query cannot be empty")
494512
}
495513

514+
ctx, span := telemetry.StartSpan(ctx, "search-files", trace.WithAttributes(
515+
attribute.String("sql.query", query),
516+
))
517+
defer span.End()
518+
496519
rows, err := s.db.QueryContext(ctx, query)
497520
if err != nil {
498521
return nil, fmt.Errorf("failed to execute query: %w", err)

internal/storage/repository.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import (
44
"context"
55
"fmt"
66
"path/filepath"
7+
"time"
78

89
"github.com/moov-io/ach"
910
"github.com/moov-io/ach-web-viewer/pkg/filelist"
11+
"github.com/moov-io/base/telemetry"
12+
13+
"go.opentelemetry.io/otel/attribute"
14+
"go.opentelemetry.io/otel/trace"
1015
)
1116

1217
type Repository struct {
@@ -32,6 +37,13 @@ func NewRepository(config Config) (*Repository, error) {
3237
}
3338

3439
func (r *Repository) ListAchFiles(ctx context.Context, params FilterParams) ([]File, error) {
40+
_, span := telemetry.StartSpan(ctx, "list-ach-files", trace.WithAttributes(
41+
attribute.String("filter.start", params.StartDate.Format(time.RFC3339)),
42+
attribute.String("filter.end", params.EndDate.Format(time.RFC3339)),
43+
attribute.String("filter.pattern", params.Pattern),
44+
))
45+
defer span.End()
46+
3547
resp, err := r.ach.GetFiles(filelist.ListOpts{
3648
StartDate: params.StartDate,
3749
EndDate: params.EndDate,

pkg/service/environment.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
package service
44

55
import (
6+
"context"
7+
"fmt"
8+
69
"github.com/moov-io/base/log"
710
"github.com/moov-io/base/stime"
11+
"github.com/moov-io/base/telemetry"
12+
railmsgsql "github.com/moov-io/rail-msg-sql"
813

914
"github.com/gorilla/mux"
1015
)
@@ -44,6 +49,16 @@ func NewEnvironment(env *Environment) (*Environment, error) {
4449
env.TimeService = stime.NewSystemTimeService()
4550
}
4651

52+
telemetryShutdownFunc, err := telemetry.SetupTelemetry(context.Background(), env.Config.Telemetry, railmsgsql.Version)
53+
if err != nil {
54+
return env, fmt.Errorf("setting up telemetry failed: %w", err)
55+
}
56+
prev := env.Shutdown
57+
env.Shutdown = func() {
58+
prev()
59+
telemetryShutdownFunc()
60+
}
61+
4762
// router
4863
if env.PublicRouter == nil {
4964
env.PublicRouter = mux.NewRouter()

pkg/service/model_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package service
44

55
import (
6+
"github.com/moov-io/base/telemetry"
67
"github.com/moov-io/rail-msg-sql/internal/search"
78
"github.com/moov-io/rail-msg-sql/internal/storage"
89
)
@@ -13,6 +14,8 @@ type GlobalConfig struct {
1314

1415
// Config defines all the configuration for the app
1516
type Config struct {
17+
Telemetry telemetry.Config
18+
1619
Servers ServerConfig
1720

1821
Search search.Config

0 commit comments

Comments
 (0)