Skip to content

Writing Data to Guance via DataKit

This document primarily introduces how to write data to DataKit using this system. (That is: use DataFlux Func as a collector to connect with DataKit)

The sample data used in this article are random numbers. Therefore, for the overall appearance of the document, some screenshots have fictional elements and do not guarantee identical results every time you perform the operation.

The examples in this article will only work under correct configuration/operation of components like DataKit, DataWay, etc., from Guance and normal network conditions.

Always use the latest version of DataFlux Func for operations.

1. Background

In some cases, the collectors provided by Guance may not meet the needs for data reporting. At such times, you can use DataFlux Func as a collector for DataKit to report data.

This article assumes that the user has already correctly connected DataKit within DataFlux Func. For information on how to connect DataKit in DataFlux Func, please refer to:

2. Writing Data Insertion Functions

Enter the editor, create a script, and input the following sample script:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import time
import random

@DFF.API('DataKit Demo')
def datakit_demo():
    # Get the DataKit operation object
    datakit = DFF.CONN('datakit')

    # From 30 minutes ago to the current time, generate a random number between 90 and 110 per minute
    now = time.time()
    for i in range(30):
        value     = random.randint(95, 105)
        timestamp = now - i * 60

        # Write data as Metrics into DataKit
        _measurement = 'datakit_demo'             # Measurement
        _tags        = { 'type': 'some_integer' } # Tags
        _fields      = { 'value': value }         # Fields
        datakit.write_metric(measurement=_measurement, tags=_tags, fields=_fields, timestamp=timestamp)

        # Write data as Logs into DataKit
        _measurement = 'datakit_demo'                           # Measurement (i.e., source in logs)
        _tags        = { 'type': 'some_log' }                   # Tags
        _fields      = { 'message': 'value is: ' + str(value) } # Fields
        datakit.write_logging(measurement=_measurement, tags=_tags, fields=_fields, timestamp=timestamp)

    print('OK')

3.1. Debugging Functions

Click the "Execute" button, and if it runs correctly, it indicates success.

4. Publishing the Script

After modifying the script, it will be saved as a draft. You need to publish it to officially go live.

5. Configuring Automatic Function Execution

Functions decorated with @DFF.API(...) can be configured for automatic execution in the "Manage / Scheduled Tasks (Old Version: Automatic Trigger Configuration)" section.

6. Viewing Data in Guance

After the function runs normally, wait for a moment...

Switch to the "Metrics" section in Guance.

Select the Measurement and tags written earlier to view the generated data.

Switch to the "Logs" section in Guance.

Select the Measurement and tags written earlier to view the generated data.