메트릭 사용방법 변경
This commit is contained in:
@ -23,6 +23,7 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||||
|
"repositories.action2quare.com/ayo/gocommon/metric"
|
||||||
"repositories.action2quare.com/ayo/houston/shared"
|
"repositories.action2quare.com/ayo/houston/shared"
|
||||||
"repositories.action2quare.com/ayo/houston/shared/protos"
|
"repositories.action2quare.com/ayo/houston/shared/protos"
|
||||||
|
|
||||||
@ -93,19 +94,20 @@ func (pm *procmeta) setState(s protos.ProcessState) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type houstonClient struct {
|
type houstonClient struct {
|
||||||
childProcs []*procmeta
|
childProcs []*procmeta
|
||||||
extraMetrics unsafe.Pointer // map[string]float32
|
extraMetrics unsafe.Pointer // map[string]float32
|
||||||
deploys map[string][]*protos.VersionAndArgs
|
deploys map[string][]*protos.VersionAndArgs
|
||||||
shutdownFunc context.CancelFunc
|
shutdownFunc context.CancelFunc
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
operationChan chan *protos.OperationQueryResponse
|
operationChan chan *protos.OperationQueryResponse
|
||||||
exitChan chan *exec.Cmd
|
exitChan chan *exec.Cmd
|
||||||
clientChan chan *grpc.ClientConn
|
clientChan chan *grpc.ClientConn
|
||||||
timestamp string
|
timestamp string
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
config clientConfig
|
config clientConfig
|
||||||
version string
|
version string
|
||||||
standalone bool
|
standalone bool
|
||||||
|
metricExporter metric.Exporter
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshal[T any](val *T, src map[string]string) {
|
func unmarshal[T any](val *T, src map[string]string) {
|
||||||
@ -258,13 +260,14 @@ func NewClient(standalone bool) (HoustonClient, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
hc := &houstonClient{
|
hc := &houstonClient{
|
||||||
config: clientConfig,
|
config: clientConfig,
|
||||||
clientChan: make(chan *grpc.ClientConn),
|
clientChan: make(chan *grpc.ClientConn),
|
||||||
extraMetrics: unsafe.Pointer(&map[string]float32{}),
|
extraMetrics: unsafe.Pointer(&map[string]float32{}),
|
||||||
deploys: deploys,
|
deploys: deploys,
|
||||||
timestamp: exefi.ModTime().String(),
|
timestamp: exefi.ModTime().String(),
|
||||||
version: string(ver),
|
version: string(ver),
|
||||||
standalone: standalone,
|
standalone: standalone,
|
||||||
|
metricExporter: metric.NewPrometheusExport(clientConfig.MetricNamespace),
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|||||||
@ -1,85 +0,0 @@
|
|||||||
package client
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math"
|
|
||||||
_ "net/http/pprof"
|
|
||||||
"sync/atomic"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"repositories.action2quare.com/ayo/gocommon/metric"
|
|
||||||
)
|
|
||||||
|
|
||||||
type metricDesc struct {
|
|
||||||
*prometheus.Desc
|
|
||||||
val *uint64
|
|
||||||
valueType prometheus.ValueType
|
|
||||||
}
|
|
||||||
|
|
||||||
type exporterForPrometheus struct {
|
|
||||||
metricPtr unsafe.Pointer // []metricDesc
|
|
||||||
}
|
|
||||||
|
|
||||||
func newExporterForPrometheus() *exporterForPrometheus {
|
|
||||||
return &exporterForPrometheus{
|
|
||||||
metricPtr: unsafe.Pointer(new([]metricDesc)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type metricValueAccessor struct {
|
|
||||||
ptr *uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (va *metricValueAccessor) set(val float64) {
|
|
||||||
atomic.StoreUint64(va.ptr, math.Float64bits(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertValueType(in metric.MetricType) prometheus.ValueType {
|
|
||||||
switch in {
|
|
||||||
case metric.MetricCounter:
|
|
||||||
return prometheus.CounterValue
|
|
||||||
|
|
||||||
case metric.MetricGuage:
|
|
||||||
return prometheus.GaugeValue
|
|
||||||
}
|
|
||||||
|
|
||||||
return prometheus.UntypedValue
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *exporterForPrometheus) registMetric(namespace string, desc metric.MetricDescription) metricValueAccessor {
|
|
||||||
ptr := atomic.LoadPointer(&e.metricPtr)
|
|
||||||
container := *(*[]metricDesc)(ptr)
|
|
||||||
|
|
||||||
newcont := make([]metricDesc, len(container)+1)
|
|
||||||
copy(newcont, container)
|
|
||||||
|
|
||||||
newval := new(uint64)
|
|
||||||
newcont[len(container)] = metricDesc{
|
|
||||||
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", desc.Name), desc.Help, nil, desc.ConstLabels),
|
|
||||||
val: newval,
|
|
||||||
valueType: convertValueType(desc.Type),
|
|
||||||
}
|
|
||||||
|
|
||||||
atomic.StorePointer(&e.metricPtr, unsafe.Pointer(&newcont))
|
|
||||||
return metricValueAccessor{
|
|
||||||
ptr: newval,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *exporterForPrometheus) Describe(ch chan<- *prometheus.Desc) {
|
|
||||||
ptr := atomic.LoadPointer(&e.metricPtr)
|
|
||||||
container := *(*[]metricDesc)(ptr)
|
|
||||||
|
|
||||||
for _, v := range container {
|
|
||||||
ch <- v.Desc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *exporterForPrometheus) Collect(ch chan<- prometheus.Metric) {
|
|
||||||
ptr := atomic.LoadPointer(&e.metricPtr)
|
|
||||||
container := *(*[]metricDesc)(ptr)
|
|
||||||
|
|
||||||
for _, v := range container {
|
|
||||||
ch <- prometheus.MustNewConstMetric(v.Desc, v.valueType, math.Float64frombits(atomic.LoadUint64(v.val)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -4,12 +4,10 @@ import (
|
|||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/binary"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@ -20,7 +18,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||||
"repositories.action2quare.com/ayo/gocommon/metric"
|
"repositories.action2quare.com/ayo/gocommon/metric"
|
||||||
"repositories.action2quare.com/ayo/houston/shared"
|
"repositories.action2quare.com/ayo/houston/shared"
|
||||||
@ -233,8 +230,6 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
|||||||
|
|
||||||
readingMetric := false
|
readingMetric := false
|
||||||
var metricBuffer []byte
|
var metricBuffer []byte
|
||||||
metricValues := make(map[string]metricValueAccessor)
|
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
logger.Println("stdReader is terminated :", meta.name)
|
logger.Println("stdReader is terminated :", meta.name)
|
||||||
if meta.isState(protos.ProcessState_Running) {
|
if meta.isState(protos.ProcessState_Running) {
|
||||||
@ -273,27 +268,10 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, registered := metricValues[metric.Key]; !registered {
|
hc.metricExporter.RegisterMetric(&metric)
|
||||||
exporter := newExporterForPrometheus()
|
|
||||||
accessor := exporter.registMetric(hc.config.MetricNamespace, metric)
|
|
||||||
|
|
||||||
if err := prometheus.Register(exporter); err != nil {
|
|
||||||
logger.Println("prometheus.Register error :", err)
|
|
||||||
} else {
|
|
||||||
metricValues[metric.Key] = accessor
|
|
||||||
logger.Println("metric registered :", metric)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
keybytes := metricBuffer[:8]
|
key, val := metric.ReadMetricValue(metricBuffer)
|
||||||
valbits := binary.LittleEndian.Uint64(metricBuffer[8:])
|
hc.metricExporter.UpdateMetric(key, val)
|
||||||
val := math.Float64frombits(valbits)
|
|
||||||
|
|
||||||
if accessor, ok := metricValues[string(keybytes)]; ok {
|
|
||||||
accessor.set(val)
|
|
||||||
} else {
|
|
||||||
logger.Println("metric set but unregistered :", string(keybytes))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
metricBuffer = metricBuffer[:0]
|
metricBuffer = metricBuffer[:0]
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -10,7 +10,7 @@ require (
|
|||||||
golang.org/x/text v0.10.0
|
golang.org/x/text v0.10.0
|
||||||
google.golang.org/grpc v1.56.0
|
google.golang.org/grpc v1.56.0
|
||||||
google.golang.org/protobuf v1.31.0
|
google.golang.org/protobuf v1.31.0
|
||||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20231127145009-2c0211678122
|
repositories.action2quare.com/ayo/gocommon v0.0.0-20231128132952-e5a5240f96d8
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
4
go.sum
4
go.sum
@ -120,5 +120,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
|
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
|
||||||
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
|
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
|
||||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20231127145009-2c0211678122 h1:pbROgLih1x7ooXNrKBO7sPK6bOW5bzuMHQY8DKWkRbc=
|
repositories.action2quare.com/ayo/gocommon v0.0.0-20231128132952-e5a5240f96d8 h1:GbvfiscAV/gquGzC9bJ3RTNtezLcdfGOv+9JmAZYQVc=
|
||||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20231127145009-2c0211678122/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
|
repositories.action2quare.com/ayo/gocommon v0.0.0-20231128132952-e5a5240f96d8/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
|
||||||
|
|||||||
Reference in New Issue
Block a user