Skip to content

Conversation

jqll
Copy link
Contributor

@jqll jqll commented Mar 2, 2019

Add code samples for https://cloud.google.com/profiler.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Mar 2, 2019
@jqll
Copy link
Contributor Author

jqll commented Mar 2, 2019

@tswast sounds like I can't add reviewers. Could you take a look at this PR? We'd like to add some code samples for https://cloud.google.com/profiler. Also, could we request write access to this repo?

@aalexand @kalyanac could you also take a look? This change adds a quickstart sample that works for GCE, GKE and local environment. App Engine samples are to be added.


# [END profiler_python_quickstart]
while True:
pass
Copy link

Choose a reason for hiding this comment

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

The flame graph is probably going to look quite boring. Would it make sense to make it more interesting by at least having a named function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some functions so that the profile shows CPU time percentage as expected.

# Profiler initialization, best done as early as possible.
try:
googlecloudprofiler.start(
service='service-name',
Copy link

Choose a reason for hiding this comment

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

In https://github.com/GoogleCloudPlatform/golang-samples/blob/master/profiler/profiler_quickstart/main.go we specify more realistic service name (and no service version) rather than placeholder string. Should we try to be consistent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use a more realistic servce name. This will be used as the code snippet for using Python agent. I slightly prefer keeping the service version, like this: https://github.com/GoogleCloudPlatform/golang-samples/blob/master/profiler/snippets/snippets.go#L28?

# project_id='my-project-id',
)
except (ValueError, NotImplementedError) as e:
print(e) # Handles errors here
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you provide some explanation for when ValueError or NotImplementedError might occur?

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 a little long and it will be presented in the doc surrounding this code snippet. I'm a little hesitate to make this code snippet too long.

# project_id must be set if not running on GCP.
# project_id='my-project-id',
)
except (ValueError, NotImplementedError) as e:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: exc not e

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.

googlecloudprofiler.start(
service='hello-profiler',
service_version='1.0.1',
verbose=3,
Copy link
Contributor

Choose a reason for hiding this comment

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

3 is a magic number. Does the client library provide any constants which may be used instead? Otherwise create a constant for the sample.

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.

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 just added a comment. The client code doesn't provide constants for logging levels. This sample will serve as a code snippet of calling the start function. I'm a little hesitate to create a constant, which may confuse the reader.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a FR to add such constants to the client? I think such constants would be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created a FR. Though I'm not sure if we definitely want to do that change, the number is consistent with the profiling agents in other languages.

googlecloudprofiler.start(
service='hello-profiler',
service_version='1.0.1',
verbose=3,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a FR to add such constants to the client? I think such constants would be useful.

@tswast tswast merged commit 3aa7511 into GoogleCloudPlatform:master Mar 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants