Skip to content

About

A LunaMultiplayer server plugin for OpenTelemetry & Grafana integration

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

LmpServerTelemetry

A LunaMultiplayer server plugin that exports server metrics and traces over OpenTelemetry (OTLP), so they can be charted and alerted on in Grafana Cloud or any OTLP-compatible backend.

Drop the plugin into the server's Plugins folder, point it at an OTLP endpoint with environment variables or a settings file, and start the server. No server settings need to change.

What it exports

Metrics (Prometheus series names, as stored by Grafana Cloud / Mimir):

Area Series Type
Players lmp_players_online, lmp_players_peak, lmp_clients_connected gauge
Players lmp_players_by_version (labels lmp_version, ksp_version), lmp_players_by_subspace (label lmp_subspace) gauge
Sessions lmp_client_connects_total, lmp_client_authentications_total, lmp_client_disconnects_total counter
Sessions lmp_session_duration_seconds (_bucket, _sum, _count) histogram
Messages lmp_messages_received_total, lmp_messages_sent_total (label lmp_message_type) counter
Network lmp_client_rtt_average_milliseconds, lmp_client_rtt_max_milliseconds gauge
Network lmp_send_queue_depth, lmp_send_queue_depth_max gauge
Network lmp_network_sent_bytes_total, lmp_network_received_bytes_total counter
World lmp_vessels, lmp_vessels_pending_removal, lmp_locks, lmp_groups, lmp_scenarios gauge
Warp lmp_warp_subspaces, lmp_warp_furthest_ahead_seconds, lmp_warp_subspace_spread_seconds gauge
Server lmp_server_uptime_seconds gauge

The .NET runtime and process instrumentation (dotnet_*, process_*) is exported as well.

Traces: a lmp.server.start span when the server starts, and one lmp.client.session span per client session.

Every series carries service_name="LunaMultiplayer.Server" and the server's name as the lmp.server.name resource attribute.

Install

  1. Build the plugin (see below) or download the LmpServerTelemetry-plugin artifact from a CI run.
  2. Copy every DLL from the output folder into the server's Plugins folder. The local dashboard page (lmp-local-dashboard.html) is not a plugin file, so it can go anywhere.
  3. Set the endpoint and token with environment variables, or with a Config/LmpServerTelemetry.env file (see Configuration). Use the file when you cannot set environment variables, as on many hosted servers.
  4. Start the server. The console should show lines starting with [LmpServerTelemetry] that name the endpoint being used.

The output folder deliberately does not contain Server.dll, LmpCommon.dll, Lidgren.Core.dll or LunaConfigNode.dll. Do not add copies of them to Plugins: a second copy of those assemblies stops the server recognising the plugin.

Configuration

Settings are read once, when the server starts. Each one can come from an environment variable or from the settings file. If both are set, the environment variable wins.

Variable Purpose
OTEL_EXPORTER_OTLP_ENDPOINT Base OTLP URL, for example https://otlp-gateway-prod-us-east-2.grafana.net/otlp. If unset, the exporter falls back to http://localhost:4318 and the plugin logs a warning.
OTEL_EXPORTER_OTLP_HEADERS Authentication header, for example Authorization=Basic <value>. Never logged by the plugin.
OTEL_EXPORTER_OTLP_PROTOCOL Optional. The plugin always uses HTTP/protobuf, so only http/protobuf makes sense here.
OTEL_METRIC_EXPORT_INTERVAL Milliseconds between metric exports. The OpenTelemetry default is 60000.
LMP_TELEMETRY_PLAYER_NAMES Set to true or 1 to add player names to session spans. Off by default because player names are personal data.
LMP_TELEMETRY_LOCAL_PORT Optional. A port from 1024 to 65535 for the local dashboard. Defaults to 9464 when unset. Set to off to disable it.

Leave OTEL_SERVICE_NAME unset: the plugin sets the service name to LunaMultiplayer.Server, and the dashboard and alert rules filter on it.

Connecting to Grafana Cloud

