@@ -16,6 +16,10 @@ import (
1616 "github.com/apognu/gocal"
1717 "github.com/spechtlabs/go-otel-utils/otelzap"
1818 "github.com/spf13/viper"
19+ "go.opentelemetry.io/otel"
20+ "go.opentelemetry.io/otel/attribute"
21+ "go.opentelemetry.io/otel/codes"
22+ "go.opentelemetry.io/otel/trace"
1923 "go.uber.org/zap"
2024
2125 pb "github.com/SpechtLabs/CalendarAPI/pkg/protos"
@@ -25,6 +29,7 @@ type ICalClient struct {
2529 cacheMux sync.RWMutex
2630 cache * pb.CalendarResponse
2731 cacheExpiration time.Time
32+ tracer trace.Tracer
2833
2934 statusMux sync.RWMutex
3035 CustomStatus map [string ]* pb.CustomStatus // custom status is a map from calendar-name to status
@@ -79,10 +84,14 @@ func NewICalClient() *ICalClient {
7984 cacheExpiration : time .Now (),
8085 cache : & pb.CalendarResponse {LastUpdated : time .Now ().Unix ()},
8186 CustomStatus : make (map [string ]* pb.CustomStatus ),
87+ tracer : otel .GetTracerProvider ().Tracer ("github.com/SpechtLabs/CalendarAPI/pkg/client" ),
8288 }
8389}
8490
8591func (e * ICalClient ) FetchEvents (ctx context.Context ) {
92+ ctx , span := e .tracer .Start (ctx , "ICalClient.FetchEvents" )
93+ defer span .End ()
94+
8695 response := & pb.CalendarResponse {
8796 LastUpdated : time .Now ().Unix (),
8897 Entries : make ([]* pb.CalendarEntry , 0 ),
@@ -127,6 +136,9 @@ func (e *ICalClient) FetchEvents(ctx context.Context) {
127136}
128137
129138func (e * ICalClient ) GetEvents (ctx context.Context ) * pb.CalendarResponse {
139+ ctx , span := e .tracer .Start (ctx , "ICalClient.GetEvents" )
140+ defer span .End ()
141+
130142 if e .cache == nil {
131143 otelzap .L ().Ctx (ctx ).Sugar ().Infow ("Experiencing cold. Fetching events now!" )
132144 e .FetchEvents (ctx )
@@ -138,6 +150,9 @@ func (e *ICalClient) GetEvents(ctx context.Context) *pb.CalendarResponse {
138150}
139151
140152func (e * ICalClient ) GetCurrentEvent (ctx context.Context , calendar string ) * pb.CalendarEntry {
153+ ctx , span := e .tracer .Start (ctx , "ICalClient.GetCurrentEvent" )
154+ defer span .End ()
155+
141156 if e .cache == nil {
142157 otelzap .L ().Ctx (ctx ).Sugar ().Infow ("Experiencing cold. Fetching events now!" )
143158 e .FetchEvents (ctx )
@@ -186,7 +201,10 @@ func (e *ICalClient) GetCurrentEvent(ctx context.Context, calendar string) *pb.C
186201 return closest
187202}
188203
189- func (e * ICalClient ) GetCustomStatus (_ context.Context , req * pb.GetCustomStatusRequest ) * pb.CustomStatus {
204+ func (e * ICalClient ) GetCustomStatus (ctx context.Context , req * pb.GetCustomStatusRequest ) * pb.CustomStatus {
205+ ctx , span := e .tracer .Start (ctx , "ICalClient.GetCustomStatus" )
206+ defer span .End ()
207+
190208 e .statusMux .RLock ()
191209 defer e .statusMux .RUnlock ()
192210
@@ -197,14 +215,26 @@ func (e *ICalClient) GetCustomStatus(_ context.Context, req *pb.GetCustomStatusR
197215 return & pb.CustomStatus {}
198216}
199217
200- func (e * ICalClient ) SetCustomStatus (_ context.Context , req * pb.SetCustomStatusRequest ) {
218+ func (e * ICalClient ) SetCustomStatus (ctx context.Context , req * pb.SetCustomStatusRequest ) {
219+ ctx , span := e .tracer .Start (ctx , "ICalClient.SetCustomStatus" )
220+ defer span .End ()
221+
201222 e .statusMux .Lock ()
202223 defer e .statusMux .Unlock ()
203224
204225 e .CustomStatus [req .CalendarName ] = req .Status
205226}
206227
207228func (e * ICalClient ) loadEvents (ctx context.Context , calName string , from string , url string , rules []Rule ) ([]* pb.CalendarEntry , * errors.ResolvingError ) {
229+ ctx , span := e .tracer .Start (ctx , "ICalClient.loadEvents" )
230+ defer span .End ()
231+
232+ span .SetAttributes (
233+ attribute .String ("calendar.name" , calName ),
234+ attribute .String ("calendar.from" , from ),
235+ attribute .String ("calendar.url" , url ),
236+ )
237+
208238 ical , err := e .getIcal (ctx , from , url )
209239 if ical == nil || err != nil {
210240 return nil , errors .Wrap (err , fmt .Errorf ("failed to load iCal calendar file" ), "" )
@@ -328,14 +358,25 @@ func (e *ICalClient) getIcalFromFile(path string) (io.ReadCloser, *errors.Resolv
328358}
329359
330360func (e * ICalClient ) getIcalFromURL (ctx context.Context , url string ) (io.ReadCloser , * errors.ResolvingError ) {
331- req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
361+ ctx , span := e .tracer .Start (ctx , "ICalClient.getIcalFromURL" )
362+ defer span .End ()
363+
364+ span .SetAttributes (
365+ attribute .String ("http.method" , http .MethodGet ),
366+ )
367+
368+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
332369 if err != nil {
370+ span .RecordError (err )
371+ span .SetStatus (codes .Error , err .Error ())
333372 return nil , errors .NewResolvingError (fmt .Errorf ("failed creating request for %s: %w" , url , err ), "" )
334373 }
335374
336375 client := http .DefaultClient
337376 resp , err := client .Do (req )
338377 if err != nil {
378+ span .RecordError (err )
379+ span .SetStatus (codes .Error , err .Error ())
339380 return nil , errors .NewResolvingError (fmt .Errorf ("failed making request to %s: %w" , url , err ), "verify if URL exists and is accessible" )
340381 }
341382
0 commit comments