Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push handler metrics to main service when exiting #565

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"time"

"github.com/frostbyte73/core"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"google.golang.org/grpc"

"github.com/livekit/egress/pkg/config"
Expand Down Expand Up @@ -120,6 +123,17 @@ func (h *Handler) Run() error {
case res := <-result:
// recording finished
_, _ = h.ioClient.UpdateEgress(ctx, res)

m, err := h.GenerateMetrics(ctx)
if err == nil {
h.ipcServiceClient.HandlerShuttingDown(ctx, &ipc.HandlerShuttingDownRequest{
EgressId: h.conf.Info.EgressId,
Metrics: m,
})
} else {
logger.Errorw("failed generating handler metrics", err)
}

h.rpcServer.Shutdown()
h.ipcHandlerServer.Stop()
return nil
Expand All @@ -130,3 +144,35 @@ func (h *Handler) Run() error {
func (h *Handler) Kill() {
h.kill.Break()
}

func (h *Handler) GenerateMetrics(ctx context.Context) (string, error) {
metrics, err := prometheus.DefaultGatherer.Gather()
if err != nil {
return "", err
}

metricsAsString, err := renderMetrics(metrics)
if err != nil {
return "", err
}

return metricsAsString, nil
}

func renderMetrics(metrics []*dto.MetricFamily) (string, error) {
// Create a StringWriter to render the metrics into text format
writer := &strings.Builder{}
totalCnt := 0
for _, metric := range metrics {
// Write each metric family to text
cnt, err := expfmt.MetricFamilyToText(writer, metric)
if err != nil {
logger.Errorw("error writing metric family", err)
return "", err
}
totalCnt += cnt
}

// Get the rendered metrics as a string from the StringWriter
return writer.String(), nil
}
30 changes: 1 addition & 29 deletions pkg/handler/handler_ipc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ package handler

import (
"context"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/livekit/egress/pkg/errors"
"github.com/livekit/egress/pkg/ipc"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/pprof"
"github.com/livekit/protocol/tracer"
)
Expand Down Expand Up @@ -79,12 +74,7 @@ func (h *Handler) GetMetrics(ctx context.Context, req *ipc.MetricsRequest) (*ipc
ctx, span := tracer.Start(ctx, "Handler.GetMetrics")
defer span.End()

metrics, err := prometheus.DefaultGatherer.Gather()
if err != nil {
return nil, err
}

metricsAsString, err := renderMetrics(metrics)
metricsAsString, err := h.GenerateMetrics(ctx)
if err != nil {
return nil, err
}
Expand All @@ -93,21 +83,3 @@ func (h *Handler) GetMetrics(ctx context.Context, req *ipc.MetricsRequest) (*ipc
Metrics: metricsAsString,
}, nil
}

func renderMetrics(metrics []*dto.MetricFamily) (string, error) {
// Create a StringWriter to render the metrics into text format
writer := &strings.Builder{}
totalCnt := 0
for _, metric := range metrics {
// Write each metric family to text
cnt, err := expfmt.MetricFamilyToText(writer, metric)
if err != nil {
logger.Errorw("error writing metric family", err)
return "", err
}
totalCnt += cnt
}

// Get the rendered metrics as a string from the StringWriter
return writer.String(), nil
}
Loading