You need two values: the endpoint and the header. Both must be set, or nothing reaches Grafana.

  1. In Grafana Cloud, open your stack, go to Connections, search for OpenTelemetry, and choose OpenTelemetry (OTLP).
  2. Copy the endpoint from that page. It looks like https://otlp-gateway-REGION.grafana.net/otlp. Use it exactly as shown: do not add /v1/metrics (the exporter adds it), and do not use your stack's own *.grafana.net address, which answers with a 301 redirect.
  3. Get the header value. The Authorization=Basic <value> header is Base64 of instance-id:token, and the instance ID is the one on this page. It can differ from the IDs of your Prometheus or Tempo data sources, and the raw glc_... token is not accepted on its own. See Building and checking the header for the two ways to get it.
  4. Set the endpoint and the header, using environment variables in the same window that starts the server, or the settings file:
$env:OTEL_EXPORTER_OTLP_ENDPOINT = "https://otlp-gateway-REGION.grafana.net/otlp"
$env:OTEL_EXPORTER_OTLP_HEADERS = "Authorization=Basic $header"
.\Server.exe

An environment variable applies only to processes started from the window where you set it. 5. Check the console, then verify data in Grafana.

The endpoint check in the console matters: a missing endpoint is only a warning, and the plugin then exports to http://localhost:4318, where nothing is listening.

Settings file

Create Config/LmpServerTelemetry.env inside the server folder. The file is optional and is read before the exporter starts. Start from LmpServerTelemetry.env.example, which is also copied to the publish folder.

OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-gateway-REGION.grafana.net/otlp
OTEL_EXPORTER_OTLP_HEADERS=Authorization=Basic BASE64_OF_INSTANCE_ID_COLON_TOKEN
OTEL_METRIC_EXPORT_INTERVAL=60000
LMP_TELEMETRY_PLAYER_NAMES=false
  • One NAME=VALUE per line. Blank lines and lines starting with # are ignored. Matching quotes around a value are removed.
  • Only the variables in the table above are accepted. Anything else is ignored.
  • A variable that is already set in the environment is never overwritten by the file.
  • The console reports which names were applied, kept from the environment, ignored or malformed, plus line numbers for malformed lines. It never prints values or the contents of a bad line, so a token pasted on the wrong line is not leaked into the log.
  • Changes take effect on the next server start.

Keeping the token safe

The header value is a credential. Anyone who has it can write telemetry into your Grafana Cloud stack.

  • Create a dedicated access policy for this plugin with only the metrics:write and traces:write scopes, and give its token an expiry date. A leaked token can then write data but not read or change anything.
  • Never commit the token, the header, or Config/LmpServerTelemetry.env. The repository's .gitignore excludes *.env.
  • Never paste the token or header into chat, issues, screenshots or support tickets. If you do, treat it as leaked and rotate it.
  • Prefer environment variables when you control the server's environment. Use the settings file when you do not, and treat that file as a secret: restrict who can read it and keep it out of backups you share.
  • The plugin never logs the header, the token or any value from the settings file.

Building and checking the header

Both methods below use hidden prompts, so the secret is not shown on screen or saved in your shell history. Whichever you use, run the check afterwards and continue only when it prints 200.

Method 1: the ready-made value from the OTLP page. If the page shows a finished Authorization=Basic <value> header for your token, copy only the Base64 part after Basic . It is already complete. Do not encode it again, and do not paste it into an Instance ID prompt: a doubly encoded header is rejected with 401.

$secure = Read-Host "Base64 value from the OTLP page" -AsSecureString
$header = [System.Net.NetworkCredential]::new("", $secure).Password
Remove-Variable secure

Method 2: build it from the instance ID and the token. Use this if you created the token yourself, for example under Cloud access policies. The instance ID is the plain number shown on the OTLP page. The snippet refuses anything that is not digits, which catches a token or Base64 value pasted into the wrong prompt.

$id = Read-Host "Instance ID (digits only)"
if ($id -notmatch '^\d+$') { throw "The instance ID must be a plain number, not a token or a Base64 value." }
$secure = Read-Host "Token" -AsSecureString
$plain = [System.Net.NetworkCredential]::new("", $secure).Password
$header = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("${id}:${plain}"))
Remove-Variable plain, secure, id

Check the header against your own gateway host:

curl.exe -s -o NUL -w "%{http_code}" -X POST https://otlp-gateway-REGION.grafana.net/otlp/v1/metrics -H "Authorization: Basic $header" -H "Content-Type: application/x-protobuf" --data-binary "@nul"
Result Meaning
200 The header is valid.
301 You used the stack's own address or an extra /. Use the gateway host from the OTLP page.
401 The instance ID or token is wrong, the token was deleted or expired, or the value was encoded twice.
403 The token is valid but its access policy lacks the metrics:write or traces:write scope.

