mcpField
G
go-feature-flag

go-feature-flag

github.com/laurigates开发5958 次安装1 天前更新
代码仓库

关于 go-feature-flag

GO Feature Flag 是基于文件配置并集成 OpenFeature 的开源功能开关方案。此技能涵盖标志设置、定向与渐进发布规则、中继代理部署及配置验证,适用于管理自托管功能开关的团队。

主要功能

  1. 01基于文件的标志配置
  2. 02集成 OpenFeature
  3. 03定向规则与百分比发布
  4. 04设置中继代理
  5. 05使用 CLI 验证配置

适用场景

  1. 01为应用配置功能开关
  2. 02按用户属性定向标志变体
  3. 03运行中继代理评估标志
  4. 04检查标志配置文件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/laurigates/claude-plugins --skill go-feature-flag

安装后在 Claude Code 中输入/go-feature-flag确认已加载。

附带文件REFERENCE.md

详细介绍

GO Feature Flag

开源功能标志解决方案,支持基于文件的配置和 OpenFeature 集成。适用于设置可自托管的功能标志、配置标志文件或部署中继代理。

适用场景

自动激活触发条件:

  • 用户提及“GO Feature Flag”、“GOFF”或“gofeatureflag”
  • 项目依赖项中包含 @openfeature/go-feature-flag-provider
  • 项目中包含 flags.goff.yaml 或类似的标志配置文件
  • 用户询问可自托管的功能标志
  • Docker/K8s 配置中包含 gofeatureflag/go-feature-flag 镜像

相关技能:

  • openfeature - OpenFeature SDK 使用模式
  • container-development - Docker/K8s 部署

架构概述

┌─────────────────────────────────────────────────────────────┐
│                     Application                              │
│                         │                                    │
│                   OpenFeature SDK                            │
│                         │                                    │
│              GO Feature Flag Provider                        │
└─────────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────────┐
│                 GO Feature Flag Relay Proxy                  │
│  ┌─────────────────────────────────────────────────────────┐│
│  │                    Retriever                            ││
│  │  (File, S3, GitHub, HTTP, K8s ConfigMap, etc.)         ││
│  └─────────────────────────────────────────────────────────┘│
│  ┌─────────────────────────────────────────────────────────┐│
│  │                    Exporter                             ││
│  │  (Webhook, S3, Kafka, PubSub, etc.)                    ││
│  └─────────────────────────────────────────────────────────┘│
│  ┌─────────────────────────────────────────────────────────┐│
│  │                   Notifier                              ││
│  │  (Slack, Discord, Teams, Webhook)                      ││
│  └─────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────────┐
│                   Flag Configuration                         │
│                    (flags.goff.yaml)                         │
└─────────────────────────────────────────────────────────────┘

标志配置格式

基本结构
# flags.goff.yaml
flag-name:
  variations:       # All possible values
    variation1: value1
    variation2: value2
  defaultRule:      # Rule when no targeting matches
    variation: variation1
  targeting:        # Optional: targeting rules
    - name: rule-name
      query: 'expression'
      variation: variation2
布尔标志
# Simple on/off flag
new-feature:
  variations:
    enabled: true
    disabled: false
  defaultRule:
    variation: disabled

String、Number 和 Object/JSON 标志示例,请参阅 REFERENCE.md。

定向规则

查询语法

GO Feature Flag 使用类似 CEL 的查询语法进行定向:

targeting:
  - name: beta-users
    query: 'groups co "beta"'  # contains
    variation: enabled

  - name: specific-user
    query: 'targetingKey eq "user-123"'  # equals
    variation: enabled

  - name: email-domain
    query: 'email ew "@company.com"'  # ends with
    variation: enabled

  - name: premium-tier
    query: 'plan in ["pro", "enterprise"]'  # in list
    variation: enabled
运算符
运算符描述示例
eq等于email eq "[email protected]"
ne不等于plan ne "free"
co包含groups co "admin"
sw以……开头email sw "admin"
ew以……结尾email ew "@company.com"
in在列表中country in ["US", "CA"]
gt、ge、lt、le比较age gt 18
and、or逻辑运算plan eq "pro" and country eq "US"
优先级

规则从上到下进行评估。第一个匹配的规则生效:

targeting:
  # Highest priority: specific user override
  - name: test-user
    query: 'targetingKey eq "test-user-id"'
    variation: enabled

  # Second: admin group
  - name: admins
    query: 'groups co "admin"'
    variation: enabled

  # Third: beta users
  - name: beta
    query: 'groups co "beta"'
    variation: enabled

  # Fallback is defaultRule
defaultRule:
  variation: disabled

发布策略

百分比发布
new-checkout:
  variations:
    enabled: true
    disabled: false
  defaultRule:
    percentage:
      enabled: 20   # 20% of users
      disabled: 80  # 80% of users

渐进式发布、计划变更和 A/B 测试模式,请参阅 REFERENCE.md。

