Skip to content

Commit 526843e

Browse files
committed
Add memory store tracing
1 parent c3866e0 commit 526843e

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

exporter/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (c *Component) Start() error {
128128
tracer := otel.Tracer("exporter")
129129
for message := range c.ChanResult {
130130
ctx, span := tracer.Start(context.Background(), "export")
131-
c.MemoryStore.Add(message)
131+
c.MemoryStore.Add(ctx, message)
132132
if message.Success {
133133
c.Logger.Debug("Healthcheck successful",
134134
zap.String("name", message.Name),

http/handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,12 @@ func (c *Component) handlers() {
295295
if !c.Config.DisableResultAPI {
296296
apiGroup.GET("/result", func(ec echo.Context) error {
297297
return ec.JSON(http.StatusOK, ListResultsOutput{
298-
Result: c.MemoryStore.List(),
298+
Result: c.MemoryStore.List(ec.Request().Context()),
299299
})
300300
})
301301
apiGroup.GET("/result/:name", func(ec echo.Context) error {
302302
name := ec.Param("name")
303-
result, err := c.MemoryStore.Get(name)
303+
result, err := c.MemoryStore.Get(ec.Request().Context(), name)
304304
if err != nil {
305305
return corbierror.New(err.Error(), corbierror.NotFound, true)
306306
}
@@ -351,7 +351,7 @@ func (c *Component) handlers() {
351351
return corbierror.Wrap(err, "Internal error", corbierror.Internal, true)
352352
}
353353
var tmplBytes bytes.Buffer
354-
if err := tmpl.Execute(&tmplBytes, c.MemoryStore.List()); err != nil {
354+
if err := tmpl.Execute(&tmplBytes, c.MemoryStore.List(ec.Request().Context())); err != nil {
355355
return corbierror.Wrap(err, "Internal error", corbierror.Internal, true)
356356
}
357357
return ec.HTML(http.StatusOK, tmplBytes.String())

memorystore/root.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package memorystore
22

33
import (
4+
"context"
45
"fmt"
56
"sort"
67
"sync"
@@ -10,6 +11,7 @@ import (
1011
"gopkg.in/tomb.v2"
1112

1213
"github.com/appclacks/cabourotte/healthcheck"
14+
"go.opentelemetry.io/otel"
1315
)
1416

1517
// MemoryStore A store containing the latest healthchecks results
@@ -41,7 +43,7 @@ func (m *MemoryStore) Start() {
4143
for {
4244
select {
4345
case <-m.Tick.C:
44-
m.Purge()
46+
m.Purge(context.Background())
4547
case <-m.t.Dying():
4648
return nil
4749
}
@@ -61,14 +63,20 @@ func (m *MemoryStore) Stop() error {
6163
}
6264

6365
// Add a new Result to the store
64-
func (m *MemoryStore) Add(result *healthcheck.Result) {
66+
func (m *MemoryStore) Add(ctx context.Context, result *healthcheck.Result) {
67+
tracer := otel.Tracer("memorystore")
68+
_, span := tracer.Start(ctx, "memory_store_add")
69+
defer span.End()
6570
m.lock.Lock()
6671
defer m.lock.Unlock()
6772
m.Results[result.Name] = result
6873
}
6974

7075
// Purge the expired results
71-
func (m *MemoryStore) Purge() {
76+
func (m *MemoryStore) Purge(ctx context.Context) {
77+
tracer := otel.Tracer("memorystore")
78+
_, span := tracer.Start(ctx, "memory_store_purge")
79+
defer span.End()
7280
m.lock.Lock()
7381
defer m.lock.Unlock()
7482
now := time.Now()
@@ -84,7 +92,10 @@ func (m *MemoryStore) Purge() {
8492
}
8593

8694
// List returns the current value of the results
87-
func (m *MemoryStore) List() []healthcheck.Result {
95+
func (m *MemoryStore) List(ctx context.Context) []healthcheck.Result {
96+
tracer := otel.Tracer("memorystore")
97+
_, span := tracer.Start(ctx, "memory_store_list")
98+
defer span.End()
8899
m.lock.RLock()
89100
defer m.lock.RUnlock()
90101
result := make([]healthcheck.Result, 0, len(m.Results))
@@ -99,7 +110,10 @@ func (m *MemoryStore) List() []healthcheck.Result {
99110
}
100111

101112
// Get returns the current value for a healthcheck
102-
func (m *MemoryStore) Get(name string) (healthcheck.Result, error) {
113+
func (m *MemoryStore) Get(ctx context.Context, name string) (healthcheck.Result, error) {
114+
tracer := otel.Tracer("memorystore")
115+
_, span := tracer.Start(ctx, "memory_store_get")
116+
defer span.End()
103117
m.lock.RLock()
104118
defer m.lock.RUnlock()
105119
if result, ok := m.Results[name]; ok {

memorystore/root_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package memorystore
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

@@ -18,8 +19,8 @@ func TestMemoryExporter(t *testing.T) {
1819
HealthcheckTimestamp: ts.Unix(),
1920
Message: "message",
2021
}
21-
store.Add(result)
22-
resultList := store.List()
22+
store.Add(context.Background(), result)
23+
resultList := store.List(context.Background())
2324
if !resultList[0].Equals(*result) {
2425
t.Fatalf("Invalid result content")
2526
}
@@ -33,13 +34,13 @@ func TestMemoryExporter(t *testing.T) {
3334
HealthcheckTimestamp: ts.Unix(),
3435
Message: "message",
3536
}
36-
store.Add(expiredResult)
37-
resultList = store.List()
37+
store.Add(context.Background(), expiredResult)
38+
resultList = store.List(context.Background())
3839
if len(resultList) != 2 {
3940
t.Fatalf("Invalid result list size: %d", len(resultList))
4041
}
41-
store.Purge()
42-
resultList = store.List()
42+
store.Purge(context.Background())
43+
resultList = store.List(context.Background())
4344
if !resultList[0].Equals(*result) {
4445
t.Fatalf("Invalid result content")
4546
}

0 commit comments

Comments
 (0)