Support

Code Functions

Javascript Code

This feature is currently in beta.

If you would like early access, please reach out to us.

Use Javascript code snippets to transform Streamfold events.


Intro

With the Javascript code function you can transform Streamfold events using familiar Javascript. This is useful when the built-in transform functions don't support an existing use case. For example, here's a small Javascript code sample that lower cases the service name.

const { fields } = getEvent();

fields.service = fields.service.toLowerCase()

Required Configuration

  • Javascript snippet: A snippet of Javascript code to execute per event.

Examples

Retrieving fields and metadata

The getEvent() function returns the event fields and the event metadata. The fields represent the top-level fields of the event and meta represents the metadata of the event.

const { fields, meta } = getEvent();

if (meta.source.type === "datadog_agent" && meta.type === "metrics") {
    fields['_num_metrics'] = fields.series.length;
}

if (meta.source.type === "datadog_agent" && meta.type === "sketches") {
    fields['_num_metrics'] = fields.sketches.length;
}

Dropping or replacing event fields

Any returned value from the code snippet will replace the fields structure of the event. Returning null will drop the event entirely.

Return a new event with only the top-level fields service and host.

const { fields: f } = getEvent();

return {
    service: f.serviceName,
    host: f.hostname,
}

A return without a value (undefined) will default to the existing fields.

const { fields: f, meta: m } = getEvent();

// Keep logs
if (m.type === "logs") {
    return
}

// Drop all but metrics
if (m.type !== "metrics") {
    return null
}

// Keep only metrics from production
f.series = f.series.filter((s) => s.tags.includes("env:production"))

Event time

The event time is included in the events as the field _time. It is encoded in RFC3339Nano format as a string.

const { fields: f } = getEvent();

const d = new Date(f['_time']);
f.month = d.getMonth()

Custom functions

Custom functions can be defined and used in code snippets as well.

function resStrAttr(res, key) {
    const a = res.attributes.find((a) => a.key === key);
    
    return a.value?.stringValue;
}

const { fields: f } = getEvent();

// strip logs of currencyservice
f.resourceLogs = f.resourceLogs.filter((r) => resStrAttr(r.resource, "service.name") !== "currencyservice");

if (f.resourceLogs.length === 0) {
    return null
}

Notes

The Javascript code function is compatible with ECMAScript 5.1.

The Javascript code function is a powerful way to extend the capabilities of Streamfold beyond what built-in functions can provide; however, it comes at the cost of introducing overhead in stream processing. Therefore, it is still better to use built-in functions when possible. Over time the set of built-in functions will expand to cover more use cases. Additionally, the performance of Javascript code execution will be improved.

Previous
Bloblang Code