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

Custom metrics #8

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updates
  • Loading branch information
dgkanatsios committed Feb 28, 2024
commit 5543232fbe95e02e8f3d182428c14bbca023b2ec
9 changes: 9 additions & 0 deletions windows_linux_custom_gameserver_metrics/PF_StartupScript.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ echo "PlayFab Virtual Machine ID is $env:PF_VM_ID" # e.g. vmss:SouthCentralUs:24
echo "Region where the VM is deployed is $env:PF_REGION" # e.g. SouthCentralUs
echo "Shared content folder is $env:PF_SHARED_CONTENT_FOLDER_VM" # e.g. D:\sharedcontentfolder (All servers running on this VM have access to this folder through the PF_SHARED_CONTENT_FOLDER env variable.)

# install telegraf as a Windows service
$telegrafPath = 'C:\Program Files\telegraf'
New-Item -ItemType Directory -Force -Path "$telegrafPath"
cp "$scriptPath\telegraf.*" "$telegrafPath"
cd "$telegrafPath"
.\telegraf.exe --service install --config "$telegrafPath\telegraf.conf"

# start the telegraf service
.\telegraf.exe --service start
26 changes: 26 additions & 0 deletions windows_linux_custom_gameserver_metrics/PF_StartupScript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
set -o errexit # script exits when a command fails == set -e
set -o nounset # script exits when tries to use undeclared variables == set -u
set -o xtrace # trace what's executed == set -x (useful for debugging)
set -o pipefail # causes pipelines to retain / set the last non-zero status

# get script's path
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

# install telegraf
sudo apt-get -yq --no-install-recommends install "${DIR}"/telegraf.deb

systemctl stop telegraf

# remove \r\n in-place
sed -i -e 's/\r$//' ${DIR}/telegraf.conf

mkdir -p /etc/telegraf || echo 'Warning: /etc/telegraf already exists'

# add PF_TITLE_ID, PF_BUILD_ID, PF_VM_ID to telegraf.conf as dimensions
sed -e "s/_%PF_TITLE_ID%_/${PF_TITLE_ID}/g" -e "s/_%PF_BUILD_ID%_/${PF_BUILD_ID}/g" -e "s/_%PF_VM_ID%_/${PF_VM_ID}/g" -e "s/_%PF_REGION%_/${PF_REGION}/g" ${DIR}/telegraf.conf > /etc/telegraf/telegraf.conf

# add user telegraf to group docker so telegraf daemon can read docker logs
sudo usermod -a -G docker telegraf

systemctl restart telegraf
17 changes: 10 additions & 7 deletions windows_linux_custom_gameserver_metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

## What it does

This script uses [telegraf](https://www.influxdata.com/time-series-platform/telegraf/) agent that sets up an HTTP server to listen for external metrics. Using this solution, you can send any kind of metrics from your game server to telegraf. You can use any of the [telegraf output plugins](https://github.com/influxdata/telegraf/blob/master/docs/OUTPUTS.md) to store them.
This script uses [telegraf](https://www.influxdata.com/time-series-platform/telegraf/) agent that sets up an HTTP server to listen for external metrics. Using this solution, you can send any kind of metrics from your game server to telegraf and you can use any of the [telegraf output plugins](https://github.com/influxdata/telegraf/blob/master/docs/OUTPUTS.md) to store them, in the backend of your choice.

The documentation for the [http listener telegraf input plugin is here](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/http_listener_v2/README.md).
The documentation for the [http listener telegraf input plugin is here](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/http_listener_v2/README.md), check it out for instructions on how to properly configure it. You can also check this [telegraf.conf sample config file](./telegraf.conf) file for an example of how to configure telegraf to listen for metrics on port 8080.

You can see the [telegraf.conf](./telegraf.conf) file for an example of how to configure telegraf to listen for metrics on port 8080. To send metrics from your game server, you need to find the localhost IP address and send a POST request to the telegraf server.
To send custom metrics, you need send a POST request to the telegraf server from your game server. To do that, you need to find the host VM IP address. This is different depending on the type of Build you are using.

### Host IP using Windows or Linux Processes

If you are using [process mode](https://learn.microsoft.com/en-us/gaming/playfab/features/multiplayer/servers/deploy-using-game-manager#server-details-for-process-mode), you can talk to telegraf using `localhost` or `127.0.0.1`.

### Host IP using Windows Containers

To get the IP of the host when using Windows Containers, you can use the following Powershell script from inside the container, before you start your game server:
PlayFab configures a bridge network between containers and the host VM. To get the IP of the host when using Windows Containers, you can use the following Powershell script from inside the container, before you start your game server:

```powershell
$gateway = (Get-WmiObject -Class Win32_IP4RouteTable | where { $_.destination -eq '0.0.0.0' -and $_.mask -eq '0.0.0.0' } | sort metric1 | select nexthop).nexthop
Expand All @@ -27,12 +27,16 @@ Write-Output $gateway

### Host IP using Linux Containers

To get the IP of the host when using Linux Containers, you can a script from inside the container, before you start your game server. The script you car run depends on your container image. If, for example, you are using Ubuntu, you can do `sudo apt update && sudo apt install net-tools` and then grab the gateway IP using:
PlayFab configures a bridge network between containers and the host VM. To get the IP of the host when using Linux Containers, you can run a script from inside the container, before you start your game server. The script you can run depends on the OS that you configure your container image with. For example, if you are using Ubuntu, you can do `sudo apt update && sudo apt install net-tools` to install `netstat` and then grab the host VM (which acts as a gateway) IP using:

```bash
netstat -rn | grep '^0.0.0.0' | awk '{print $2}'
```

## Sending metrics to telegraf

To send metrics to telegraf, you'd need to send the data in a specific format. You can see the documentation [here](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/http_listener_v2/README.md) and a couple of samples below:

```bash
curl -i -XPOST 'http://localhost:8080/telegraf' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
```
Expand All @@ -47,12 +51,11 @@ $response = Invoke-WebRequest -Uri "http://localhost:8080/telegraf" -Method POST

## Usage

You should download telegraf from the [GitHub releases](https://github.com/influxdata/telegraf/releases) section. The [Windows amd64 package](https://dl.influxdata.com/telegraf/releases/telegraf-1.24.4_windows_amd64.zip) works with this sample. Then, you should grab either the DeveloperSecretKey or the TelemetryKey from your PlayFab account and add it to the plugin.conf file.
You should download telegraf from the [GitHub releases](https://github.com/influxdata/telegraf/releases) section. The [Windows amd64 package](https://dl.influxdata.com/telegraf/releases/telegraf-1.24.4_windows_amd64.zip) works with this sample.

Then, create a zip file with the following contents:

- telegraf.conf
- plugin.conf
- telegraf package file (e.g. telegraf.exe)
- PF_StartupScript.ps1 file

Expand Down
7 changes: 4 additions & 3 deletions windows_linux_custom_gameserver_metrics/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
omit_hostname = true

[global_tags]
titleID = "59F84"
buildID = "f49eeebd-9a7a-4bd1-a7a9-92b5b1a9568d"
vmID = "vmss:EastUs:59F84_a7582365-e048-4a88-9271-40a74b87ba83:45c93cbe-83d0-4220-9f25-24b5e4f20665"
titleID = "_%PF_TITLE_ID%_"
buildID = "_%PF_BUILD_ID%_"
vmID = "_%PF_VM_ID%_"

[[outputs.file]]
## Files to write to, "stdout" is a specially handled file.
# this is just for testing, you should add the outputs of your choice
files = ["stdout"]

# Generic HTTP write listener
Expand Down
Loading