-
Notifications
You must be signed in to change notification settings - Fork 327
Conversation
This package adds resources as a core concept to OpenCensus. Signed-off-by: Fabian Reinartz <[email protected]>
@fabxc this is an interesting idea but definitely needs to be discussed on opencensus-specs first, would you mind raising an issue or PR there for discussion? |
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.
Leaving some high-level readability comments, haven't reviewed the encoder/decoder implementations.
resource/resource_test.go
Outdated
}, | ||
}, | ||
} | ||
for _, c := range cases { |
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.
Use t.Run to create sub tests, so they can be individually run and output is more readable.
resource/resource_test.go
Outdated
{s: `a, extra=chars`, fail: true}, | ||
} | ||
for i, c := range cases { | ||
t.Logf("test %d: %s", i, c.s) |
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.
Ditto, don't Log. Use t.Run.
resource/resource_test.go
Outdated
|
||
func TestMerge(t *testing.T) { | ||
cases := []struct { | ||
a, b, expect *Resource |
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.
s/expect/want
resource/resource_test.go
Outdated
|
||
func TestDecodeTags(t *testing.T) { | ||
cases := []struct { | ||
s string |
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.
s/s/encoded
resource/resource_test.go
Outdated
func TestDecodeTags(t *testing.T) { | ||
cases := []struct { | ||
s string | ||
expect map[string]string |
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.
wantTags
resource/resource.go
Outdated
} | ||
|
||
// FromEnvVars loads resource information from the OC_TYPE and OC_RESOURCE_TAGS environment variables. | ||
func FromEnvVars(context.Context) (*Resource, error) { |
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.
We can call this just FromEnv and dropping the var, similar to the standard library style: https://golang.org/pkg/os/#Setenv.
resource/resource.go
Outdated
// If a value contains whitespaces, =, or " characters, it must always be quoted. | ||
var tagRegex = regexp.MustCompile(`\s*([a-zA-Z0-9-_./]+)=(?:(".*?")|([^\s="]+))\s*,`) | ||
|
||
func DecodeTags(s string) (map[string]string, error) { |
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.
Add godoc.
resource/resource.go
Outdated
return s | ||
} | ||
|
||
// We accept domain names and paths as tag keys. Values may be quoted or unquoted in general. |
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.
Document this restriction somewhere in the godoc.
resource/resource.go
Outdated
} | ||
|
||
// EncodeTags encodes a tags to a string as provided via the OC_RESOURCE_TAGS environment variable. | ||
func EncodeTags(tags map[string]string) (s string) { |
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.
Don't have named return variables if name is not self documenting.
s/s string/string
Signed-off-by: Fabian Reinartz <[email protected]>
resource/resource_test.go
Outdated
{encoded: `missing=leading-quote"`, wantFail: true}, | ||
{encoded: `extra=chars, a`, wantFail: true}, | ||
{encoded: `a, extra=chars`, wantFail: true}, | ||
{encoded: `a, extra=chars`, wantFail: true}, |
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.
duplicate.
resource/resource.go
Outdated
|
||
// Detectall calls all input detectors sequentially an merges each result with the previous one. | ||
// It returns on the first error that a sub-detector encounters. | ||
func DetectAll(ctx context.Context, detectors ...Detector) (*Resource, error) { |
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.
Does DetectAll need to be public?
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.
I'd say so. Seems like a useful helper function that library users may inevitably rebuild for testing or similar.
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.
ChainedDetector(d1, d2, ...)(ctx) does the same. why do we need two ways?
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.
ChainedDetector is implemented using DetectAll (:
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.
Keep the method just make it private detectAll().
Can someone take a look at the appveyor build failure? It doesn't seem like it's related to the code:
|
Signed-off-by: Fabian Reinartz <[email protected]>
@fabxc ignore the appveyor build failure. (see #894 (comment)) |
Signed-off-by: Fabian Reinartz <[email protected]>
/cc @Ramonza can you review? |
resource/resource.go
Outdated
envVarTags = "OC_RESOURCE_TAGS" | ||
) | ||
|
||
// Resource describes an entity about which data is exposed. |
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.
Can we be more specific than "data"?
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.
Maybe give some examples
resource/resource.go
Outdated
Tags map[string]string | ||
} | ||
|
||
// EncodeTags encodes a tags to a string as provided via the OC_RESOURCE_TAGS environment variable. |
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.
encodes a tags map?
resource/resource.go
Outdated
|
||
var tagRegex = regexp.MustCompile(`\s*([a-zA-Z0-9-_./]+)=(?:(".*?")|([^\s="]+))\s*,`) | ||
|
||
// DecodeTags decodes serialized a tag map as used in the OC_RESOURCE_TAGS variable. |
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.
decodes a serialized tag map
resource/resource.go
Outdated
return m, nil | ||
} | ||
|
||
// FromEnv loads resource information from the OC_TYPE and OC_RESOURCE_TAGS environment variables. |
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.
s/OC_TYPE/OC_RESOURCE_TYPE/
resource/resource.go
Outdated
// FromEnv loads resource information from the OC_TYPE and OC_RESOURCE_TAGS environment variables. | ||
func FromEnv(context.Context) (*Resource, error) { | ||
res := &Resource{ | ||
Type: strings.TrimSpace(os.Getenv(envVarType)), |
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.
What happens if this env var is not set?
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.
It will be the empty string, i.e. equivalent to net setting Type
. If it doesn't get populated later on, e.g. by the agent, exporters can decide how to handle this.
resource/resource.go
Outdated
if res.Type == "" { | ||
res.Type = b.Type | ||
} | ||
for k, v := range b.Tags { |
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.
If you first added all the b
tags and then the a
tags then you wouldn't need to check for the existence of the key in the map before overwriting.
resource/resource.go
Outdated
|
||
// Detector attempts to detect resource information. | ||
// If the detector cannot find specific information, the respective Resource fields should | ||
// be left empty but no error should be returned. |
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.
This seems to imply its not valid to return nil, nil
(saying that you should leave fields empty), though that seems like it would be the most natural way to return "this detector does not apply in the current environment".
Signed-off-by: Fabian Reinartz <[email protected]>
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.
Looks good. One thing I'm still not clear on is how this will be used. Is it going to be used to replace the existing resource package in the Stackdriver repo? Is there any way to show an example of its usage here?
Signed-off-by: Fabian Reinartz <[email protected]>
Signed-off-by: Fabian Reinartz <[email protected]>
Signed-off-by: Fabian Reinartz <[email protected]>
resource/resource.go
Outdated
Labels map[string]string | ||
} | ||
|
||
// EncodeLabels encodes a labels map to a string as provided via the OC_RESOURCE_labelS environment variable. |
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.
OC_RESOURCE_labelS probably OC_RESOURCE_LABELS
} | ||
|
||
// EncodeLabels encodes a labels map to a string as provided via the OC_RESOURCE_labelS environment variable. | ||
func EncodeLabels(labels map[string]string) string { |
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.
Do you need this to be public (exported)?
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.
It's not used internally at all, so it's only here for public use. Seemed good to have and we already have a use case with the tool that runs discoveries and produces envvars.
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.
You can document that most users will depend on FromEnv in the godoc.
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.
In which one? Seems odd to document that getter X will be used more frequently in the docstring of a encoder.
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.
Documenting what most users might prefer in godoc is a style we often use at Google and elsewhere. We don't have to do it, I just want users to have an easier time finding an entry point.
// DecodeLabels decodes a serialized label map as used in the OC_RESOURCE_labelS variable. | ||
// A list of labels of the form `<key1>="<value1>",<key2>="<value2>",...` is accepted. | ||
// Domain names and paths are accepted as label keys. | ||
func DecodeLabels(s string) (map[string]string, error) { |
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.
Same comment about public.
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.
Since we have EncodeLabels
public, it seems to make sense to have the symmetric encoding function as well.
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.
I think there's an argument to make both EncodeLabels and DecodeLabels private and just document the expected format in the FromEnv godoc: How do we expect users to use EncodeLabels? Won't they usually just set environment variables in something like a k8s yaml file or shell script? If so, how does it help to have a Go function exported?
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.
We already have a use case for EncodeLabels
here. I think it is a very legitimate one and the Encode/Decode symmetry should generally be given.
resource/resource.go
Outdated
|
||
var labelRegex = regexp.MustCompile(`^\s*([[:ascii:]]{1,256}?)=("[[:ascii:]]{0,256}?")\s*,`) | ||
|
||
// DecodeLabels decodes a serialized label map as used in the OC_RESOURCE_labelS variable. |
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.
Same comment about the env variable name.
resource/resource.go
Outdated
type Detector func(context.Context) (*Resource, error) | ||
|
||
// NewDetectorFromResource returns a detector that will always return resource r. | ||
func NewDetectorFromResource(r *Resource) Detector { |
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.
This is not necessary needed? as @rakyll always told me this is 1 line for users to write.
resource/resource.go
Outdated
var _ Detector = FromEnv | ||
|
||
// Merge resource information from b into a. In case of a collision, a takes precedence. | ||
func Merge(a, b *Resource) *Resource { |
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.
Is this important to be public? People can call the Chained version.
resource/resource.go
Outdated
// ChainedDetector returns a Detector that calls all input detectors sequentially an | ||
// merges each result with the previous one. | ||
// It returns on the first error that a sub-detector encounters. | ||
func ChainedDetector(detectors ...Detector) Detector { |
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.
Maybe FromDetectors? I will let @rakyll propose a good name for this.
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.
What about MultiDetector a la io.MultiReader?
Signed-off-by: Fabian Reinartz <[email protected]>
resource/resource.go
Outdated
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package resource defines the resource type and provides helpers to derive them as well |
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.
Explain what a resource type is rather than saying resource package contains the type.
Signed-off-by: Fabian Reinartz <[email protected]>
@rakyll @bogdandrutu @Ramonza could you take a final look after the recent changes? |
// DecodeLabels decodes a serialized label map as used in the OC_RESOURCE_labelS variable. | ||
// A list of labels of the form `<key1>="<value1>",<key2>="<value2>",...` is accepted. | ||
// Domain names and paths are accepted as label keys. | ||
func DecodeLabels(s string) (map[string]string, error) { |
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.
I think there's an argument to make both EncodeLabels and DecodeLabels private and just document the expected format in the FromEnv godoc: How do we expect users to use EncodeLabels? Won't they usually just set environment variables in something like a k8s yaml file or shell script? If so, how does it help to have a Go function exported?
} | ||
labels := strings.TrimSpace(os.Getenv(EnvVarLabels)) | ||
if labels == "" { | ||
return res, nil |
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.
If EnvVarType is empty, wouldn't it make more sense to return a nil
resource here?
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.
The type is not mandatory per our specs so I don't think we necessarily have to handle it specially here.
In general the empty resource is equivalent to the unset resource. Just that an empty struct requires less special handling by the caller, which seems preferable.
This package adds resources as a core concept to OpenCensus.
@rghetia @bogdandrutu @Ramonza @rakyll @jkohen As this is the core step of a relatively fundamental change, you may all want to take a quick look.