中继代理配置

Docker Compose(开发环境)
# docker-compose.yaml
services:
  goff-relay:
    image: gofeatureflag/go-feature-flag:latest
    ports:
      - "1031:1031"  # API
      - "1032:1032"  # Health/metrics
    volumes:
      - ./flags.goff.yaml:/goff/flags.yaml:ro
    environment:
      # Retriever configuration
      - RETRIEVER_KIND=file
      - RETRIEVER_PATH=/goff/flags.yaml

      # Polling interval (ms)
      - POLLING_INTERVAL_MS=10000

      # Logging
      - LOG_LEVEL=info
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:1032/health"]
      interval: 10s
      timeout: 5s
      retries: 3
环境变量
# Retriever (choose one)
RETRIEVER_KIND=file|s3|http|github|gitlab|googlecloud|azureblob|k8s

# File retriever
RETRIEVER_PATH=/path/to/flags.yaml

# S3 retriever
RETRIEVER_BUCKET=my-bucket
RETRIEVER_ITEM=flags/production.yaml
AWS_REGION=us-east-1

# GitHub retriever
RETRIEVER_REPOSITORY_SLUG=owner/repo
RETRIEVER_FILE_PATH=flags/production.yaml
RETRIEVER_BRANCH=main
GITHUB_TOKEN=ghp_xxxx

# HTTP retriever
RETRIEVER_URL=https://api.example.com/flags.yaml
RETRIEVER_HEADERS=Authorization=Bearer xxx

# Polling
POLLING_INTERVAL_MS=30000

# Server
HTTP_PORT=1031
ADMIN_PORT=1032
LOG_LEVEL=info|debug|warn|error

Kubernetes 部署配置,请参阅 REFERENCE.md。

导出器

导出标志评估数据以供分析。支持的类型:webhook、s3、googlecloud、kafka、pubsub、log。

导出器环境变量的详细配置,请参阅 REFERENCE.md。

通知器

在标志发生变更时发送通知。支持:Slack、Discord、Microsoft Teams、Webhook。

Webhook URL 配置,请参阅 REFERENCE.md。

CLI 工具

验证配置
# Install CLI
go install github.com/thomaspoignant/go-feature-flag/cmd/goff@latest

# Validate flag file
goff lint --config flags.goff.yaml

# Output format
goff lint --config flags.goff.yaml --format json
在本地测试标志
# Start relay in foreground
docker run -p 1031:1031 -p 1032:1032 \
  -v $(pwd)/flags.goff.yaml:/goff/flags.yaml:ro \
  -e RETRIEVER_KIND=file \
  -e RETRIEVER_PATH=/goff/flags.yaml \
  gofeatureflag/go-feature-flag:latest

# Test flag evaluation
curl -X POST http://localhost:1031/v1/feature/new-feature/eval \
  -H "Content-Type: application/json" \
  -d '{"evaluationContext": {"targetingKey": "user-123"}}'

最佳实践

1. 标志命名约定
# Namespace by feature/team
checkout.new-payment-form
dashboard.beta-widgets
api.v2-endpoints

# Use consistent suffixes
*.enabled      # boolean toggles
*.config       # object/JSON config
*.percentage   # rollout percentage
2. 跟踪标志生命周期
# Add metadata comments (YAML supports comments)
new-feature:
  # Created: 2024-11-01
  # Owner: team-checkout
  # Jira: PROJ-123
  # Target removal: 2025-01-15
  variations:
    enabled: true
    disabled: false
3. 监控和清理
  • 导出评估数据以跟踪使用情况
  • 为达到 100% 的标志设置提醒(已准备好移除)
  • 每季度审查并清理标志

GitOps CI/CD 工作流和特定环境的标志模式,请参阅 REFERENCE.md。

故障排查

标志评估不正确
# Check relay logs
docker logs goff-relay

# Test evaluation directly
curl -X POST http://localhost:1031/v1/feature/my-flag/eval \
  -H "Content-Type: application/json" \
  -d '{"evaluationContext": {"targetingKey": "test", "email": "[email protected]"}}'
提供程序连接问题
// Check provider initialization
const provider = new GoFeatureFlagProvider({
  endpoint: process.env.GOFF_RELAY_URL,
  timeout: 5000, // Increase timeout
});

// Handle events
provider.on('PROVIDER_READY', () => console.log('Provider ready'));
provider.on('PROVIDER_ERROR', (e) => console.error('Provider error', e));
配置未更新
# Check polling interval
POLLING_INTERVAL_MS=10000  # 10 seconds

# Verify file is readable
docker exec goff-relay cat /goff/flags.yaml

# Check retriever status
curl http://localhost:1032/info

文档

相关命令

  • /configure:feature-flags - 设置完整的功能标志基础设施
  • /configure:dockerfile - 容器配置最佳实践

评论

支持 Markdown