
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Mona’s SDK is a python based package which enables you to securely access Mona’s API and export your data to Mona directly from within your code, either message-by-message or as a batch.
$ pip install mona_sdk
We recommend you get acquainted with Mona's key concepts in our docs.
MonaSingleMessage: A dataclass wrapping all needed fields for a Mona data exporting message, containing the following:
from mona_sdk.client import Client, MonaSingleMessage
import time
api_key = <An API key is accessible in the admin page in your dashboard>
secret = <secret corresponding to the given api_key>
my_mona_client = Client(api_key, secret)
# One can send a single message to Mona's servers by calling export() with a
# MonaSingleMessage object:
succeed_to_export = my_mona_client.export(MonaSingleMessage(
message={'monitoring_information_1': '1', 'monitoring_information_2': '2'},
contextClass='MY_CONTEXT_CLASS_NAME',
contextId='CONTEXT_INSTANCE_UNIQUE_ID',
exportTimestamp=time.time(),
action="OVERWRITE"
))
# Another option is to send a batch of messages to Mona using export_batch:
messages_batch_to_mona = []
for context_instance in my_data:
messages_batch_to_mona.append(
MonaSingleMessage(
message=context_instance.relevant_monitoring_information,
contextClass="MY_CONTEXT_CLASS_NAME",
contextId=context_instance.unique_id,
exportTimestamp=context_instance.timestamp,
)
)
# Use dafault_action to select a default action for messages with no specified action.
export_result = my_mona_client.export_batch(
messages_batch_to_mona,
default_action="ADD",
)
Mona sdk provides a simple API to access your information and control your configuration and data on Mona. You can see all functions info and examples on our docs under REST API. Notice that the responses (since version 0.0.46) match the 'Service responses' section below, which is a bit different from the responses for the REST API.
The structure of the response for the different services is as follows:
{
"success": <was the request successful>, (bool)
"data": <the returned response data> (str|dict|list|None)
"error_message": <explains the error, if occured> (str)
}
Arguments:
new_configuration = {
"MY_CONTEXT_CLASS": {
"fields": <fields dict>,
"field_vectors": <field vectors dict>
"stanzas_global_defaults": <global defaults dict>,
"stanzas": <stansas dict>,
"notifications": <notifications dict>
}
}
author = 'author@mycompany.io'
upload_result = my_client.upload_config(new_configuration, "My commit message", author)
# the return value format will be:
# upload_result == {
# "success": <was the upload successful>, (bool)
# "data": {"new_config_id": <the new configuration ID> (str), "new_config":<the config that was uploaded> (dict)}
# "error_message": <explains the error, if occured> (str)
#}
Mona uses several environment variables you can set as you prefer:
Another way to control these behaviors is to pass the relevant arguments to the client constructor as follows (the environment variables are used as defaults for these arguments, and by passing these arguments to the constructor you can override the default values you set with the environment variables):
my_mona_client = Client(
api_key,
secret,
raise_authentication_exceptions=True,
raise_export_exceptions=True,
raise_service_exceptions=True,
num_of_retries_for_authentication=6,
wait_time_for_authentication_retries=0,
should_log_failed_messages=True,
filter_none_fields_on_export=True,
default_sampling_rate=0.1,
context_class_to_sampling_rate={"class1": 0.5, "class2": 1},
)
Unrelated to the actual data being exported, Mona's client may log debug/info/warning/error for various reasons, including to help with debugging communications with Mona's server. To make logging as adaptable to your system as possible, Mona is using its own logger named "mona-logger". You can configure it in your code by just calling
logging.getLogger("mona-logger")
and then setting handlers and formatters as you please.
You can also configure Mona's logging using the following environment variables:
Avoid using field names with "MONA" as their prefix, Mona uses this pattern internally. If you do that, these fields will be changed to a have prefix of "MY_MONA" before exporting to Mona's servers.
The client's tests are written using unittest framework.
In order to run the tests type the following to your shell:
python -m unittest mona_sdk/tests/client_tests.py
Same services as the regular client are provided in a non-blocking version in the AsyncClient, in order to give you the option to embed the code you write using this package with asyncIO code.
AsyncClient Constructor parameters:
event_loop (_UnixSelctorEventLoop): (optional) The event loop that will manage the threads. If not provided, a default is used.
executor (TreadPoolExecutor): (optional) The executor that will manage the thread pool. If not provided, a default is used.
When using AsyncClient, while all the regular (synchronous) client functions are still supported, you can simply add "_async" suffix to any function (e.g export_async() instead of export(); export_batch_async() instead of export_batch() etc). The async version of the methods accept the same parameters as the synchronous version, in addition to the following parameters:
An example for using export_batch_async to send data to Mona asynchronously, and then printing the result and exception (if occurred):
from mona_sdk import MonaSingleMessage
from mona_sdk.async_client import AsyncClient
import asyncio
async def main():
api_key = <An API key is accessible in the admin page in your dashboard>
secret = <secret corresponding to the given api_key>
my_mona_async_client = AsyncClient(api_key, secret)
messages_batch_to_mona = []
for context_instance in my_data:
messages_batch_to_mona.append(
MonaSingleMessage(
message=context_instance.relevant_monitoring_information,
contextClass="MY_CONTEXT_CLASS_NAME",
contextId=context_instance.unique_id,
exportTimestamp=context_instance.timestamp,
)
)
task = asyncio.create_task(my_mona_async_client.export_batch_async(messages_batch_to_mona))
await task
print(task.result())
print(task.exception())
asyncio.run(main())
FAQs
SDK for communicating with Mona's servers
We found that mona-sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.