Skip to content
Open
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
7 changes: 7 additions & 0 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,10 @@ const (
LoggerAppender = "console"
LoggerFormat = "text"
)

const (
HealthCheckDefaultPort = "8080"
HealthCheckDefaultLivePath = "/health/live"
HealthCheckDefaultReadyPath = "/health/ready"
HealthCheckDefaultTimeout = "10s"
)
9 changes: 9 additions & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ const (
PrometheusPushgatewayJobKey = "prometheus.pushgateway.job"
)

// Health check HTTP keys for K8S integration
const (
HealthCheckEnabledKey = "health-check.enabled"
HealthCheckPortKey = "health-check.port"
HealthCheckLivePathKey = "health-check.live-path"
HealthCheckReadyPathKey = "health-check.ready-path"
HealthCheckTimeoutKey = "health-check.timeout"
)

// default meta cache config
const (
DefaultMetaCacheName = "dubbo.meta"
Expand Down
34 changes: 25 additions & 9 deletions config/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,27 @@ import (

// MetricsConfig This is the config struct for all metrics implementation
type MetricsConfig struct {
Enable *bool `default:"false" yaml:"enable" json:"enable,omitempty" property:"enable"`
Port string `default:"9090" yaml:"port" json:"port,omitempty" property:"port"`
Path string `default:"/metrics" yaml:"path" json:"path,omitempty" property:"path"`
Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"`
EnableMetadata *bool `default:"false" yaml:"enable-metadata" json:"enable-metadata,omitempty" property:"enable-metadata"`
EnableRegistry *bool `default:"false" yaml:"enable-registry" json:"enable-registry,omitempty" property:"enable-registry"`
EnableConfigCenter *bool `default:"false" yaml:"enable-config-center" json:"enable-config-center,omitempty" property:"enable-config-center"`
Prometheus *PrometheusConfig `yaml:"prometheus" json:"prometheus" property:"prometheus"`
Aggregation *AggregateConfig `yaml:"aggregation" json:"aggregation" property:"aggregation"`
Enable *bool `default:"false" yaml:"enable" json:"enable,omitempty" property:"enable"`
Port string `default:"9090" yaml:"port" json:"port,omitempty" property:"port"`
Path string `default:"/metrics" yaml:"path" json:"path,omitempty" property:"path"`
Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"`
EnableMetadata *bool `default:"false" yaml:"enable-metadata" json:"enable-metadata,omitempty" property:"enable-metadata"`
EnableRegistry *bool `default:"false" yaml:"enable-registry" json:"enable-registry,omitempty" property:"enable-registry"`
EnableConfigCenter *bool `default:"false" yaml:"enable-config-center" json:"enable-config-center,omitempty" property:"enable-config-center"`
Prometheus *PrometheusConfig `yaml:"prometheus" json:"prometheus" property:"prometheus"`
Aggregation *AggregateConfig `yaml:"aggregation" json:"aggregation" property:"aggregation"`
HealthCheck *HealthCheckConfig `yaml:"health-check" json:"health-check" property:"health-check"`
rootConfig *RootConfig
}

type HealthCheckConfig struct {
Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"`
Port string `default:"8080" yaml:"port" json:"port,omitempty" property:"port"`
LivePath string `default:"/health/live" yaml:"live-path" json:"live-path,omitempty" property:"live-path"`
ReadyPath string `default:"/health/ready" yaml:"ready-path" json:"ready-path,omitempty" property:"ready-path"`
Timeout string `default:"10s" yaml:"timeout" json:"timeout,omitempty" property:"timeout"`
}

type AggregateConfig struct {
Enabled *bool `default:"false" yaml:"enabled" json:"enabled,omitempty" property:"enabled"`
BucketNum int `default:"10" yaml:"bucket-num" json:"bucket-num,omitempty" property:"bucket-num"`
Expand Down Expand Up @@ -161,5 +170,12 @@ func (mc *MetricsConfig) toURL() *common.URL {
url.SetParam(constant.PrometheusPushgatewayJobKey, pushGateWay.Job)
}
}
if mc.HealthCheck != nil {
url.SetParam(constant.HealthCheckEnabledKey, strconv.FormatBool(*mc.HealthCheck.Enabled))
url.SetParam(constant.HealthCheckPortKey, mc.HealthCheck.Port)
url.SetParam(constant.HealthCheckLivePathKey, mc.HealthCheck.LivePath)
url.SetParam(constant.HealthCheckReadyPathKey, mc.HealthCheck.ReadyPath)
url.SetParam(constant.HealthCheckTimeoutKey, mc.HealthCheck.Timeout)
}
return url
}
1 change: 1 addition & 0 deletions imports/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
_ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos"
_ "dubbo.apache.org/dubbo-go/v3/metadata/report/zookeeper"
_ "dubbo.apache.org/dubbo-go/v3/metrics/app_info"
_ "dubbo.apache.org/dubbo-go/v3/metrics/health"
_ "dubbo.apache.org/dubbo-go/v3/metrics/prometheus"
_ "dubbo.apache.org/dubbo-go/v3/otel/trace/jaeger"
_ "dubbo.apache.org/dubbo-go/v3/otel/trace/otlp"
Expand Down
31 changes: 31 additions & 0 deletions metrics/health/health_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package health

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/metrics"
"dubbo.apache.org/dubbo-go/v3/server"
)

