Skip to content

Commit

Permalink
Add memory store tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
mcorbin committed Jan 9, 2025
1 parent c3866e0 commit 526843e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
2 changes: 1 addition & 1 deletion exporter/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (c *Component) Start() error {
tracer := otel.Tracer("exporter")
for message := range c.ChanResult {
ctx, span := tracer.Start(context.Background(), "export")
c.MemoryStore.Add(message)
c.MemoryStore.Add(ctx, message)
if message.Success {
c.Logger.Debug("Healthcheck successful",
zap.String("name", message.Name),
Expand Down
6 changes: 3 additions & 3 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@ func (c *Component) handlers() {
if !c.Config.DisableResultAPI {
apiGroup.GET("/result", func(ec echo.Context) error {
return ec.JSON(http.StatusOK, ListResultsOutput{
Result: c.MemoryStore.List(),
Result: c.MemoryStore.List(ec.Request().Context()),
})
})
apiGroup.GET("/result/:name", func(ec echo.Context) error {
name := ec.Param("name")
result, err := c.MemoryStore.Get(name)
result, err := c.MemoryStore.Get(ec.Request().Context(), name)
if err != nil {
return corbierror.New(err.Error(), corbierror.NotFound, true)
}
Expand Down Expand Up @@ -351,7 +351,7 @@ func (c *Component) handlers() {
return corbierror.Wrap(err, "Internal error", corbierror.Internal, true)
}
var tmplBytes bytes.Buffer
if err := tmpl.Execute(&tmplBytes, c.MemoryStore.List()); err != nil {
if err := tmpl.Execute(&tmplBytes, c.MemoryStore.List(ec.Request().Context())); err != nil {
return corbierror.Wrap(err, "Internal error", corbierror.Internal, true)
}
return ec.HTML(http.StatusOK, tmplBytes.String())
Expand Down
24 changes: 19 additions & 5 deletions memorystore/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memorystore

import (
"context"
"fmt"
"sort"
"sync"
Expand All @@ -10,6 +11,7 @@ import (
"gopkg.in/tomb.v2"

"github.com/appclacks/cabourotte/healthcheck"
"go.opentelemetry.io/otel"
)

// MemoryStore A store containing the latest healthchecks results
Expand Down Expand Up @@ -41,7 +43,7 @@ func (m *MemoryStore) Start() {
for {
select {
case <-m.Tick.C:
m.Purge()
m.Purge(context.Background())
case <-m.t.Dying():
return nil
}
Expand All @@ -61,14 +63,20 @@ func (m *MemoryStore) Stop() error {
}

// Add a new Result to the store
func (m *MemoryStore) Add(result *healthcheck.Result) {
func (m *MemoryStore) Add(ctx context.Context, result *healthcheck.Result) {
tracer := otel.Tracer("memorystore")
_, span := tracer.Start(ctx, "memory_store_add")
defer span.End()
m.lock.Lock()
defer m.lock.Unlock()
m.Results[result.Name] = result
}

// Purge the expired results
func (m *MemoryStore) Purge() {
func (m *MemoryStore) Purge(ctx context.Context) {
tracer := otel.Tracer("memorystore")
_, span := tracer.Start(ctx, "memory_store_purge")
defer span.End()
m.lock.Lock()
defer m.lock.Unlock()
now := time.Now()
Expand All @@ -84,7 +92,10 @@ func (m *MemoryStore) Purge() {
}

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

// Get returns the current value for a healthcheck
func (m *MemoryStore) Get(name string) (healthcheck.Result, error) {
func (m *MemoryStore) Get(ctx context.Context, name string) (healthcheck.Result, error) {
tracer := otel.Tracer("memorystore")
_, span := tracer.Start(ctx, "memory_store_get")
defer span.End()
m.lock.RLock()
defer m.lock.RUnlock()
if result, ok := m.Results[name]; ok {
Expand Down
13 changes: 7 additions & 6 deletions memorystore/root_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memorystore

import (
"context"
"testing"
"time"

Expand All @@ -18,8 +19,8 @@ func TestMemoryExporter(t *testing.T) {
HealthcheckTimestamp: ts.Unix(),
Message: "message",
}
store.Add(result)
resultList := store.List()
store.Add(context.Background(), result)
resultList := store.List(context.Background())
if !resultList[0].Equals(*result) {
t.Fatalf("Invalid result content")
}
Expand All @@ -33,13 +34,13 @@ func TestMemoryExporter(t *testing.T) {
HealthcheckTimestamp: ts.Unix(),
Message: "message",
}
store.Add(expiredResult)
resultList = store.List()
store.Add(context.Background(), expiredResult)
resultList = store.List(context.Background())
if len(resultList) != 2 {
t.Fatalf("Invalid result list size: %d", len(resultList))
}
store.Purge()
resultList = store.List()
store.Purge(context.Background())
resultList = store.List(context.Background())
if !resultList[0].Equals(*result) {
t.Fatalf("Invalid result content")
}
Expand Down

0 comments on commit 526843e

Please sign in to comment.