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

ExecStartPre and EnvironmentFile settings to system unit file #157

Merged
merged 6 commits into from
Mar 6, 2024

Conversation

fabiiw05
Copy link
Contributor

I propose adding the capability to configure ExecStartPre and EnvironmentFile in the system unit file. This enhancement allows us to dynamically define variables before running the Grafana Agent, enabling their use in config.yaml.

For example, this change makes it possible to fetch EC2 tags and use them as labels for metrics.

Below is an example script that retrieves the instance ID, queries for the project tag associated with that EC2 instance, and writes it to an environment file. This tag is then used as an external label in metrics configuration.

#! /bin/sh

export INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
ENV_FILE=/var/lib/grafana-agent/aws-env
AWS_PROJECT_TAG=$(aws ec2 describe-instances --region ap-northeast-1 --instance-ids "${INSTANCE_ID}" --query 'Reservations[0].Instances[0].Tags[?Key==`Project`]|[0].Value' --output text)

echo AWS_PROJECT_TAG="${AWS_PROJECT_TAG}" > ${ENV_FILE}

This environment variable is then referenced in the config.yaml for the Grafana Agent as shown below:

metrics:
  global:
    external_labels:
      project: ${AWS_PROJECT_TAG}

@CLAassistant
Copy link

CLAassistant commented Feb 23, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@gardar gardar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I don't see anything wrong with adding this I wonder if it's perhaps too niche to be special variables?
Perhaps it would be more useful to add a variable grafana_agent_service_extra which accepts arbitrary options to the systemd unit?

@fabiiw05
Copy link
Contributor Author

I think that's a great idea. I was also considering setting AmbientCapabilities and CapabilityBoundingSet for logging purposes. Thank you for your suggestion. I'll look into making that adjustment.

@fabiiw05
Copy link
Contributor Author

I've updated the implementation. To ensure the functionality aligns with our expectations, I defined the following variables and tested the behavior:

grafana_agent_service_extra:
  CapabilityBoundingSet: CAP_DAC_READ_SEARCH
  AmbientCapabilities: CAP_DAC_READ_SEARCH
  EnvironmentFile: /var/lib/grafana-agent/extra-service.env
  ExecStartPre: |-
    /etc/grafana-agent/pre_exec.sh \
          --option1 value1 \
          --option2 value2

@fabiiw05 fabiiw05 requested a review from gardar February 26, 2024 22:30
Copy link
Collaborator

@gardar gardar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ishanjainn ishanjainn merged commit 433b4d2 into grafana:main Mar 6, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants