-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add testable documentation examples to featuregate package #13159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
parv18050212
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
parv18050212:add-featuregate-examples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d87ca40
Add testable documentation examples to featuregate package
parv18050212 f2f5acf
Merge branch 'main' into add-featuregate-examples
parv18050212 c7869df
Improve featuregate package docs: focus on checking if a gate is enabled
parv18050212 b6453e7
Merge branch 'main' into add-featuregate-examples
parv18050212 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package featuregate provides mechanisms for feature flagging in the OpenTelemetry Collector. | ||
// | ||
// Example (Creating a feature gate and printing its properties): | ||
// | ||
// gate := featuregate.GlobalRegistry().MustRegister( | ||
// "example.feature", | ||
// featuregate.StageAlpha, | ||
// featuregate.WithRegisterDescription("Example feature gate for demonstration."), | ||
// featuregate.WithRegisterReferenceURL("https://example.com/feature"), | ||
// featuregate.WithRegisterFromVersion("v0.1.0"), | ||
// ) | ||
// fmt.Println("ID:", gate.ID()) | ||
// fmt.Println("Description:", gate.Description()) | ||
// fmt.Println("Stage:", gate.Stage()) | ||
// fmt.Println("ReferenceURL:", gate.ReferenceURL()) | ||
// fmt.Println("FromVersion:", gate.FromVersion()) | ||
// fmt.Println("IsEnabled:", gate.IsEnabled()) | ||
// | ||
// Example (Registering, setting, and visiting feature gates): | ||
// | ||
// reg := featuregate.NewRegistry() | ||
// gate := reg.MustRegister("example.feature2", featuregate.StageAlpha, featuregate.WithRegisterDescription("Another example.")) | ||
// _ = reg.Set(gate.ID(), true) // Enable the gate | ||
// reg.VisitAll(func(g *featuregate.Gate) { | ||
// fmt.Printf("Gate: %s, Enabled: %v, Description: %s\n", g.ID(), g.IsEnabled(), g.Description()) | ||
// }) | ||
// | ||
package featuregate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package featuregate_test | ||
|
||
import ( | ||
"fmt" | ||
"go.opentelemetry.io/collector/featuregate" | ||
) | ||
|
||
// Example_createFeatureGate demonstrates creating a feature gate and printing its properties. | ||
func Example_createFeatureGate() { | ||
gate := featuregate.GlobalRegistry().MustRegister( | ||
"example.feature", | ||
featuregate.StageAlpha, | ||
featuregate.WithRegisterDescription("Example feature gate for demonstration."), | ||
featuregate.WithRegisterReferenceURL("https://example.com/feature"), | ||
featuregate.WithRegisterFromVersion("v0.1.0"), | ||
) | ||
fmt.Println("ID:", gate.ID()) | ||
fmt.Println("Description:", gate.Description()) | ||
fmt.Println("Stage:", gate.Stage()) | ||
fmt.Println("ReferenceURL:", gate.ReferenceURL()) | ||
fmt.Println("FromVersion:", gate.FromVersion()) | ||
fmt.Println("IsEnabled:", gate.IsEnabled()) | ||
// Output: | ||
// ID: example.feature | ||
// Description: Example feature gate for demonstration. | ||
// Stage: Alpha | ||
// ReferenceURL: https://example.com/feature | ||
// FromVersion: v0.1.0 | ||
// IsEnabled: false | ||
} | ||
|
||
// Example_registrySetAndVisit demonstrates registering, setting, and visiting feature gates. | ||
func Example_registrySetAndVisit() { | ||
reg := featuregate.NewRegistry() | ||
gate := reg.MustRegister("example.feature2", featuregate.StageAlpha, featuregate.WithRegisterDescription("Another example.")) | ||
_ = reg.Set(gate.ID(), true) // Enable the gate | ||
reg.VisitAll(func(g *featuregate.Gate) { | ||
fmt.Printf("Gate: %s, Enabled: %v, Description: %s\n", g.ID(), g.IsEnabled(), g.Description()) | ||
}) | ||
// Output: | ||
// Gate: example.feature2, Enabled: true, Description: Another example. | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than outputting every attribute here, I would show how to check the gate is enabled. That's the main thing someone looking to use this package will want to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback!
I've updated the documentation to focus on the main use case: checking if a feature gate is enabled. The example now shows how to register a gate and use
IsEnabled()
, and the second example demonstrates registering, setting, and visiting gates in a concise way.Let me know if you have any further suggestions or if there’s anything else I should adjust!