Skip to content

Commit

Permalink
metric actor
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Jan 8, 2024
1 parent 4ae3f8b commit 5a3569b
Show file tree
Hide file tree
Showing 30 changed files with 134 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
uses: actions/checkout@v4

- name: Test
run: go test ./pkg/*
run: go test ./internal/*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ api: build
@./bin/api --seed

test:
@go test ./pkg/* -v
@go test ./internal/* -v

proto:
protoc --go_out=. --go_opt=paths=source_relative --proto_path=. proto/types.proto
Expand Down
8 changes: 4 additions & 4 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"os"
"time"

"github.com/anthdm/raptor/pkg/api"
"github.com/anthdm/raptor/pkg/config"
"github.com/anthdm/raptor/pkg/storage"
"github.com/anthdm/raptor/pkg/types"
"github.com/anthdm/raptor/internal/api"
"github.com/anthdm/raptor/internal/config"
"github.com/anthdm/raptor/internal/storage"
"github.com/anthdm/raptor/internal/types"
"github.com/google/uuid"
"github.com/tetratelabs/wazero"
)
Expand Down
Empty file added cmd/cli/js.wasm
Empty file.
8 changes: 4 additions & 4 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"os"
"strings"

"github.com/anthdm/raptor/pkg/api"
"github.com/anthdm/raptor/pkg/client"
"github.com/anthdm/raptor/pkg/config"
"github.com/anthdm/raptor/pkg/types"
"github.com/anthdm/raptor/internal/api"
"github.com/anthdm/raptor/internal/client"
"github.com/anthdm/raptor/internal/config"
"github.com/anthdm/raptor/internal/types"
"github.com/google/uuid"
)

Expand Down
34 changes: 34 additions & 0 deletions cmd/load/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"time"
)

func main() {
for {
makeRequest()
time.Sleep(time.Millisecond * 50)
}
}

func makeRequest() {
req, err := http.NewRequest("get", "http://localhost:5000/live/09248ef6-c401-4601-8928-5964d61f2c61", nil)
if err != nil {
log.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

fmt.Println(string(b))
}
16 changes: 11 additions & 5 deletions cmd/wasmserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/anthdm/hollywood/actor"
"github.com/anthdm/hollywood/cluster"
"github.com/anthdm/hollywood/remote"
"github.com/anthdm/raptor/pkg/actrs"
"github.com/anthdm/raptor/pkg/config"
"github.com/anthdm/raptor/pkg/storage"
"github.com/anthdm/raptor/internal/actrs"
"github.com/anthdm/raptor/internal/config"
"github.com/anthdm/raptor/internal/storage"
)

func main() {
Expand Down Expand Up @@ -57,10 +57,16 @@ func main() {
ID: config.Get().Cluster.ID,
ClusterProvider: cluster.NewSelfManagedProvider(),
})
c.RegisterKind(actrs.KindRuntime, actrs.NewRuntime(store, metricStore, modCache), &cluster.KindConfig{})
c.RegisterKind(actrs.KindRuntime, actrs.NewRuntime(store, modCache), &cluster.KindConfig{})
c.Engine().Spawn(actrs.NewMetric, actrs.KindMetric, actor.WithID("1"))
c.Start()

server := actrs.NewWasmServer(config.Get().WASMServerAddr, c, store, metricStore, modCache)
server := actrs.NewWasmServer(
config.Get().WASMServerAddr,
c,
store,
metricStore,
modCache)
c.Engine().Spawn(server, actrs.KindWasmServer)
fmt.Printf("wasm server running\t%s\n", config.Get().WASMServerAddr)

Expand Down
4 changes: 2 additions & 2 deletions examples/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"net/http"

run "github.com/anthdm/raptor/sdk"
raptor "github.com/anthdm/raptor/sdk"
"github.com/go-chi/chi"
)

Expand All @@ -21,5 +21,5 @@ func main() {
router := chi.NewMux()
router.Get("/dashboard", handleDashboard)
router.Get("/login", handleLogin)
run.Handle(router)
raptor.Handle(router)
}
2 changes: 1 addition & 1 deletion examples/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ function respond(res, status) {
console.log(status)
}

respond("<h1>From my Raptor application</h1>", 200)
respond("<h1>From my Raptor application</h1></br>some other stuff here</br>", 200)
27 changes: 27 additions & 0 deletions internal/actrs/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package actrs

import (
"github.com/anthdm/hollywood/actor"
"github.com/anthdm/raptor/internal/types"
)

// The metric actor is responsible for handling metrics that are being
// sent from the runtimes locally from the same machine.

const KindMetric = "runtime_metric"

type Metric struct{}

func NewMetric() actor.Receiver {
return &Metric{}
}

// TODO: Store metrics where they belong
func (m *Metric) Receive(c *actor.Context) {
switch msg := c.Message().(type) {
case actor.Started:
case actor.Stopped:
case types.RuntimeMetric:
_ = msg
}
}
46 changes: 22 additions & 24 deletions pkg/actrs/runtime.go → internal/actrs/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"time"

"github.com/anthdm/hollywood/actor"
"github.com/anthdm/raptor/pkg/runtime"
"github.com/anthdm/raptor/pkg/shared"
"github.com/anthdm/raptor/pkg/spidermonkey"
"github.com/anthdm/raptor/pkg/storage"
"github.com/anthdm/raptor/pkg/types"
"github.com/anthdm/raptor/internal/runtime"
"github.com/anthdm/raptor/internal/shared"
"github.com/anthdm/raptor/internal/spidermonkey"
"github.com/anthdm/raptor/internal/storage"
"github.com/anthdm/raptor/internal/types"
"github.com/anthdm/raptor/proto"
"github.com/google/uuid"
"github.com/tetratelabs/wazero"
Expand All @@ -26,19 +26,17 @@ const KindRuntime = "runtime"

// Runtime is an actor that can execute compiled WASM blobs in a distributed cluster.
type Runtime struct {
store storage.Store
metricStore storage.MetricStore
cache storage.ModCacher
started time.Time
deployID uuid.UUID
store storage.Store
cache storage.ModCacher
started time.Time
deployID uuid.UUID
}

func NewRuntime(store storage.Store, metricStore storage.MetricStore, cache storage.ModCacher) actor.Producer {
func NewRuntime(store storage.Store, cache storage.ModCacher) actor.Producer {
return func() actor.Receiver {
return &Runtime{
store: store,
metricStore: metricStore,
cache: cache,
store: store,
cache: cache,
}
}
}
Expand Down Expand Up @@ -119,19 +117,19 @@ func (r *Runtime) handleHTTPRequest(ctx *actor.Context, msg *proto.HTTPRequest)

ctx.Engine().Poison(ctx.PID())

// only store metrics when its a request on LIVE
// only send metrics when its a request on LIVE
if !msg.Preview {
metric := types.RuntimeMetric{
ID: uuid.New(),
StartTime: r.started,
Duration: time.Since(r.started),
DeployID: deploy.ID,
EndpointID: deploy.EndpointID,
RequestURL: msg.URL,
}
if err := r.metricStore.CreateRuntimeMetric(&metric); err != nil {
slog.Warn("failed to create runtime metric", "err", err)
ID: uuid.New(),
StartTime: r.started,
Duration: time.Since(r.started),
DeploymentID: deploy.ID,
EndpointID: deploy.EndpointID,
RequestURL: msg.URL,
StatusCode: status,
}
pid := ctx.Engine().Registry.GetPID(KindMetric, "1")
ctx.Send(pid, metric)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/actrs/wasmserver.go → internal/actrs/wasmserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/anthdm/hollywood/actor"
"github.com/anthdm/hollywood/cluster"
"github.com/anthdm/raptor/pkg/shared"
"github.com/anthdm/raptor/pkg/storage"
"github.com/anthdm/raptor/internal/shared"
"github.com/anthdm/raptor/internal/storage"
"github.com/anthdm/raptor/proto"
"github.com/google/uuid"
)
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions pkg/api/server.go → internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"io"
"net/http"

"github.com/anthdm/raptor/pkg/config"
"github.com/anthdm/raptor/pkg/storage"
"github.com/anthdm/raptor/pkg/types"
"github.com/anthdm/raptor/internal/config"
"github.com/anthdm/raptor/internal/storage"
"github.com/anthdm/raptor/internal/types"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions pkg/client/client.go → internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"io"
"net/http"

"github.com/anthdm/raptor/pkg/api"
"github.com/anthdm/raptor/pkg/types"
"github.com/anthdm/raptor/internal/api"
"github.com/anthdm/raptor/internal/types"
"github.com/google/uuid"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/storage/sql.go → internal/storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"strings"

"github.com/anthdm/raptor/pkg/types"
"github.com/anthdm/raptor/internal/types"
"github.com/google/uuid"
_ "github.com/lib/pq"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/storage.go → internal/storage/storage.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package storage

import (
"github.com/anthdm/raptor/pkg/types"
"github.com/anthdm/raptor/internal/types"
"github.com/google/uuid"
)

Expand Down
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions internal/types/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package types

import (
"time"

"github.com/google/uuid"
)

// RuntimeMetric holds information about a single runtime execution.
type RuntimeMetric struct {
ID uuid.UUID `json:"id"`
EndpointID uuid.UUID `json:"endpoint_id"`
DeploymentID uuid.UUID `json:"deployment_id"`
RequestURL string `json:"request_url"`
Duration time.Duration `json:"duration"`
StartTime time.Time `json:"start_time"`
StatusCode int `json:"status_code"`
}
File renamed without changes.
17 changes: 0 additions & 17 deletions pkg/types/metrics.go

This file was deleted.

0 comments on commit 5a3569b

Please sign in to comment.