iop/packages/observability/observability.go
toki 2d6fde0876 기능: IOP 모노레포 스캐폴드 초기 구현
apps/node 중심 구현 — TCP+JSON transport, Hexagonal Architecture,
mock/cli adapter, fx DI, SQLite 실행 이력 저장.
edge/control-plane/worker는 cobra placeholder.
유닛 테스트 및 통합 테스트 클라이언트 계획서 추가.
2026-05-02 13:20:35 +09:00

34 lines
891 B
Go

package observability
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewLogger creates a zap production logger at the given level.
func NewLogger(level string) (*zap.Logger, error) {
cfg := zap.NewProductionConfig()
lvl, err := zapcore.ParseLevel(level)
if err != nil {
lvl = zapcore.InfoLevel
}
cfg.Level = zap.NewAtomicLevelAt(lvl)
return cfg.Build()
}
// ServeMetrics starts a Prometheus /metrics endpoint on the given port.
// It blocks until the server exits.
func ServeMetrics(port int) error {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
addr := fmt.Sprintf(":%d", port)
return http.ListenAndServe(addr, mux)
}