1818package pipeline
1919
2020import (
21+ "context"
2122 "encoding/json"
2223 "fmt"
2324 "net/http"
2425
25- "github.com/moov-io/achgateway/internal/service"
2626 "github.com/moov-io/base/log"
27+ "github.com/moov-io/base/telemetry"
28+
29+ "go.opentelemetry.io/otel/attribute"
30+ "go.opentelemetry.io/otel/trace"
2731)
2832
2933type manuallyTriggeredCutoff struct {
30- C chan error
34+ C chan error
35+ ctx context.Context
3136}
3237
3338type manualCutoffBody struct {
@@ -63,16 +68,28 @@ func (fr *FileReceiver) triggerManualCutoff() http.HandlerFunc {
6368 return
6469 }
6570
71+ ctx , span := telemetry .StartSpan (r .Context (), "api-manual-cutoff" , trace .WithAttributes (
72+ attribute .StringSlice ("achgateway.shards" , body .ShardNames ),
73+ ))
74+ defer span .End ()
75+
6676 responses := shardResponses {
6777 Shards : make (map [string ]* string ),
6878 }
6979
70- for _ , xfagg := range fr .shardAggregators {
80+ for _ , shardName := range body .ShardNames {
81+ xfagg , ok := fr .shardAggregators [shardName ]
82+ if ! ok {
83+ errString := fmt .Sprintf ("unknown shard %s" , shardName )
84+ responses .Shards [shardName ] = & errString
85+ continue
86+ }
87+
7188 logger := fr .logger .With (log.Fields {
7289 "shard" : log .String (xfagg .shard .Name ),
7390 })
7491
75- waiter , err := processManualCutoff (logger , body . ShardNames , xfagg . shard , xfagg )
92+ waiter , err := processManualCutoff (ctx , logger , xfagg )
7693 if err != nil {
7794 errString := err .Error ()
7895 responses .Shards [xfagg .shard .Name ] = & errString
@@ -107,15 +124,12 @@ func (fr *FileReceiver) triggerManualCutoff() http.HandlerFunc {
107124 }
108125}
109126
110- func processManualCutoff (logger log.Logger , shardNames []string , shard service.Shard , xfagg * aggregator ) (* manuallyTriggeredCutoff , error ) {
111- if ! exists (shardNames , shard .Name ) {
112- return nil , fmt .Errorf ("unknown shard %s" , shard .Name )
113- }
114-
127+ func processManualCutoff (ctx context.Context , logger log.Logger , xfagg * aggregator ) (* manuallyTriggeredCutoff , error ) {
115128 logger .Info ().Log ("found shard to manually trigger" )
116129
117130 waiter := manuallyTriggeredCutoff {
118- C : make (chan error , 1 ),
131+ C : make (chan error , 1 ),
132+ ctx : ctx ,
119133 }
120134 xfagg .cutoffTrigger <- waiter
121135 return & waiter , nil
@@ -129,3 +143,11 @@ func exists(names []string, shardName string) bool {
129143 }
130144 return false
131145}
146+
147+ func (fr * FileReceiver ) getShardNames () []string {
148+ shardNames := make ([]string , 0 , len (fr .shardAggregators ))
149+ for _ , xfagg := range fr .shardAggregators {
150+ shardNames = append (shardNames , xfagg .shard .Name )
151+ }
152+ return shardNames
153+ }
0 commit comments