2121use Hyperf \Metric \Contract \HistogramInterface ;
2222use Hyperf \Metric \Contract \MetricFactoryInterface ;
2323
24+ use function Hyperf \Support \env ;
2425use function Hyperf \Support \make ;
2526
2627class MetricFactory implements MetricFactoryInterface
@@ -38,32 +39,32 @@ public function __construct(private ConfigInterface $config)
3839
3940 public function makeCounter (string $ name , ?array $ labelNames = []): CounterInterface
4041 {
41- return new Counter (
42+ return ( new Counter (
4243 $ this ->client ,
4344 $ name ,
4445 $ this ->getSampleRate (),
45- $ labelNames
46- );
46+ array_merge ([ ' service.name ' , ' host.name ' ], $ labelNames),
47+ ))-> with ( $ this -> getNamespace (), $ this -> getHostname ()) ;
4748 }
4849
4950 public function makeGauge (string $ name , ?array $ labelNames = []): GaugeInterface
5051 {
51- return new Gauge (
52+ return ( new Gauge (
5253 $ this ->client ,
5354 $ name ,
5455 $ this ->getSampleRate (),
55- $ labelNames
56- );
56+ array_merge ([ ' service.name ' , ' host.name ' ], $ labelNames),
57+ ))-> with ( $ this -> getNamespace (), $ this -> getHostname ()) ;
5758 }
5859
5960 public function makeHistogram (string $ name , ?array $ labelNames = []): HistogramInterface
6061 {
61- return new Histogram (
62+ return ( new Histogram (
6263 $ this ->client ,
6364 $ name ,
6465 $ this ->getSampleRate (),
65- $ labelNames
66- );
66+ array_merge ([ ' service.name ' , ' host.name ' ], $ labelNames),
67+ ))-> with ( $ this -> getNamespace (), $ this -> getHostname ()) ;
6768 }
6869
6970 public function handle (): void
@@ -112,4 +113,9 @@ protected function getSampleRate(): float
112113 $ name = $ this ->config ->get ('metric.default ' );
113114 return $ this ->config ->get ("metric.metric. {$ name }.sample_rate " , 1.0 );
114115 }
116+
117+ protected function getHostname (): string
118+ {
119+ return $ this ->config ->get ('hostname ' , env ('HOSTNAME ' ));
120+ }
115121}
0 commit comments