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

Temporary Variables #333

Open
glightfoot opened this issue Aug 18, 2020 · 1 comment
Open

Temporary Variables #333

glightfoot opened this issue Aug 18, 2020 · 1 comment
Labels
enhancement This is considered a feature request, not currently guaranteed by the code or design today mtail-Language/VM Issues related to the mtail language, compiler, or VM

Comments

@glightfoot
Copy link

glightfoot commented Aug 18, 2020

Is it possible to store temporary variables in an mtail program for convenience? Hidden variables seem to be for storing state between lines, but these variables would just be used for transforming data from each line. For example, using the apache program in examples, if you wanted to transform response code to response code group to save on cardinality, is this the right way to do it?

# Parser for the common apache log format as follow.
# LogFormat "%h %l %u %t \"%r\" %>s %b %D \"%{Referer}i\" \"%{User-agent}i\"
counter apache_http_requests_total by request_method, http_version, status_code
counter apache_http_bytes_total by request_method, http_version, status_code
gauge apache_http_response_time by remote_host, request_method, request_uri, status_code, user_agent
gauge apache_http_response_size by remote_host, request_method, request_uri, status_code, user_agent
hidden text status_code_group

/^/ +
/(?P<remote_host>[0-9A-Za-z\.:-]+) / + # %h
/(?P<remote_logname>[0-9A-Za-z-]+) / + # %l
/(?P<remote_username>[0-9A-Za-z-]+) / + # %u
/\[(?P<timestamp>\d{2}\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2} (\+|-)\d{4})\] / + # %u
/"(?P<request_method>[A-Z]+) (?P<request_uri>\S+) (?P<http_version>HTTP\/[0-9\.]+)" / + # \"%r\"
/(?P<status_code>\d{3}) / + # %>s
/((?P<response_size>\d+)|-) / + # %b
/(?P<response_time>\d+) / + # %D
/"(?P<referer>\S+)" / + # \"%{Referer}i\"
/"(?P<user_agent>[[:print:]]+)"/ + # \"%{User-agent}i\"
/$/ {
  strptime($timestamp, "02/Jan/2006:15:04:05 -0700") # for tests

  if $status_code < 200 {
    status_code_group = "2xx"
  }
  if $status_code >= 200 && $status_code < 300 {
    status_code_group = "3xx"
  }
  if $status_code >= 300 && $status_code < 400 {
    status_code_group = "4xx"
  }
  if $status_code >= 400 && $status_code < 500 {
    status_code_group = "4xx"
  }
  if $status_code >= 500 {
    status_code_group = "5xx"
  }

  apache_http_requests_total[$request_method][$http_version][$status_code_group]++
  $response_size > 0 {
      apache_http_bytes_total[$request_method][$http_version][$status_code_group] += $response_size
      apache_http_response_size[$remote_host][$request_method][$request_uri][$status_code_group][$user_agent] += $response_size
  }
  apache_http_response_time[$remote_host][$request_method][$request_uri][$status_code_group][$user_agent] = $response_time

  del status_code_group
}
@jaqx0r
Copy link
Contributor

jaqx0r commented Mar 20, 2021

Sorry foer the delay. That looks like a reasonable approach. There's no magic that says hidden variables are for state only, you can do this if it works for you!

There's no other concept of temporary variables, but there could be. I can leave this open as a feature request.

@jaqx0r jaqx0r added enhancement This is considered a feature request, not currently guaranteed by the code or design today Language mtail-Language/VM Issues related to the mtail language, compiler, or VM and removed mtail-Language labels Mar 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is considered a feature request, not currently guaranteed by the code or design today mtail-Language/VM Issues related to the mtail language, compiler, or VM
Projects
None yet
Development

No branches or pull requests

2 participants