Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Add resource package #878

Merged
merged 10 commits into from
Oct 29, 2018
Merged

Add resource package #878

merged 10 commits into from
Oct 29, 2018

Conversation

fabxc
Copy link
Contributor

@fabxc fabxc commented Aug 22, 2018

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.

This package adds resources as a core concept to OpenCensus.

Signed-off-by: Fabian Reinartz <[email protected]>
@semistrict
Copy link
Contributor

@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?

Copy link
Contributor

@rakyll rakyll left a 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.

},
},
}
for _, c := range cases {
Copy link
Contributor

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.

{s: `a, extra=chars`, fail: true},
}
for i, c := range cases {
t.Logf("test %d: %s", i, c.s)
Copy link
Contributor

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.


func TestMerge(t *testing.T) {
cases := []struct {
a, b, expect *Resource
Copy link
Contributor

Choose a reason for hiding this comment

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

s/expect/want


func TestDecodeTags(t *testing.T) {
cases := []struct {
s string
Copy link
Contributor

Choose a reason for hiding this comment

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

s/s/encoded

func TestDecodeTags(t *testing.T) {
cases := []struct {
s string
expect map[string]string
Copy link
Contributor

Choose a reason for hiding this comment

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

wantTags

}

// FromEnvVars loads resource information from the OC_TYPE and OC_RESOURCE_TAGS environment variables.
func FromEnvVars(context.Context) (*Resource, error) {
Copy link
Contributor

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 Show resolved Hide resolved
// 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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add godoc.

return s
}

// We accept domain names and paths as tag keys. Values may be quoted or unquoted in general.
Copy link
Contributor

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.

}

// EncodeTags encodes a tags to a string as provided via the OC_RESOURCE_TAGS environment variable.
func EncodeTags(tags map[string]string) (s string) {
Copy link
Contributor

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]>
{encoded: `missing=leading-quote"`, wantFail: true},
{encoded: `extra=chars, a`, wantFail: true},
{encoded: `a, extra=chars`, wantFail: true},
{encoded: `a, extra=chars`, wantFail: true},
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicate.


// 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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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 (:

Copy link
Contributor

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().

resource/resource_test.go Show resolved Hide resolved
@fabxc
Copy link
Contributor Author

fabxc commented Sep 13, 2018

Can someone take a look at the appveyor build failure? It doesn't seem like it's related to the code:

go test -race -v .\...
?   	go.opencensus.io	[no test files]
?   	go.opencensus.io/examples/exporter	[no test files]
# runtime/cgo
exec: "gcc": executable file not found in %PATH%
FAIL	go.opencensus.io/exporter/jaeger [build failed]
FAIL	go.opencensus.io/exporter/prometheus [build failed]
FAIL	go.opencensus.io/exporter/stackdriver/propagation [build failed]

Signed-off-by: Fabian Reinartz <[email protected]>
@rghetia
Copy link
Contributor

rghetia commented Sep 13, 2018

@fabxc ignore the appveyor build failure. (see #894 (comment))

Signed-off-by: Fabian Reinartz <[email protected]>
@rakyll
Copy link
Contributor

rakyll commented Sep 22, 2018

/cc @Ramonza can you review?

envVarTags = "OC_RESOURCE_TAGS"
)

// Resource describes an entity about which data is exposed.
Copy link
Contributor

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"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe give some examples

Tags map[string]string
}

// EncodeTags encodes a tags to a string as provided via the OC_RESOURCE_TAGS environment variable.
Copy link
Contributor

Choose a reason for hiding this comment

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

encodes a tags map?


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.
Copy link
Contributor

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

return m, nil
}

// FromEnv loads resource information from the OC_TYPE and OC_RESOURCE_TAGS environment variables.
Copy link
Contributor

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/

// 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)),
Copy link
Contributor

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?

Copy link
Contributor Author

@fabxc fabxc Sep 24, 2018

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.

if res.Type == "" {
res.Type = b.Type
}
for k, v := range b.Tags {
Copy link
Contributor

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.


// 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.
Copy link
Contributor

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".

resource/resource.go Show resolved Hide resolved
Signed-off-by: Fabian Reinartz <[email protected]>
Copy link
Contributor

@semistrict semistrict left a 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?

resource/resource.go Show resolved Hide resolved
Fabian Reinartz added 3 commits October 10, 2018 14:38
Signed-off-by: Fabian Reinartz <[email protected]>
Labels map[string]string
}

// EncodeLabels encodes a labels map to a string as provided via the OC_RESOURCE_labelS environment variable.
Copy link
Contributor

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 {
Copy link
Contributor

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)?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment about public.

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.


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.
Copy link
Contributor

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.

type Detector func(context.Context) (*Resource, error)

// NewDetectorFromResource returns a detector that will always return resource r.
func NewDetectorFromResource(r *Resource) Detector {
Copy link
Contributor

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.

var _ Detector = FromEnv

// Merge resource information from b into a. In case of a collision, a takes precedence.
func Merge(a, b *Resource) *Resource {
Copy link
Contributor

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 Show resolved Hide resolved
// 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 {
Copy link
Contributor

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.

Copy link
Contributor

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]>
// 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
Copy link
Contributor

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]>
@fabxc
Copy link
Contributor Author

fabxc commented Oct 22, 2018

@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) {
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants