-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Dynatrace/release_130
Prepare release v1.3.0
- Loading branch information
Showing
19 changed files
with
532 additions
and
146 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,4 @@ | ||
# OneAgent SDK for Python forking sample | ||
|
||
This example demonstrates how to use the [OneAgent SDK for | ||
Python](https://github.com/Dynatrace/OneAgent-SDK-for-Python) with forked child processes. |
This file contains 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,128 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2019 Dynatrace LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
'''This example demonstrates how to use the OneAgent SDK for Python in a | ||
parent/child process environment. The OneAgent SDK for Python will be initialized | ||
in the parent process and the child processes can then use the SDK. | ||
Note: this example will only work on Linux. There's no Windows support available. | ||
''' | ||
|
||
import os | ||
import sys | ||
|
||
import oneagent # SDK initialization functions | ||
import oneagent.sdk as onesdk # All other SDK functions. | ||
|
||
try: # Python 2 compatibility. | ||
input = raw_input #pylint:disable=redefined-builtin | ||
except NameError: | ||
pass | ||
|
||
|
||
getsdk = oneagent.get_sdk # Just to make the code shorter. | ||
|
||
|
||
def do_some_fancy_stuff(proc_number): | ||
sdk = getsdk() | ||
|
||
# The agent state in the child process should be ACTIVE (0). | ||
print('Agent state (child process #{}): {}'.format(proc_number, sdk.agent_state), flush=True) | ||
|
||
print('Agent found:', sdk.agent_found) | ||
print('Agent is compatible:', sdk.agent_is_compatible) | ||
print('Agent version:', sdk.agent_version_string) | ||
|
||
# This call below will complete the OneAgent for Python SDK initialization and then it | ||
# will start the tracer for tracing the custom service | ||
with sdk.trace_custom_service('my_fancy_transaction', 'MyFancyService #{}'.format(proc_number)): | ||
print('do some fancy stuff') | ||
|
||
def create_child_process(proc_number): | ||
pid = os.fork() | ||
if pid == 0: | ||
print('child #{} is running ...'.format(proc_number)) | ||
do_some_fancy_stuff(proc_number) | ||
print('child #{} is exiting ...'.format(proc_number)) | ||
sys.exit(0) | ||
|
||
return pid | ||
|
||
def fork_children(): | ||
print('now starting children ...', flush=True) | ||
pid_1 = create_child_process(1) | ||
pid_2 = create_child_process(2) | ||
|
||
print('waiting for child #1 ...', flush=True) | ||
os.waitpid(pid_1, 0) | ||
print('child #1 exited', flush=True) | ||
|
||
print('waiting for child #2 ...', flush=True) | ||
os.waitpid(pid_2, 0) | ||
print('child #2 exited', flush=True) | ||
|
||
print('all children exited', flush=True) | ||
|
||
def main(): | ||
# This gathers arguments prefixed with '--dt_' from sys.argv into the | ||
# returned list. See also the basic-sdk-sample. | ||
sdk_options = oneagent.sdkopts_from_commandline(remove=True) | ||
|
||
# Before using the SDK you have to initialize the OneAgent. In this scenario, we | ||
# initialize the SDK and prepare it for forking. | ||
# | ||
# Passing in the sdk_options is entirely optional and usually not required | ||
# as all settings will be automatically provided by the Dynatrace OneAgent | ||
# that is installed on the host. | ||
# | ||
# To activate the forking support add the optional 'forkable' parameter and set it to True. | ||
# | ||
# If you run this example on Windows then you'll get an "Invalid Argument" error back | ||
# because there's no forking support for Windows available. | ||
init_result = oneagent.initialize(sdk_options, forkable=True) | ||
try: | ||
if init_result.error is not None: | ||
print('Error during SDK initialization:', init_result.error) | ||
|
||
# While not by much, it is a bit faster to cache the result of | ||
# oneagent.get_sdk() instead of calling the function multiple times. | ||
sdk = getsdk() | ||
|
||
# The agent state is one of the integers in oneagent.sdk.AgentState. | ||
# Since we're using the 'forkable' mode the state will be TEMPORARILY_INACTIVE (1) on Linux. | ||
print('Agent state (parent process):', sdk.agent_state) | ||
|
||
# The instance attribute 'agent_found' indicates whether an agent could be found or not. | ||
print('Agent found:', sdk.agent_found) | ||
|
||
# If an agent was found but it is incompatible with this version of the SDK for Python | ||
# then 'agent_is_compatible' would be set to false. | ||
print('Agent is compatible:', sdk.agent_is_compatible) | ||
|
||
# The agent version is a string holding both the OneAgent version and the | ||
# OneAgent SDK for C/C++ version separated by a '/'. | ||
print('Agent version:', sdk.agent_version_string) | ||
|
||
if init_result.error is None: | ||
fork_children() | ||
input('Now wait until the path appears in the UI ...') | ||
finally: | ||
shutdown_error = oneagent.shutdown() | ||
if shutdown_error: | ||
print('Error shutting down SDK:', shutdown_error) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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,55 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright 2018 Dynatrace LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import io | ||
|
||
from setuptools import setup | ||
|
||
with io.open('README.md', encoding='utf-8') as readmefile: | ||
long_description = readmefile.read() | ||
del readmefile | ||
|
||
setup( | ||
py_modules=['fork_sdk_sample'], | ||
zip_safe=True, | ||
name='oneagent-sdk-fork-sample', | ||
version='0.0', # This sample is not separately versioned | ||
|
||
install_requires=['oneagent-sdk==1.*,>=1.3'], | ||
|
||
description='OneAgent SDK for Python: Fork sample application', | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
url='https://github.com/Dynatrace/OneAgent-SDK-for-Python', | ||
maintainer='Dynatrace LLC', | ||
maintainer_email='[email protected]', | ||
license='Apache License 2.0', | ||
entry_points={ | ||
'console_scripts': ['oneagent-sdk-basic-sample=fork_sdk_sample:main'], | ||
}, | ||
classifiers=[ | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved', | ||
'License :: OSI Approved :: Apache Software License', # 2.0 | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2', | ||
'Programming Language :: Python :: 2.7', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: Implementation :: CPython', | ||
'Operating System :: POSIX :: Linux', | ||
'Operating System :: Microsoft :: Windows', | ||
'Topic :: System :: Monitoring' | ||
]) |
Oops, something went wrong.