Then put Authorization=Basic $header where the server can read it:

$env:OTEL_EXPORTER_OTLP_HEADERS = "Authorization=Basic $header"

or copy the full OTEL_EXPORTER_OTLP_HEADERS=... line into the settings file:

Set-Clipboard "OTEL_EXPORTER_OTLP_HEADERS=Authorization=Basic $header"

Paste it into the file, then clear the clipboard and the variable, because Windows clipboard history can keep the value:

Set-Clipboard $null
Remove-Variable header

On a Windows server you manage yourself, you can also restrict the file to your own account:

icacls "Config\LmpServerTelemetry.env" /inheritance:r /grant:r "$($env:USERNAME):(R,W)"

Rotating the token

Rotate on a schedule, when the token nears its expiry, and immediately if it may have been exposed. The overlap lets the server keep exporting while you switch.

  1. In Grafana Cloud, open your stack's Administration > Users and access > Cloud access policies, find the policy used for this plugin, and choose Add token. Give it an expiry and copy it once, because it is not shown again.
  2. Build and check the new header with one of the methods above. Do not continue until it returns 200.
  3. Replace the old value in the environment variable or in Config/LmpServerTelemetry.env, then restart the server.
  4. Confirm data is arriving: the dashboard's lmp_server_uptime_seconds should restart from a low value and then keep climbing.
  5. Delete the old token in the same access policy.

The running server keeps the header it started with until it restarts. If you delete the old token before restarting with the new one, exports stop and the console shows nothing. Likewise, if the new header is rejected after the restart, the console will not say so, because exporter authentication errors are not logged. Re-run the curl check against the value you actually deployed.

Verifying the connection

  1. The console should show [LmpServerTelemetry] Exporting metrics and traces to https://otlp-gateway-REGION.grafana.net/otlp. If it shows a warning about http://localhost:4318 instead, the endpoint was not set.
  2. Wait about a minute. Metrics are exported every 60 seconds by default, so nothing appears sooner. Set OTEL_METRIC_EXPORT_INTERVAL=10000 while testing if you want faster feedback.
  3. In Grafana, open Explore, choose the Prometheus data source for your stack, and run lmp_server_uptime_seconds. One series per running server should appear, and its value should keep climbing.
  4. For traces, choose the Tempo data source and run { name = "lmp.server.start" }. A span appears each time the server starts.
  5. Import the dashboard once data is arriving.

To tell a live server from a stale one, run time() - timestamp(lmp_server_uptime_seconds). A value that keeps growing past a couple of minutes means that server is no longer delivering data.

Troubleshooting

Symptom Likely cause
Console warns about http://localhost:4318 OTEL_EXPORTER_OTLP_ENDPOINT is not set where the server can see it. Environment variables only apply to the window they were set in.
No data and no console error Exporter errors are not logged. Re-run the curl check with the deployed header, confirm the endpoint line in the console, and allow a full export interval.
Data stops after working The token was deleted or expired while the server was running. Restart the server with a header for a valid token.
curl returns 301, 401 or 403 See the table under Building and checking the header.
Old series stay in Grafana after a restart Each server run reports as a new instance. The old series stop updating and disappear on their own.

Hosted servers

Hosted servers often do not let you set environment variables, which is why the settings file exists. If your host gives you a file manager or SFTP, upload LmpServerTelemetry.env into the server's Config folder (next to ConnectionSettings.xml) and the DLLs into Plugins, then restart the server.

Things to confirm with your host before relying on this:

  • You can write to Config and Plugins, and a restart picks up changes.
  • The server runs on Windows. The plugin targets .NET 10 and has only been tested on Windows.
  • Other customers cannot read your uploaded files.
  • If the host offers a place to set environment variables for the server process, that is preferable to the file.

When you upload the file, use SFTP or the host's HTTPS panel, and delete any local copy that is not in a private folder.

Grafana dashboard and alert rules

The grafana folder contains exports you can import.

  • lmp-server-dashboard.json: import it from Dashboards > New > Import. It asks you to choose the Prometheus data source that receives the metrics.
  • lmp-alert-rules.json: Grafana file-provisioning format. Its data source UID is grafanacloud-prom, the Grafana Cloud default. Change it if your stack uses a different one.

