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

amazon-cloudwatch-agent: init at 1.300049.1 #337212

Merged
merged 1 commit into from
Nov 21, 2024

Conversation

commiterate
Copy link
Contributor

@commiterate commiterate commented Aug 25, 2024

Description of changes

Initialize amazon-cloudwatch-agent at 1.300049.1.

This is commonly deployed alongside applications run on Amazon Web Services compute (e.g. EC2, Fargate).

Fixes #6848.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@commiterate
Copy link
Contributor Author

commiterate commented Aug 26, 2024

Tried writing the service module and it seems like this might be a little complicated. The following doesn't work:

{ lib, pkgs, config, ... }:
let
  cfg = config.services.amazon-cloudwatch-agent;
in
{
  options.services.amazon-cloudwatch-agent = {
    enable = lib.mkEnableOption "amazon-cloudwatch-agent";
    package = lib.mkPackageOption pkgs "amazon-cloudwatch-agent" { };
    settings = lib.mkOption {
      type = types.submodule {
        freeformType = (pkgs.formats.json { }).type;
      };
      default = { };
      description = ''
        Amazon CloudWatch Agent configuration. See
        <https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Agent-Configuration-File-Details.html>
        for supported values.
      '';
    };
  };

  # See https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300045.0/packaging/dependencies/amazon-cloudwatch-agent.service.
  config = lib.mkIf cfg.enable {
    systemd.services.amazon-cloudwatch-agent = {
      description = "Amazon CloudWatch Agent";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Type = "simple";
        # TODO: "start-amazon-cloudwatch-agent" assumes the agent is installed at "/opt/aws/amazon-cloudwatch-agent".
        #
        # It's inconvenient to use "amazon-cloudwatch-agent" directly since "start-amazon-cloudwatch-agent" calls
        # "config-translator" to translate the JSON configuration into separate TOML (for CloudWatch Logs + Metrics)
        # and YAML (for X-Ray traces) configuration files. This doesn't seem to be a 1:1 transformation.
        ExecStart = "${cfg.package}/bin/start-amazon-cloudwatch-agent";
        KillMode = "process";
        Restart = "on-failure";
        RestartSec = "60s";
      };
    };
  };

  environment.etc."amazon-cloudwatch-agent/amazon-cloudwatch-agent.json".text = toJSON cfg.settings;
}

The problem is start-amazon-cloudwatch-agent which hardcodes the expected package installation path as /opt/aws/amazon-cloudwatch-agent. This is used to derive other binary paths and generated output file paths.

https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300045.0/tool/paths/paths_unix.go#L12

start-amazon-cloudwatch-agent is unfortunately not just a simple wrapper program that calls amazon-cloudwatch-agent with pre-defined arguments. It runs config-translator to translate /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json into:

  • /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
    • For CloudWatch Logs + Metrics.
  • /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.yaml
    • For X-Ray traces.

These don't seem to be 1:1 translations either (there may be some key renames).

We'll probably need to ask upstream to provide the following options:

  • Installation directory.
    • Where the package is installed.
    • Maybe they can switch to relative paths for executing config-translator and amazon-cloudwatch-agent instead or get the current directory? Then this won't be needed at all.
  • Input JSON configuration file path.
    • Input amazon-cloudwatch-agent.json.
    • This is something NixOS users would control with services.amazon-cloudwatch-agent.settings and we'd write to /etc/amazon-cloudwatch-agent/amazon-cloudwatch-agent.json.
  • Runtime output directory.
    • Where to write amazon-cloudwatch-agent.toml, amazon-cloudwatch-agent.yaml, amazon-cloudwatch-agent.env, amazon-cloudwatch-agent.pid, etc.
    • This has to be a mutable directory in NixOS.
      • Probably /run/amazon-cloudwatch-agent. /etc can be immutable if system.etc.overlay.mutable is set to false (docs).

Alternatively, they might want to have amazon-cloudwatch-agent accept the unified JSON configuration directly and work with it in memory instead of writing out intermediary results to other files (though they still want to write out their optional PID file somewhere). I'm not sure why these derived configuration files need to exist.


Going to swap this PR to a draft in the meanwhile.

@commiterate
Copy link
Contributor Author

commiterate commented Aug 26, 2024

Opened an issue upstream about start-amazon-cloudwatch-agent.

aws/amazon-cloudwatch-agent#1319

We might be able to get away with using ExecStartPre to call config-translator:

${cfg.package}/bin/config-translator \
  -input /etc/amazon-cloudwatch-agent/amazon-cloudwatch-agent.json \
  -input-dir /etc/amazon-cloudwatch-agent/amazon-cloudwatch-agent.d \
  -mode auto \
  -output /run/amazon-cloudwatch-agent/amazon-cloudwatch-agent.toml

