What Grafana version= Grafana v10.0.2 Operating system= Windows 11
Note - I am very new to Grafana and have only been using it for a week.
Goal - I want to visualize my Elasticsearch logs in a table panel. Currently my Dashboard has a table panel with the appropriate Elasticsearch datasource and a textbox to enter a serial_num which then searches across the Data for a specific log. Each log has a unique 8 digit number(serial_num) associated with it, I have created a textbox variable to enter a desired serial number, which searches across the logs and returns the results on the table. This functionality is already working and in place.
I need to extend this search functionality further. The way the serial number is structured in one of our legacy systems is XXXX1234XXXX5678.
Here the Xâs indicate the characters that I donât want and would like to concatenate 1234 and 5678 to create a serial number 12345678 (this should be used to search across the serial number in the table panel).
The textbox variable is currently called $Serial_num. And I am using it in the query section as
Query = ID:$Serial_Num
Where ID is the field in the table and $Serial_num is one of the serial numbers in the ID column.
Questions:
- I want to first place a check on the $Serial_Num variable. If it is 8 digits it should be directly used to search aka ID:$Serial_Num. If not then,
- If it is 16 digits long, I want to be able to extract the characters 1234, 5678 from a string of the format XXXX1234XXXX5678 and then concatenate them to 12345678 and then use it in the query as:
$Serial_num = XXXX1234XXXX5678
$Newserial_num = 12345678
Query = ID:$Newserial_num
I have tried adding a TS script in the text panel to create a new variable out of the $OldSerial_num but it doesnât seem to be working for me.
Is there a different approach to this? How can I achieve this regex query?
Thanks!
Please post your ts script?
Also is the search being done against es or against data already in table panel
Hi @yosiasz here is my TS Script -
##############################################################
const searchString: string = $Serial_ID;
if (searchString.length === 15) {
const extractedString1: string = searchString.substring(4,8);
const extractedString2: string = searchString.substring(11,14);
const concatenatedString: string = extractedString1 + extractedString2;
const newVariable: string = âNewVariable=â + concatenatedString;
console.log(âConcatenated Stringâ + concatenatedString + â\nâ + newVariable);
} else {
console.log(âInvalid inputâ);
}
##########################################################
I might be wrong with the script as well, let me know if any changes should be made!
I donât see anything for length of 8 other than console.log
Please provide fully working code with test harness data.
I havenât yet done anything for string of length 8 because it needs to be used as it is, so the query would simply be ID:$Serial_ID.
Only if it is 16 characters long then it should be manipulated and the $NewVariable should be used in the query.
ID:$NewVariable.
As for the test data - I am afraid I cant share it but the test data has the fields like
- ID
- Timestamp
- GitCommit
4.GitAuthor
etc.
well the code looks fine to me. I tested it locally and it works not sure what the issue is other than the value you put XXXX1234XXXX5678
is not === 15 but 16.
Also do you have a variable named $Newserial_num configured in variables?
Also fix this? redundant vars you dont use elsewhere
const concatenatedString: string = searchString.substring(4,8)+ searchString.substring(11,14);
No, I donât have a variable named $NewVariable configured anywhere else. I am creating it in the script so that I can use it in my Table Panel. Let me try it once again, I mightâve made some minor mistakes!
Also where are you able to see the console.log output - it doesnt print anything on my text panel 
Hello,
use this instead?
const serial = 'XXXX1212XXXX3233'
parseInt(serial.replace(/[a-z]+/gi, ''))

Like this you donât have to check for the length as you can extract any numbers in a string and convert them to Integer.
*I just read the code and suggest to change it, I didnât read all your posts.
2 Likes
console.log will print in the browser console like @clevernessisamyth showed above not in the text panel
2 Likes
well not sure how the elasticsearch query would be fed this value if it is not declared in the dashboard?
Sorry I am really new to Grafana, I assumed creating a variable in the Text Panel would allow me to use that variable in the Table Panel since they both belong to the same dashboard.
so is this new variable being used to filter stuff in the table panel or fed to elastic search query?
The table panel has the data source as ElasticSearch, and there are 2-3 lucene queries within the table panel (I am assuming here that the query directly queries the elasticsearch logs and just visualizes the results in the table.)
So in short, yes the variable from the text panel needs to be used in a query. To clarify - pasting a picture here to let you know what I am talking about.
1 Like
Click on Options? Is there a regex?
Unfortunately no, the only Options is âLimitâ on the number of logs to be displayed.
maybe a different approach to this? What visualization will you be using for this? time series or bar chart? or what
No other visualization - this dashboard is more like a query tool - where in I can see any of the logs and the resources related to it using a search tool (in this case searching a log using its ID). I need to integrate the NewVariable into the query instead of $Serial_ID and my dashboard should be usable enough.
interesting use case. why not just use kibana or some other es search tools? what does grafana provide you other tools cannot?
XD Client Requirements - they already have Grafana Enterprise and want to use it.
1 Like