Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V8/new telemetry (Influx implementation) #3424

Open
wants to merge 4 commits into
base: v8/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/hash": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@influxdata/influxdb-client": "^1.35.0",
"@polkadot/api": "^9.3.2",
"@polkadot/keyring": "^10.1.7",
"@polkadot/util": "^10.1.7",
Expand Down
88 changes: 88 additions & 0 deletions src/modules/repository/implementation/influx/influx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { InfluxDB, Point } from '@influxdata/influxdb-client';

Check failure on line 1 in src/modules/repository/implementation/influx/influx.js

View workflow job for this annotation

GitHub Actions / lint

Unable to resolve path to module '@influxdata/influxdb-client'
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';

// Dynamically resolve the directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Dynamically locate and load the `.env` file
function setEnvParameters() {
const projectRoot = path.resolve(__dirname, '../../../../..'); // Adjust this path if needed
const realRoot = fs.realpathSync(projectRoot); // Resolve symbolic link to real path
const envPath = path.join(realRoot, '.env'); // Append `.env` to resolved path

// Check if the `.env` file exists
if (fs.existsSync(envPath)) {
//console.log(`Loading environment variables from: ${envPath}`);

Check failure on line 19 in src/modules/repository/implementation/influx/influx.js

View workflow job for this annotation

GitHub Actions / lint

Expected exception block, space or tab after '//' in comment
dotenv.config({ path: envPath }); // Load the environment variables
} else {
console.error(`.env file not found at: ${envPath}`);

Check warning on line 22 in src/modules/repository/implementation/influx/influx.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
process.exit(1); // Exit if `.env` is not found
}
}

// Call `setEnvParameters` to load the environment variables
setEnvParameters();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look how it's done in sequelize-repository.js line 58


// Preconfigure the InfluxDB variables using environment variables
const influxDBUrl = process.env.INFLUXDB_URL;
const influxDBToken = process.env.INFLUXDB_TOKEN;
const influxDBOrg = process.env.INFLUXDB_ORG;
const influxDBBucket = process.env.INFLUXDB_BUCKET;

// Check if required variables are present
if (!influxDBUrl || !influxDBToken || !influxDBOrg || !influxDBBucket) {
console.error('One or more required environment variables are missing:');

Check warning on line 38 in src/modules/repository/implementation/influx/influx.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use console but logger

console.error({

Check warning on line 39 in src/modules/repository/implementation/influx/influx.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
INFLUXDB_URL: influxDBUrl,
INFLUXDB_TOKEN: influxDBToken,
INFLUXDB_ORG: influxDBOrg,
INFLUXDB_BUCKET: influxDBBucket,
});
process.exit(1); // Exit the script if variables are missing
}

// Initialize the InfluxDB client (use the imported class here)
const influxDBClient = new InfluxDB({
url: influxDBUrl,
token: influxDBToken,
});

// Create the write API
const writeApi = influxDBClient.getWriteApi(influxDBOrg, influxDBBucket, 'ns');

// Function to send event telemetry to InfluxDB
export async function sendEventTelemetry(
operationId,
blockchainId,
name,
timestamp,
value1 = null,
value2 = null,
value3 = null
) {
try {
const point = new Point('event')
.tag('operationId', operationId)
.tag('blockchainId', blockchainId)
.stringField('name', name)
.intField('timestamp', timestamp)
.stringField('value1', value1 !== null ? value1.toString() : '')
.stringField('value2', value2 !== null ? value2.toString() : '')
.stringField('value3', value3 !== null ? value3.toString() : '');

writeApi.writePoint(point);
await writeApi.flush();
//console.log('Event telemetry logged to InfluxDB');

Check failure on line 79 in src/modules/repository/implementation/influx/influx.js

View workflow job for this annotation

GitHub Actions / lint

Expected exception block, space or tab after '//' in comment
} catch (err) {
console.error('Error sending telemetry to InfluxDB:', err);

Check warning on line 81 in src/modules/repository/implementation/influx/influx.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
throw err;
}
}

export default {
sendEventTelemetry,
};
5 changes: 3 additions & 2 deletions src/modules/repository/repository-module-manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BaseModuleManager from '../base-module-manager.js';
import influx from './implementation/influx/influx.js';

class RepositoryModuleManager extends BaseModuleManager {
getName() {
Expand Down Expand Up @@ -217,15 +218,15 @@
value2 = null,
value3 = null,
) {
return this.getRepository('event').createEventRecord(
return influx.sendEventTelemetry(

Check failure on line 221 in src/modules/repository/repository-module-manager.js

View workflow job for this annotation

GitHub Actions / lint

Caution: `influx` also has a named export `sendEventTelemetry`. Check if you meant to write `import {sendEventTelemetry} from './implementation/influx/influx.js'` instead
Copy link
Collaborator

@Mihajlo-Pavlovic Mihajlo-Pavlovic Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be done like this, I think that this function createEventRecord should be moved to the telemetry module and then called from there

operationId,
blockchainId,
name,
timestamp,
value1,
value2,
value3,
);
);
}

async getUnpublishedEvents() {
Expand Down
Loading