Then we'll make ExecStart do:

${cfg.package}/bin/amazon-cloudwatch-agent \
  -config /run/amazon-cloudwatch-agent/amazon-cloudwatch-agent.toml \
  -envconfig /run/amazon-cloudwatch-agent/env-config.json \
  -otelconfig /run/amazon-cloudwatch-agent/amazon-cloudwatch-agent.yaml \
  -pidfile /run/amazon-cloudwatch-agent/amazon-cloudwatch-agent.pid

Reference: https://github.com/aws/amazon-cloudwatch-agent/blob/v1.300045.0/cmd/start-amazon-cloudwatch-agent/path.go#L67-L79

@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from d6a58a3 to 417c268 Compare August 26, 2024 23:53
@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Aug 26, 2024
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch 2 times, most recently from 3efb2a8 to 9caa0fd Compare August 27, 2024 04:05
@ofborg ofborg bot added the ofborg-internal-error Ofborg encountered an error label Aug 27, 2024
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from 9caa0fd to 86905fe Compare August 27, 2024 04:11
@commiterate
Copy link
Contributor Author

Added the system service. Need to figure out how to test this in a VM now.

@commiterate commiterate marked this pull request as ready for review August 27, 2024 04:12
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from 86905fe to 3f5ab94 Compare August 27, 2024 04:22
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch 9 times, most recently from 1d850ea to 6b5c96b Compare August 27, 2024 06:53
@commiterate
Copy link
Contributor Author

commiterate commented Oct 30, 2024

We'll stick with the current version in the MR (1.300047.0) then given that 1.300048.1 is mostly feature additions with a minor bug fix.

As for soliciting a review, the only place I know of is the review request mega-thread in Discourse. If there's another avenue for doing so like a Matrix room, please share this PR there as well.

If the reviewer with write permissions asks for changes, I'll also bump the version as well since we'll need to wait for another full OfBorg eval anyways.

@commiterate
Copy link
Contributor Author

Posted to the Nixpkgs Review Requests channel in Matrix.

@commiterate
Copy link
Contributor Author

Still no bites. Need to rebase anyways to fix a merge conflict with the release notes. Will bump the version as well.

@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from af8871c to 1438c4c Compare November 9, 2024 22:51
@commiterate commiterate changed the title amazon-cloudwatch-agent: init at 1.300047.0 amazon-cloudwatch-agent: init at 1.300048.1 Nov 9, 2024
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from 1438c4c to defa3f2 Compare November 9, 2024 23:02
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from defa3f2 to 36e7d18 Compare November 9, 2024 23:12
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from 36e7d18 to 37cd2b9 Compare November 9, 2024 23:30
@wegank wegank removed 12.approvals: 1 This PR was reviewed and approved by one reputable person 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in the package labels Nov 10, 2024
@ofborg ofborg bot requested a review from philipmw November 10, 2024 09:48
@wegank wegank added 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in the package 12.approvals: 2 This PR was reviewed and approved by two reputable people labels Nov 10, 2024
@commiterate
Copy link
Contributor Author

commiterate commented Nov 16, 2024

Looks like we're stuck waiting for an aarch64-darwin OfBorg runner again.

Going to bump to 1.300049.1 since it has some important enhancements and bug fixes.

Given that there's no aarch64-darwin tests and I've already tested build on an M2 Mac, we should be good to merge once all of the Linux tests pass.

@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from 37cd2b9 to 1201648 Compare November 16, 2024 19:35
@commiterate commiterate changed the title amazon-cloudwatch-agent: init at 1.300048.1 amazon-cloudwatch-agent: init at 1.300049.1 Nov 16, 2024
@wegank wegank removed 12.approvals: 2 This PR was reviewed and approved by two reputable people 12.approved-by: package-maintainer This PR was reviewed and approved by a maintainer listed in the package labels Nov 17, 2024
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from 1201648 to c4aef4c Compare November 19, 2024 00:04
@commiterate commiterate force-pushed the init/amazon-cloudwatch-agent branch from c4aef4c to 3d0e315 Compare November 19, 2024 00:19
@commiterate
Copy link
Contributor Author

OfBorg Linux checks passed. Good to merge.

@kirillrdy kirillrdy merged commit c802177 into NixOS:master Nov 21, 2024
28 of 29 checks passed
@commiterate commiterate deleted the init/amazon-cloudwatch-agent branch November 21, 2024 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 1-10 10.rebuild-darwin: 1 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adapt the Amazon CloudWatch Monitoring Scripts for Linux to NixOS
7 participants