func init() {
metrics.AddCollector("health", func(registry metrics.MetricRegistry, url *common.URL) {
healthServer := server.NewHealthCheckServer(url)
healthServer.Start()
})
}
170 changes: 170 additions & 0 deletions server/builtin_health_checkers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package server

import (
"context"
"sync/atomic"
"time"
)

// DubboHealthChecker checks if dubbo services are properly exported
type DubboHealthChecker struct {
serverInstance *Server
}

// NewDubboHealthChecker creates a health checker for dubbo services
func NewDubboHealthChecker(server *Server) *DubboHealthChecker {
return &DubboHealthChecker{serverInstance: server}
}

func (d *DubboHealthChecker) CheckLiveness(ctx context.Context) bool {
// Liveness: server instance exists (process is alive)
return d.serverInstance != nil
}

func (d *DubboHealthChecker) CheckReadiness(ctx context.Context) bool {
// Readiness: services are exported and ready to handle requests
if d.serverInstance == nil {
return false
}

// Check if any services are registered
hasServices := false
d.serverInstance.svcOptsMap.Range(func(key, value interface{}) bool {
hasServices = true
return false // Stop iteration after finding first service
})

return hasServices
}

func (d *DubboHealthChecker) CheckReadinessDetailed(ctx context.Context) *HealthCheckResult {
result := &HealthCheckResult{
Healthy: true,
Details: make(map[string]string),
}

if d.serverInstance == nil {
result.Healthy = false
result.Message = "Server instance not initialized"
result.Details["server"] = "not_initialized"
return result
}

// Count exported services
serviceCount := 0
d.serverInstance.svcOptsMap.Range(func(key, value interface{}) bool {
serviceCount++
return true
})

if serviceCount == 0 {
result.Healthy = false
result.Message = "No services exported"
result.Details["services"] = "none_exported"
} else {
result.Details["services"] = "exported"
result.Details["service_count"] = string(rune(serviceCount))
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting integer to string via rune cast is incorrect and will produce unexpected results. Use strconv.Itoa(serviceCount) instead.

Copilot uses AI. Check for mistakes.
}

return result
}

func (d *DubboHealthChecker) Name() string {
return "DubboHealthChecker"
}

// GracefulShutdownHealthChecker tracks graceful shutdown state
type GracefulShutdownHealthChecker struct {
shuttingDown int32
}

// NewGracefulShutdownHealthChecker creates a health checker that tracks shutdown state
func NewGracefulShutdownHealthChecker() *GracefulShutdownHealthChecker {
return &GracefulShutdownHealthChecker{}
}

func (g *GracefulShutdownHealthChecker) CheckLiveness(ctx context.Context) bool {
return atomic.LoadInt32(&g.shuttingDown) == 0
}

func (g *GracefulShutdownHealthChecker) CheckReadiness(ctx context.Context) bool {
return atomic.LoadInt32(&g.shuttingDown) == 0
}

func (g *GracefulShutdownHealthChecker) Name() string {
return "GracefulShutdownHealthChecker"
}

// MarkShuttingDown marks the service as shutting down
func (g *GracefulShutdownHealthChecker) MarkShuttingDown() {
atomic.StoreInt32(&g.shuttingDown, 1)
}

// TimeoutHealthChecker wraps another checker with configurable timeout
type TimeoutHealthChecker struct {
checker HealthChecker
timeout time.Duration
}

// NewTimeoutHealthChecker creates a timeout-protected health checker
func NewTimeoutHealthChecker(checker HealthChecker, timeout time.Duration) *TimeoutHealthChecker {
return &TimeoutHealthChecker{
checker: checker,
timeout: timeout,
}
}

func (t *TimeoutHealthChecker) CheckLiveness(ctx context.Context) bool {
ctx, cancel := context.WithTimeout(ctx, t.timeout)
defer cancel()

done := make(chan bool, 1)
go func() {
done <- t.checker.CheckLiveness(ctx)
}()

select {
case result := <-done:
return result
case <-ctx.Done():
return false // Timeout considered unhealthy
}
}

func (t *TimeoutHealthChecker) CheckReadiness(ctx context.Context) bool {
ctx, cancel := context.WithTimeout(ctx, t.timeout)
defer cancel()

done := make(chan bool, 1)
go func() {
done <- t.checker.CheckReadiness(ctx)
}()

select {
case result := <-done:
return result
case <-ctx.Done():
return false // Timeout considered unhealthy
}
}

func (t *TimeoutHealthChecker) Name() string {
return "TimeoutHealthChecker(" + t.checker.Name() + ")"
}
Loading
Loading