Skip to content

Commit

Permalink
Build a compound ID with a combination of attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
asllop committed Jan 18, 2024
1 parent 0befcbf commit 9226a0b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,18 @@ Queries for `EventLogFile` requiere the following fields to be present:
- `LogDate`
- `LogFile`

For queries of other event types there is no minimum set of attributes requiered, but they will only be cached (when `cache_enabled` is `True`) if `Id` is present.
For queries of other event types there is no minimum set of attributes requiered, but a unique identifier is requiered to be able to store the events on Redis (when `cache_enabled` is `True`). If the `Id` field is present, it will be used. Otherwise it will check for the id key in the query environment config:

```yaml
queries: [
{
query: "SELECT EventName, EventType, UsageType, Client, Value, StartDate, EndDate FROM PlatformEventUsageMetric ...",
id: ["Client", "Value", "StartDate", "EndDate"]
}
]
```

In this case, the integration will combine the fields `Client`, `Value`, `StartDate`, and `EndDate` to form a unique identifier for each event of the type `PlatformEventUsageMetric`.

## Usage

Expand Down
5 changes: 5 additions & 0 deletions src/newrelic_logging/query_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from .query import Query
import string

# NOTE: this sandbox can be jailbroken using the trick to exec statements inside an exec block, and run an import (and other tricks):
# https://book.hacktricks.xyz/generic-methodologies-and-resources/python/bypass-python-sandboxes#operators-and-short-tricks
# https://stackoverflow.com/a/3068475/2076108
# Would be better to use a real sandbox like https://pypi.org/project/RestrictedPython/ or https://doc.pypy.org/en/latest/sandbox.html
# or parse a small language that only supports funcion calls and binary expressions.
def sandbox(code):
__import__ = None
__loader__ = None
Expand Down
16 changes: 15 additions & 1 deletion src/newrelic_logging/salesforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import redis
from requests import RequestException
import copy
import hashlib
from .query_env import substitute
from .auth_env import Auth
from .query import Query
Expand Down Expand Up @@ -344,7 +345,7 @@ def make_single_query(self, query_obj: Query) -> Query:
}
query = substitute(args, query_obj.get_query(), env)
query = query.replace(' ', '+')

query_obj.set_query(query)
return query_obj

Expand Down Expand Up @@ -470,6 +471,19 @@ def pack_event_into_log(self, rows, query: Query):
if self.data_cache.check_cached_id(record_id):
# Record cached, skip it
continue
else:
id_keys = query.get_env().get("id", [])
compound_id = ""
for key in id_keys:
compound_id = compound_id + str(row.get(key, ""))
if compound_id != "":
m = hashlib.sha3_256()
m.update(compound_id.encode('utf-8'))
row['Id'] = m.hexdigest()
record_id = row['Id']
if self.data_cache.check_cached_id(record_id):
# Record cached, skip it
continue

timestamp_attr = query.get_env().get("timestamp_attr", "CreatedDate")
if timestamp_attr in row:
Expand Down

0 comments on commit 9226a0b

Please sign in to comment.