feat: expose version identity in Go SDK

This commit is contained in:
2026-09-20 18:29:18 +08:00
parent 5622a9e2f2
commit af10086570
6 changed files with 62 additions and 4 deletions

View File

@@ -27,6 +27,18 @@ type Health struct {
Scope string `json:"scope,omitempty"`
}
// VersionInfo 描述服务构建身份和当前协议版本,不包含主机、数据库或租户信息。
type VersionInfo struct {
Version string `json:"version"` // Version 发布版本或本地开发标识。
Revision string `json:"revision"` // Revision 不可变源码提交标识。
BuiltAt string `json:"built_at"` // BuiltAt 构建时间。
GoVersion string `json:"go_version"` // GoVersion 编译器版本。
HTTPProtocolVersion string `json:"http_protocol_version"` // HTTPProtocolVersion HTTP接口主版本。
WorkerProtocolVersion string `json:"worker_protocol_version"` // WorkerProtocolVersion Worker WebSocket子协议版本。
EventProtocolVersion string `json:"event_protocol_version"` // EventProtocolVersion Event WebSocket子协议版本。
WorkflowSchemaVersion int `json:"workflow_schema_version"` // WorkflowSchemaVersion 流程定义结构版本。
}
// HTTPError exposes the status code without including sensitive response bodies.
type HTTPError struct{ StatusCode int }
@@ -53,6 +65,40 @@ func (c *Client) Liveness(ctx context.Context) (Health, error) { return c.health
func (c *Client) Readiness(ctx context.Context) (Health, error) {
return c.health(ctx, "/health/ready")
}
// Version returns build and protocol identity from the unauthenticated version endpoint.
func (c *Client) Version(ctx context.Context) (VersionInfo, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/version", nil)
if err != nil {
return VersionInfo{}, err
}
req.Header.Set("Accept", "application/json")
response, err := c.http.Do(req)
if err != nil {
return VersionInfo{}, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return VersionInfo{}, &HTTPError{StatusCode: response.StatusCode}
}
const maxBody = 16 << 10
body, err := io.ReadAll(io.LimitReader(response.Body, maxBody+1))
if err != nil {
return VersionInfo{}, err
}
if len(body) > maxBody {
return VersionInfo{}, errors.New("Orbit version response exceeds limit")
}
var info VersionInfo
if err := json.Unmarshal(body, &info); err != nil {
return VersionInfo{}, fmt.Errorf("decode Orbit version response: %w", err)
}
if info.Version == "" || info.HTTPProtocolVersion == "" || info.WorkerProtocolVersion == "" || info.EventProtocolVersion == "" || info.WorkflowSchemaVersion == 0 {
return VersionInfo{}, errors.New("Orbit version response is missing identity")
}
return info, nil
}
func (c *Client) health(ctx context.Context, path string) (Health, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+path, nil)
if err != nil {

View File

@@ -15,6 +15,10 @@ func TestHealthContract(t *testing.T) {
if r.Method != "GET" || r.Header.Get("Accept") != "application/json" {
t.Error("incorrect request")
}
if r.URL.Path == "/version" {
_, _ = w.Write([]byte(`{"version":"1.2.3","revision":"abc","built_at":"now","go_version":"go1.27.1","http_protocol_version":"1","worker_protocol_version":"1","event_protocol_version":"1","workflow_schema_version":1}`))
return
}
if r.URL.Path == "/health/live" {
_, _ = w.Write([]byte(`{"status":"alive"}`))
return
@@ -31,6 +35,10 @@ func TestHealthContract(t *testing.T) {
if err != nil || live.Status != "alive" {
t.Fatalf("%+v %v", live, err)
}
info, err := client.Version(context.Background())
if err != nil || info.Version != "1.2.3" || info.Revision != "abc" || info.WorkflowSchemaVersion != 1 {
t.Fatalf("version=%+v err=%v", info, err)
}
_, err = client.Readiness(context.Background())
var status *orbit.HTTPError
if !errors.As(err, &status) || status.StatusCode != 503 || strings.Contains(err.Error(), "secret") {

View File

@@ -29,5 +29,9 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Printf("liveness=%s readiness=%s scope=%s\n", live.Status, ready.Status, ready.Scope)
version, err := client.Version(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("liveness=%s readiness=%s scope=%s version=%s revision=%s\n", live.Status, ready.Status, ready.Scope, version.Version, version.Revision)
}