99 "github.com/styrainc/opa-control-plane/internal/builder"
1010 "github.com/styrainc/opa-control-plane/internal/config"
1111 "github.com/styrainc/opa-control-plane/internal/logging"
12+ "github.com/styrainc/opa-control-plane/internal/metrics"
1213 "github.com/styrainc/opa-control-plane/internal/progress"
1314 "github.com/styrainc/opa-control-plane/internal/s3"
1415)
@@ -93,6 +94,7 @@ func (worker *BundleWorker) UpdateConfig(b *config.Bundle, sources []*config.Sou
9394// Execute runs a bundle synchronization iteration: git sync, bundle construct
9495// and then push bundles to object storage.
9596func (w * BundleWorker ) Execute (ctx context.Context ) time.Time {
97+ startTime := time .Now () // Used for timing metric
9698
9799 defer w .bar .Add (1 )
98100
@@ -106,22 +108,22 @@ func (w *BundleWorker) Execute(ctx context.Context) time.Time {
106108 for _ , src := range w .sources {
107109 if err := src .Wipe (); err != nil {
108110 w .log .Warnf ("failed to remove a directory for bundle %q: %v" , w .bundleConfig .Name , err )
109- return w .report (ctx , BuildStateInternalError , err )
111+ return w .report (ctx , BuildStateInternalError , startTime , err )
110112 }
111113 }
112114
113115 for _ , synchronizer := range w .synchronizers {
114116 err := synchronizer .Execute (ctx )
115117 if err != nil {
116118 w .log .Warnf ("failed to synchronize bundle %q: %v" , w .bundleConfig .Name , err )
117- return w .report (ctx , BuildStateSyncFailed , err )
119+ return w .report (ctx , BuildStateSyncFailed , startTime , err )
118120 }
119121 }
120122
121123 for _ , src := range w .sources {
122124 if err := src .Transform (ctx ); err != nil {
123125 w .log .Warnf ("failed to evaluate source %q for bundle %q: %v" , src .Name , w .bundleConfig .Name , err )
124- return w .report (ctx , BuildStateTransformFailed , err )
126+ return w .report (ctx , BuildStateTransformFailed , startTime , err )
125127 }
126128 }
127129
@@ -135,24 +137,24 @@ func (w *BundleWorker) Execute(ctx context.Context) time.Time {
135137 err := b .Build (ctx )
136138 if err != nil {
137139 w .log .Warnf ("failed to build a bundle %q: %v" , w .bundleConfig .Name , err )
138- return w .report (ctx , BuildStateBuildFailed , err )
140+ return w .report (ctx , BuildStateBuildFailed , startTime , err )
139141 }
140142
141143 if w .storage != nil {
142144 if err := w .storage .Upload (ctx , bytes .NewReader (buffer .Bytes ())); err != nil {
143145 w .log .Warnf ("failed to upload bundle %q: %v" , w .bundleConfig .Name , err )
144- return w .report (ctx , BuildStatePushFailed , err )
146+ return w .report (ctx , BuildStatePushFailed , startTime , err )
145147 }
146148
147149 w .log .Debugf ("Bundle %q built and uploaded." , w .bundleConfig .Name )
148- return w .report (ctx , BuildStateSuccess , nil )
150+ return w .report (ctx , BuildStateSuccess , startTime , nil )
149151 }
150152
151153 w .log .Debugf ("Bundle %q built." , w .bundleConfig .Name )
152- return w .report (ctx , BuildStateSuccess , nil )
154+ return w .report (ctx , BuildStateSuccess , startTime , nil )
153155}
154156
155- func (w * BundleWorker ) report (ctx context.Context , state BuildState , err error ) time.Time {
157+ func (w * BundleWorker ) report (ctx context.Context , state BuildState , startTime time. Time , err error ) time.Time {
156158 w .status .State = state
157159 if err != nil {
158160 if _ , ok := err .(ast.Errors ); ok {
@@ -162,6 +164,12 @@ func (w *BundleWorker) report(ctx context.Context, state BuildState, err error)
162164 }
163165 }
164166
167+ if state == BuildStateSuccess {
168+ metrics .BundleBuildSucceeded (w .bundleConfig .Name , state .String (), startTime )
169+ } else {
170+ metrics .BundleBuildFailed (w .bundleConfig .Name , state .String ())
171+ }
172+
165173 if w .singleShot {
166174 return w .die (ctx )
167175 }
0 commit comments