The generateParserFromTemplate() GraphQL query is used to generate an unsaved parser from a YAML template.

Syntax

Below is the syntax for generateParserFromTemplate():

graphql
generateParserFromTemplate(
    input: GenerateParserFromTemplateInput!
  ): UnsavedParser!

For the GenerateParserFromTemplateInput input see Given Datatypes, the section below. For indicating which parameters of UnsavedParser you want, see the next section, Returned Datatypes.

Below is an example of how this query field might be used:

Raw
graphql
query {
  generateParserFromTemplate(
    input: {yamlTemplate: "favorite-yaml-template"}
  ) {
    name, 
    script,
    fieldsToBeRemovedBeforeParsing
  }
}
Mac OS or Linux (curl)
shell
curl -v -X POST $YOUR_LOGSCALE_URL/graphql \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d @- << EOF
{"query" : "query {
  generateParserFromTemplate(
    input: {yamlTemplate: \"favorite-yaml-template\"}
  ) {
    name, 
    script,
    fieldsToBeRemovedBeforeParsing
  }
}"
}
EOF
Mac OS or Linux (curl) One-line
shell
curl -v -X POST $YOUR_LOGSCALE_URL/graphql \
    -H "Authorization: Bearer $TOKEN" \
    -H "Content-Type: application/json" \
    -d @- << EOF
{"query" : "query {
  generateParserFromTemplate(
    input: {yamlTemplate: \"favorite-yaml-template\"}
  ) {
    name, 
    script,
    fieldsToBeRemovedBeforeParsing
  }
}"
}
EOF
Windows Cmd and curl
shell
curl -v -X POST $YOUR_LOGSCALE_URL/graphql ^
    -H "Authorization: Bearer $TOKEN" ^
    -H "Content-Type: application/json" ^
    -d @'{"query" : "query { ^
  generateParserFromTemplate( ^
    input: {yamlTemplate: \"favorite-yaml-template\"} ^
  ) { ^
    name,  ^
    script, ^
    fieldsToBeRemovedBeforeParsing ^
  } ^
}" ^
} '
Windows Powershell and curl
powershell
curl.exe -X POST 
    -H "Authorization: Bearer $TOKEN"
    -H "Content-Type: application/json"
    -d '{"query" : "query {
  generateParserFromTemplate(
    input: {yamlTemplate: \"favorite-yaml-template\"}
  ) {
    name, 
    script,
    fieldsToBeRemovedBeforeParsing
  }
}"
}'
"$YOUR_LOGSCALE_URL/graphql"
Perl
perl
#!/usr/bin/perl

use HTTP::Request;
use LWP;

my $INGEST_TOKEN = "TOKEN";

my $uri = '$YOUR_LOGSCALE_URL/graphql';

my $json = '{"query" : "query {
  generateParserFromTemplate(
    input: {yamlTemplate: \"favorite-yaml-template\"}
  ) {
    name, 
    script,
    fieldsToBeRemovedBeforeParsing
  }
}"
}';
my $req = HTTP::Request->new("POST", $uri );

$req->header("Authorization" => "Bearer $TOKEN");
$req->header("Content-Type" => "application/json");

$req->content( $json );

my $lwp = LWP::UserAgent->new;

my $result = $lwp->request( $req );

print $result->{"_content"},"\n";
Python
python
#! /usr/local/bin/python3

import requests

url = '$YOUR_LOGSCALE_URL/graphql'
mydata = r'''{"query" : "query {
  generateParserFromTemplate(
    input: {yamlTemplate: \"favorite-yaml-template\"}
  ) {
    name, 
    script,
    fieldsToBeRemovedBeforeParsing
  }
}"
}'''

resp = requests.post(url,
                     data = mydata,
                     headers = {
   "Authorization" : "Bearer $TOKEN",
   "Content-Type" : "application/json"
}
)

print(resp.text)
Node.js
javascript
const https = require('https');

const data = JSON.stringify(
    {"query" : "query {
  generateParserFromTemplate(
    input: {yamlTemplate: \"favorite-yaml-template\"}
  ) {
    name, 
    script,
    fieldsToBeRemovedBeforeParsing
  }
}"
}
);


const options = {
  hostname: '$YOUR_LOGSCALE_URL/graphql',
  path: '/graphql',
  port: 443,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length,
    Authorization: 'BEARER ' + process.env.TOKEN,
    'User-Agent': 'Node',
  },
};

const req = https.request(options, (res) => {
  let data = '';
  console.log(`statusCode: ${res.statusCode}`);

  res.on('data', (d) => {
    data += d;
  });
  res.on('end', () => {
    console.log(JSON.parse(data).data);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.write(data);
req.end();

Given Datatypes

GenerateParserFromTemplateInput has its one required parameter: yamlTemplate with a datatype YAML, which is a scalar.

Returned Datatypes

The UnsavedParser datatype has several parameters. Below is a list of them, along with a description of each:

Table: UnsavedParser

ParameterTypeRequiredDefaultDescription
Some arguments may be required, as indicated in the Required column. For some fields, this column indicates that a result will always be returned for this column.
Table last updated: Oct 4, 2024
fieldsToBeRemovedBeforeParsing[string]yes A list of fields that will be removed from the event before it's parsed. These fields aren't included om usage calculation.
fieldsToTag[string]yes A list of fields that are used as tags.
namestringyes The name of the unsaved parser.
scriptstringyes The parser script that is executed for each incoming event.
testCases[ParserTestCase]yes Test cases that can be used to help verify that the parser works as expected. See ParserTestCase.