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

Adding Resource API - v0 #81

Merged
merged 18 commits into from
Jan 4, 2019

Conversation

simonz130
Copy link
Contributor

- Added IResource definition based on
https://github.com/census-instrumentation/opencensus-proto/blob/master/src/opencensus/proto/resource/v1/resource.proto
- Added abstract class that implements IResource. Currently it's a scheleton for adding custom resources.
This is needed so that the library can identify the resource for which telemetry is collected.
src/OpenCensus/Resource/Resource.cs Outdated Show resolved Hide resolved
src/OpenCensus/Resource/Resource.cs Outdated Show resolved Hide resolved
src/OpenCensus/Resource/Resource.cs Outdated Show resolved Hide resolved
}

string key = keyValuePair[0].Trim();
string value = Regex.Replace(keyValuePair[1].Trim(), "^\"|\"$", string.Empty);
Copy link
Member

Choose a reason for hiding this comment

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

two calls to replace will be more efficient. At least compile the regex so it is not created every cycle of the loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

static Replace method doesn't work with Regex instances (pre-compiled), so I don't see a point in regex pre-compilation here. Also, I don't create a Regex instance every loop iteration - I pass a simple regex with two patterns to match and get a string back.

src/OpenCensus/Utils/Arguments.cs Outdated Show resolved Hide resolved
/// <returns>Whether condition is true</returns>
public static bool Check(bool condition, string errorMessage)
{
if (!condition)
Copy link
Member

Choose a reason for hiding this comment

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

this is one-liner in code. Do you need a separate method defined?

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 need some form of arguments checking and then either throwing exception or logging in invalid cases. We need this logic throughout the public API surface. So I prefer to have it in one place even if it's one-liner right now than to spread it in multiple places. Besides, there will be additional overloads soon, so I laid the foundation now.

Copy link
Member

Choose a reason for hiding this comment

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

What other overloads are you anticipating? From my expirience these helper methods only complicates understanding the code. You never know will it throw or ignore an error for instance.

Choose a reason for hiding this comment

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

As far as I understand, this is a simplified guava preconditions implementation is C#. It would be cool to have something like that on the platform or common-lib level, but unfortunately, there is nothing like that and couple 3rd party I've found - CuttingEdge.Conditions and Preconditions.NET are not really popular.

We also don't get any static code analysis from this when we implement them on our own.

The problem here is not only readability but also perf. A caller has to create an error string (which is not const) and sometimes do a bunch of operations to make error message descriptive enough (add values of Ids, some context).
And this string will be constructed even when the condition is false (almost always). So such checks should not be used on the hot path anyways.

So I tend to agree with Sergey, there are certain issues with this approach and I'd prefer we explicitly throw or write log in the code where it's needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I'm removing this in the meantime.

src/OpenCensus/Resource/Resource.cs Outdated Show resolved Hide resolved
src/OpenCensus/Resource/Resource.cs Outdated Show resolved Hide resolved
/// of resource information from the environment and progressive population as signals propagate from
/// the core instrumentation library to a backend's exporter.
/// </summary>
public interface IResource
Copy link
Member

Choose a reason for hiding this comment

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

is this interface supposed to be read only? If so - change IList to IEnumerable please.

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 is supposed to be readonly, but IEnumerable doesn't have Count and the types that will implement the interface can differ wildly (Azure/AWS/GCP resources). Also, typically, the tags will be stored in a list anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Why do you need Count? There is Count() method in Linq namespace if needed. But the common use case will be to enumerate, not index these, correct?

Copy link
Member

Choose a reason for hiding this comment

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

See also PR related to this issue: #64

/// </summary>
/// <param name="rawEnvironmentTags">Environment tags as a raw, comma separated string</param>
/// <returns>Environment Tags as a list</returns>
private static ITag[] ParseResourceLabels(string rawEnvironmentTags)
Copy link
Member

Choose a reason for hiding this comment

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

Can you please add some unit tests as part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

src/OpenCensus.Abstractions/Resource/IResource.cs Outdated Show resolved Hide resolved
/// <returns>Whether condition is true</returns>
public static bool Check(bool condition, string errorMessage)
{
if (!condition)
Copy link
Member

Choose a reason for hiding this comment

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

What other overloads are you anticipating? From my expirience these helper methods only complicates understanding the code. You never know will it throw or ignore an error for instance.

labels.Add(new Tag(new TagKey(key), new TagValue(value)));
}

return labels.ToArray();
Copy link

@lmolkova lmolkova Dec 26, 2018

Choose a reason for hiding this comment

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

this is an extra allocation, could we avoid it by using the same type everywhere? (return type, private field, IResource.Tags)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you mean TagKey and TagValue constructors - I've fixed that. If you mean labels.ToArray call - there is not much you can do. The number of labels is not known in advance so you have to parse them in runtime. I could return a list but once labels were calculated, they should stay immutable. Finally, this calculation happens once in a lifetime of the process.

Copy link

@lmolkova lmolkova left a comment

Choose a reason for hiding this comment

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

Left some minor comments. The biggest concern is IList<ITag> Tags: what operations are expected on the Tags and why it's not IEnumerable

SergeyKanzhelev and others added 11 commits January 2, 2019 22:31
* snupkg, update the linux image and remove travis

* removed publish command. It's unnecessary

* remove duplicated commands

* added comment

* use buildProperties instead of arguments

* removed comment and updated to ANY 2.2

* latest 2.2
- Added IResource definition based on
https://github.com/census-instrumentation/opencensus-proto/blob/master/src/opencensus/proto/resource/v1/resource.proto
- Added abstract class that implements IResource. Currently it's a scheleton for adding custom resources.
This is needed so that the library can identify the resource for which telemetry is collected.
* readme and changelog updates

* fix issues
* fix the license and apply NuGet/Announcements#32

* missed a few files
…re (census-instrumentation#80)

* better name

* make it work

* revert wrong addition

* addressed code review

* fix build failure and added comment for the sampler

* added changelog entry

* added route into the attributs as well
* few IList replaced with IEnumerable

* parent links from IList to IEnumerable

* a few more of IList -> IEnumerable

* one more

* stats event with IEnumerable now

* smal utils method should not use IList

* another utility method
…ion#85)

* documents

* first warning was implemented

* first version of docs complete
…ecking

2) Added a few events logging
3) Added tests for Resource parsing (labels and resource type)
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