The alert rules cover: server stopped reporting, send queue backed up, client latency high, and long GC pauses. They only change state in Grafana until you route them to a contact point.

The tests check that every metric used by these two files and by the local dashboard page is produced by the plugin, so a renamed metric fails the build instead of leaving a panel empty.

Local dashboard

For a view that doesn't use Grafana Cloud, the plugin can serve its current values on the computer the server runs on. The page is a single HTML file with the same four rows as the Grafana dashboard.

  1. Start the server. The console shows Local dashboard data at http://127.0.0.1:9464/api/metrics. To use another port, set LMP_TELEMETRY_LOCAL_PORT first, for example $env:LMP_TELEMETRY_LOCAL_PORT = "9500".
  2. Open lmp-local-dashboard.html (in the publish folder or in local-dashboard) in a browser on that computer. It needs no internet connection and no separate server.
  3. If you used a different port, change the Data endpoint field at the top of the page. The browser remembers it.

Counters are plotted as rates, or as changes over the last five minutes, and session durations use the last hour of sessions. Charts keep about an hour of readings while the page is open, so a reload starts the history again. The page is tested in headless Edge.

The endpoint is for this computer only:

  • It listens on 127.0.0.1 alone, so other computers on the network cannot read it.
  • It answers only when the Host header is 127.0.0.1 or localhost, which stops web pages from reading it through DNS tricks.
  • It sends a CORS header only to a page opened from disk. Other websites get no access.
  • Other programs on the same computer can still read it. The data has no tokens, but it includes player counts and versions, so don't share it.

It is on by default at port 9464. Set LMP_TELEMETRY_LOCAL_PORT=off to turn it off. If the port is already in use, the plugin logs a warning and runs without it. It does not work from a remote host such as Styx Networks.

Build

Requirements: the .NET 10 SDK and a built LunaMultiplayer server. The plugin compiles against the server's assemblies but does not copy them.

dotnet build ..\LunaMultiplayer\Server\Server.csproj -c Release
dotnet build LmpServerTelemetry.sln -c Release
dotnet publish src\LmpServerTelemetry\LmpServerTelemetry.csproj -c Release -o publish

By default the project looks for the server repository next to this one (..\LunaMultiplayer). Override that with MSBuild properties:

dotnet build LmpServerTelemetry.sln -c Release -p:LmpRepoDir=D:\src\LunaMultiplayer -p:LmpBuildConfiguration=Release

If Server.dll cannot be found, the build stops with a message saying where it looked.

Test

dotnet test LmpServerTelemetry.sln -c Release

The tests do not start a server. Logic that depends on live server state is kept in small classes (PeakTracker, RetiredTraffic, SubspaceStatistics, PlayerBreakdown) so it can be tested directly. A real server run is still the check for anything that touches ServerContext.

Continuous integration

.github/workflows/build.yml checks out this repository and LunaMultiplayer, builds the server, builds and tests the plugin, and uploads the publish folder as the LmpServerTelemetry-plugin artifact. It runs on pushes to main, on pull requests, and manually. A manual run can pick another LunaMultiplayer branch, tag or commit with the lmp_ref input; the default is master.

Known limitations

  • OpenTelemetry is pinned to 1.15.x because newer versions pull a System.Diagnostics.DiagnosticSource that cannot load beside the server's runtime. The pin produces NU1902 vulnerability warnings at build time. See the TODO in src/LmpServerTelemetry/LmpServerTelemetry.csproj.
  • A plugin whose dependency fails to load can stop the whole server from starting, because the server's plugin loader does not guard that step. Test every dependency change against a real server.
  • Exporter authentication errors are not written to the server log. Use the curl check above when no data arrives.

Licenses

This plugin is released under the MIT License.

LunaMultiplayer is also MIT licensed (Copyright (c) 2016 Gabriel Vazquez). The plugin references the server's assemblies at build time and does not include or redistribute LunaMultiplayer code.

The published output bundles third-party libraries under their own licenses, including OpenTelemetry for .NET (Apache License 2.0) and the Microsoft.Extensions libraries (MIT). Keep their license terms when you redistribute the DLLs.

About

A LunaMultiplayer server plugin for OpenTelemetry & Grafana integration

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages