diff --git a/.github/workflows/enable_log_forwarding_action_tests.yaml b/.github/workflows/enable_log_forwarding_action_tests.yaml index d91f3e37..4890d4f1 100644 --- a/.github/workflows/enable_log_forwarding_action_tests.yaml +++ b/.github/workflows/enable_log_forwarding_action_tests.yaml @@ -53,16 +53,22 @@ jobs: runs-on: [self-hosted-linux-amd64-noble-edge] env: TEST_CONFIG_FILE: 91-enable-log-forwarding-smoke-${{ github.run_id }}-${{ github.run_attempt }}.yaml + TEST_LOG_FILE: /var/log/enable-log-forwarding-smoke-${{ github.run_id }}.log steps: - uses: actions/checkout@v6 - name: Run enable log forwarding action uses: ./actions/enable-log-forwarding with: - files: | - /var/log/syslog + files: ${{ env.TEST_LOG_FILE }} config-file-name: ${{ env.TEST_CONFIG_FILE }} - otlp-endpoint: 127.0.0.1:4317 + + - name: Populate log file to forward + run: | + for i in {1..5}; do + echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) INFO enable-log-forwarding smoke test: entry $i run_id=${{ github.run_id }}" | sudo tee -a ${TEST_LOG_FILE} + sleep 2 + done - name: Verify generated config file exists run: | diff --git a/actions/enable-log-forwarding/enable_log_forwarding.py b/actions/enable-log-forwarding/enable_log_forwarding.py index 255ae9cd..25d3795f 100644 --- a/actions/enable-log-forwarding/enable_log_forwarding.py +++ b/actions/enable-log-forwarding/enable_log_forwarding.py @@ -15,7 +15,22 @@ from typing import Sequence CONFIG_DIR = "/etc/otelcol/config.d" -EXPORTER_NAME = "otlp_grpc" +EXPORTER_NAME = "otlp/github_runner_optin" +BATCH_PROCESSOR_NAME = "batch/github_runner_optin" +SERVICE_NAME = "self-hosted-runner" +LOKI_RESOURCE_LABELS = ( + "service.name", + "github.repository", + "github.runner", + "github.workflow", + "github.job", + "github.run.id", + "github.run.attempt", +) +LOKI_ATTRIBUTE_LABELS = ( + "log.file.name", + "log.file.path", +) SNAP_CMD = Path("/usr/bin/snap") SUDO_CMD = Path("/usr/bin/sudo") MKDIR_CMD = Path("/usr/bin/mkdir") @@ -174,16 +189,29 @@ def exporter_exists_in_config_dir( def build_resource_attributes() -> list[dict[str, str]]: """Build static GitHub resource attributes attached to forwarded logs.""" attrs = [ + ("service.name", SERVICE_NAME), ("github.repository", os.getenv("GITHUB_REPOSITORY", "unknown")), ("github.runner", os.getenv("RUNNER_NAME", "unknown")), ("github.workflow", os.getenv("GITHUB_WORKFLOW", "unknown")), ("github.job", os.getenv("GITHUB_JOB", "unknown")), ("github.run.id", os.getenv("GITHUB_RUN_ID", "unknown")), ("github.run.attempt", os.getenv("GITHUB_RUN_ATTEMPT", "unknown")), + ("loki.resource.labels", ", ".join(LOKI_RESOURCE_LABELS)), ] return [{"key": key, "value": value, "action": "upsert"} for key, value in attrs] +def build_log_attribute_actions() -> list[dict[str, str]]: + """Build log-attribute actions for Loki label promotion hints.""" + return [ + { + "key": "loki.attribute.labels", + "value": ", ".join(LOKI_ATTRIBUTE_LABELS), + "action": "upsert", + } + ] + + def build_config( files: Sequence[str], resolved_endpoint: str, @@ -196,18 +224,28 @@ def build_config( "filelog/github_runner_optin": { "include": list(files), "start_at": "end", + "include_file_name": True, + "include_file_path": True, } }, "processors": { "resource/github_runner_optin": { "attributes": build_resource_attributes(), - } + }, + "attributes/github_runner_optin": { + "actions": build_log_attribute_actions(), + }, + BATCH_PROCESSOR_NAME: {}, }, "service": { "pipelines": { "logs/github_runner_optin": { "receivers": ["filelog/github_runner_optin"], - "processors": ["resource/github_runner_optin", "batch"], + "processors": [ + "resource/github_runner_optin", + "attributes/github_runner_optin", + BATCH_PROCESSOR_NAME, + ], "exporters": [exporter_name], } } @@ -217,6 +255,7 @@ def build_config( config["exporters"] = { exporter_name: { "endpoint": resolved_endpoint, + "tls": {"insecure": True}, } } diff --git a/actions/enable-log-forwarding/tests/test_enable_log_forwarding.py b/actions/enable-log-forwarding/tests/test_enable_log_forwarding.py index 358b522a..eb6a7764 100644 --- a/actions/enable-log-forwarding/tests/test_enable_log_forwarding.py +++ b/actions/enable-log-forwarding/tests/test_enable_log_forwarding.py @@ -159,6 +159,7 @@ def test_build_config_exporter_block_is_conditionally_defined( assert ("exporters" in config) is has_exporters_block if has_exporters_block: assert config["exporters"][module.EXPORTER_NAME]["endpoint"] == "otel:4318" + assert config["exporters"][module.EXPORTER_NAME]["tls"] == {"insecure": True} @pytest.mark.parametrize(