Copy link
Member

@SergeyKanzhelev SergeyKanzhelev left a comment

Choose a reason for hiding this comment

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

It is almost ready for merge. Would you please update error messages. Also it feels for me that IEnumerable is still preferable type there.

src/OpenCensus/Implementation/OpenCensusEventSource.cs Outdated Show resolved Hide resolved
src/OpenCensus/Implementation/OpenCensusEventSource.cs Outdated Show resolved Hide resolved
src/OpenCensus/Implementation/OpenCensusEventSource.cs Outdated Show resolved Hide resolved
@simonz130
Copy link
Contributor Author

I've changed from IList to IEnumerable, but I still think that it's better to use IList when the meaning is "holding a list of elements" to allow not only iterating using foreach loops or Linq but also regular for loops that are more performant.

Simon Zeltser added 2 commits January 3, 2019 14:32
 - Changed IList to IEnumerable
 - Refined error messages to more details.
@simonz130
Copy link
Contributor Author

Can you please merge it?

@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

@SergeyKanzhelev SergeyKanzhelev dismissed lmolkova’s stale review January 4, 2019 00:28

I think all feedback was addressed

@SergeyKanzhelev SergeyKanzhelev merged commit 68bd833 into census-instrumentation:develop Jan 4, 2019
@simonz130 simonz130 deleted the AddResource branch January 4, 2019 06:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants