metric 서비스 복원

This commit is contained in:
2025-07-01 18:51:40 +09:00
parent 106aa68529
commit 19a4ff103f
7 changed files with 383 additions and 13 deletions

View File

@ -31,6 +31,9 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type runcommand struct {
@ -44,6 +47,7 @@ type clientConfig struct {
HttpAddress string `json:"http_server_address"`
StorageRoot string `json:"storage_path"`
MetricNamespace string `json:"metric_namespace"`
MetricPipeName string `json:"metric_pipe"`
ConstLabels map[string]string `json:"metric_const_labels"`
Autorun map[string]runcommand `json:"autorun"`
}
@ -70,6 +74,7 @@ func loadClientConfig() (clientConfig, error) {
type HoustonClient interface {
Shutdown()
Start()
MetricHandler() http.Handler
}
var seq = int32(1)
@ -120,6 +125,7 @@ type houstonClient struct {
version string
standalone bool
siblingProcIndex map[string]uint64
registry *prometheus.Registry
}
func unmarshal[T any](val *T, src map[string]string) {
@ -298,6 +304,7 @@ func NewClient(standalone bool) (HoustonClient, error) {
standalone: standalone,
uploadChan: make(chan uploadRequest, 100),
siblingProcIndex: make(map[string]uint64),
registry: prometheus.NewRegistry(),
}
ctx, cancel := context.WithCancel(context.Background())
@ -580,6 +587,16 @@ func (hc *houstonClient) Start() {
close(hc.uploadChan)
}()
if len(hc.config.MetricPipeName) == 0 {
hc.config.MetricPipeName = "houston_metric_pipe"
}
if len(hc.config.MetricNamespace) == 0 {
hc.config.MetricNamespace = "ou"
}
run_metric_pipe_reader(hc.config, hc.registry, hc.ctx)
go func() {
// upload 고루틴
url := hc.config.HttpAddress + "/upload"
@ -651,7 +668,7 @@ func (hc *houstonClient) Start() {
reconnCount++
var err error
dialContext, cancelDial := context.WithTimeout(context.Background(), 15*time.Second)
dialContext, cancelDial := context.WithTimeout(context.Background(), 5*time.Second)
client, err = grpc.DialContext(dialContext, hc.config.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
cancelDial()
@ -679,6 +696,12 @@ func (hc *houstonClient) Shutdown() {
hc.shutdownFunc()
}
func (hc *houstonClient) MetricHandler() http.Handler {
return promhttp.InstrumentMetricHandler(
hc.registry, promhttp.HandlerFor(hc.registry, promhttp.HandlerOpts{}),
)
}
func (hc *houstonClient) checkOperation(client *grpc.ClientConn) error {
defer func() {
r := recover()