Amazon CloudWatch
로그와 메트릭을 Amazon CloudWatch로 보내 주는 출력 플러그인이에요.
지원 이벤트 유형:
logsmetrics
Amazon CloudWatch 출력 플러그인은 레코드를 CloudWatch Logs 서비스로 수집해 줘요. Embedded Metric Format (EMF)을 통한 CloudWatch Metrics 지원도 제공돼요.
이 문서는 C로 작성된 핵심 Fluent Bit CloudWatch 플러그인 문서예요. 이 플러그인은 aws/amazon-cloudwatch-logs-for-fluent-bit Golang Fluent Bit 플러그인(cloudwatch)을 대체할 수 있어요. 이 CloudWatch 플러그인은 충돌이나 혼동을 막기 위해 cloudwatch_logs라고 불러요. 원래 플러그인의 폐기 및 마이그레이션 계획에 대한 자세한 내용은 Amazon 저장소에서 Golang 플러그인을 확인해요.
AWS 자격 증명이 어떻게 가져와지는지에 대한 자세한 내용은 AWS credentials을 참고해요.
출처: 문서
본문
구성 파라미터
| Key | Description | Default |
|---|---|---|
add_entity |
Add entity to PutLogEvent calls. |
false |
alias |
Sets an alias, use for multiple instances of the same output plugin. | none |
auto_create_group |
Automatically create the log group. Allowed values: true, false (case insensitive). |
false |
auto_retry_requests |
Immediately retry failed requests to AWS services once. This option doesn't affect the normal Fluent Bit retry mechanism with backoff. Instead, it enables an immediate retry with no delay for networking errors, which can help improve throughput when there are transient/random networking issues. | true |
endpoint |
Specify a custom endpoint for the CloudWatch Logs API. | none |
external_id |
Specify an external ID for the STS API, can be used with the role_arn parameter if your role requires an external ID. |
none |
extra_user_agent |
This option appends a string to the default user agent. AWS asks that you not manually set this field yourself, it's reserved for use in vended configurations, for example, EKS Container Insights. | none |
log_format |
An optional parameter that can be used to tell CloudWatch the format of the data. A value of json/emf enables CloudWatch to extract custom metrics embedded in a JSON payload. |
none |
log_group_class |
Specifies the log storage class for new log groups when auto_create_group is set to true. You can't modify the storage class of existing log groups. Allowed values: STANDARD, INFREQUENT_ACCESS. |
STANDARD |
log_group_name |
The name of the CloudWatch log group that you want log records sent to. | none |
log_group_template |
Template for CW Log Group name using record accessor syntax. Plugin falls back to the log_group_name configured if needed. |
none |
log_key |
By default, the whole log record will be sent to CloudWatch. If you specify a key name with this option, then only the value of that key will be sent to CloudWatch. For example, if you are using the Fluentd Docker log driver, you can specify log_key log and only the log message will be sent to CloudWatch. |
none |
log_level |
Specifies the log level for output plugin. If not set here, plugin uses global log level in service section. |
info |
log_retention_days |
If set to a number greater than zero, and newly create log group's retention policy is set to this many days. Allowed values: [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653] |
0 |
log_stream_name |
The name of the CloudWatch log stream that you want log records sent to. | none |
log_stream_prefix |
Prefix for the log stream name. The tag is appended to the prefix to construct the full log stream name. Not compatible with the log_stream_name option. |
none |
log_stream_template |
Template for CloudWatch Log Stream name using record accessor syntax. Plugin falls back to the log_stream_name or log_stream_prefix configured if needed. |
none |
log_suppress_interval |
Suppresses log messages from output plugin that appear similar within a specified time interval. 0 no suppression. |
0 |
match |
Set a tag pattern to match records that output should process. Exact matches or wildcards (for example *). |
none |
match_regex |
Set a regular expression to match tags for output routing. This allows more flexible matching compared to wildcards. | none |
metric_dimensions |
Metric dimensions is a list of lists. If you have only one list of dimensions, put the values as a comma separated string. If you want to put list of lists, use the list as semicolon separated strings. If your value is d1,d2;d3, it will consider it as [[d1, d2],[d3]]. |
none |
metric_namespace |
An optional string representing the CloudWatch namespace for the metrics. See the Metrics tutorial section for a full configuration. | none |
profile |
Option to specify an AWS Profile for credentials. | none |
region |
The AWS region to send logs to. China regions (cn-*), the AWS European Sovereign Cloud regions (eusc-*), and Amazon Dedicated Cloud regions (us-iso-*, us-isob-*, us-isof-*, eu-isoe-*) are supported; Fluent Bit automatically uses the correct endpoint suffix (.amazonaws.com.cn, .amazonaws.eu, .c2s.ic.gov, .sc2s.sgov.gov, .csp.hci.ic.gov, or .cloud.adc-e.uk) and no custom endpoint is required. |
none |
retry_limit |
Set retry limit for output plugin when delivery fails. Integer, no_limits, false, or off to disable, or no_retries to disable retries entirely. |
1 |
role_arn |
ARN of an IAM role to assume for cross account access. | none |
sts_endpoint |
Specify a custom STS endpoint for the AWS STS API. | none |
tls.windows.certstore_name |
Sets the certificate store name on an output (Windows). | none |
tls.windows.use_enterprise_store |
Sets whether using enterprise certificate store or not on an output (Windows). | none |
workers |
The number of workers to perform flush operations for this output. | 1 |
시작하기 (Get started)
레코드를 Amazon CloudWatch로 보내려면 명령줄에서 또는 설정 파일을 통해 플러그인을 실행할 수 있어요.
명령줄
CloudWatch 플러그인은 -p 인자(속성)를 사용해 명령줄에서 파라미터를 읽을 수 있어요.
fluent-bit -i cpu -o cloudwatch_logs -p log_group_name=group -p log_stream_name=stream -p region=us-west-2 -m '*' -f 1
설정 파일
메인 설정 파일에 다음을 추가해요.
fluent-bit.yaml:
pipeline:
outputs:
- name: cloudwatch_logs
match: '*'
region: us-east-1
log_group_name: fluent-bit-cloudwatch
log_stream_prefix: from-fluent-bit-
auto_create_group: on
fluent-bit.conf:
[OUTPUT]
Name cloudwatch_logs
Match *
Region us-east-1
Log_Group_Name fluent-bit-cloudwatch
Log_Stream_Prefix from-fluent-bit-
Auto_Create_Group On
LocalStack과의 통합 (CloudWatch logs)
http://localhost:4566에서 실행 중인 Localstack 인스턴스의 경우 다음 구성을 사용해요.
fluent-bit.yaml:
pipeline:
outputs:
- name: cloudwatch_logs
match: '*'
region: us-east-1
log_group_name: fluent-bit-cloudwatch
log_stream_prefix: from-fluent-bit-
auto_create_group: on
endpoint: localhost
port: 4566
fluent-bit.conf:
[OUTPUT]
Name cloudwatch_logs
Match *
Region us-east-1
Log_Group_Name fluent-bit-cloudwatch
Log_Stream_Prefix from-fluent-bit-
Auto_Create_Group On
Endpoint localhost
Port 4566
테스트용 자격 증명은 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY 같은 로컬 변수로 내보낼 수 있어요.
권한
이 플러그인을 사용하려면 다음 AWS IAM 권한이 필요해요.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents",
"logs:PutRetentionPolicy"
],
"Resource": "*"
}]
}
record_accessor 문법을 사용한 로그 스트림 및 그룹 이름 템플릿화
로그 그룹이나 스트림 이름이 로그 레코드 자체의 내용에 기반하도록 하고 싶을 수 있어요. 이 플러그인은 Fluent Bit record_accessor 문법을 사용해 로그 그룹과 스트림 이름을 템플릿화하는 것을 지원해요.
다음 예시는 Kubernetes 메타데이터를 기반으로 로그 그룹과 스트림 이름을 템플릿화하는 일반적인 사용 사례를 보여 줘요.
Kubernetes 필터는 다음과 같은 메타데이터를 추가할 수 있어요.
kubernetes: {
annotations: {
"kubernetes.io/psp": "eks.privileged"
},
container_hash: "<some hash>",
container_name: "myapp",
docker_id: "<some id>",
host: "ip-10-1-128-166.us-east-2.compute.internal",
labels: {
app: "myapp",
"pod-template-hash": "<some hash>"
},
namespace_name: "my-namespace",
pod_id: "198f7dd2-2270-11ea-be47-0a5d932f5920",
pod_name: "myapp-5468c5d4d7-n2swr"
}
record_accessor를 사용하면 이 객체를 기반으로 템플릿을 만들 수 있어요.
구성은 다음과 같아요.
fluent-bit.yaml:
pipeline:
outputs:
- name: cloudwatch_logs
match: '*'
region: us-east-1
log_group_name: fallback-group
log_stream_prefix: fallback-stream
auto_create_group: on
log_group_template: application-logs-$kubernetes['host'].$kubernetes['namespace_name']
log_stream_template: $kubernetes['pod_name'].$kubernetes['container_name']
fluent-bit.conf:
[OUTPUT]
Name cloudwatch_logs
Match *
Region us-east-1
Log_Group_Name fallback-group
Log_Stream_Prefix fallback-stream
Auto_Create_Group On
Log_Group_Template application-logs-$kubernetes['host'].$kubernetes['namespace_name']
Log_Stream_Template $kubernetes['pod_name'].$kubernetes['container_name']
Kubernetes 메타데이터와 함께, 로그 그룹 이름은 application-logs-ip-10-1-128-166.us-east-2.compute.internal.my-namespace가 되고, 로그 스트림 이름은 myapp-5468c5d4d7-n2swr.myapp이 돼요.
로그 레코드에서 Kubernetes 구조를 찾을 수 없으면 log_group_name과 log_stream_prefix가 사용되고, Fluent Bit는 다음과 같은 오류를 기록해요.
[2022/06/30 06:09:29] [ warn] [record accessor] translation failed, root key=kubernetes
record_accessor 문법의 제한 사항
앞선 예시에서 템플릿 값들은 점(.) 문자로 구분돼요. Fluent Bit record_accessor 라이브러리는 템플릿 변수를 구분할 수 있는 문자에 제한이 있어요. 템플릿 변수 뒤에는 점과 쉼표(.와 ,)만 올 수 있어요. 템플릿 라이브러리가 템플릿을 파싱해서 변수의 끝을 결정해야 하기 때문이에요.
로그 레코드에 container_name과 task라는 메타데이터 키가 있다고 가정해 봐요. 다음과 같은 것은 두 템플릿 변수가 쉼표나 점으로 구분되지 않았으므로 잘못된 템플릿이에요.
$task-$container_name$task/$container_name$task_$container_name$taskfooo$container_name
하지만 다음은 유효해요.
$task.$container_name$task.resource.$container_name$task.fooo.$container_name
그리고 다음은 템플릿 변수가 하나뿐이고 그 뒤에 아무것도 없으므로 유효해요.
fooo$taskfooo____$taskfooo/bar$container_name
메트릭 튜토리얼
Fluent Bit에는 호스트 리소스 사용 메트릭을 수집하는 입력 플러그인(cpu, mem, disk, netif)이 있어요. cloudwatch_logs 출력 플러그인은 이 호스트 메트릭을 Embedded Metric Format(EMF)으로 CloudWatch에 보내는 데 사용할 수 있어요. 데이터가 위에서 언급한 입력 플러그인 중 하나에서 오면, cloudwatch_logs 출력 플러그인은 이를 EMF 형식으로 변환해 JSON 로그로 CloudWatch에 보내요. 추가로 log_format 구성 옵션의 값으로 json/emf가 설정되면, CloudWatch는 임베디드 JSON 페이로드에서 커스텀 메트릭을 추출해요.
CloudWatch로 보낼 수 있는 것은 cpu와 mem 메트릭뿐이에요.
mem 입력 플러그인을 사용해 메모리 사용 메트릭을 CloudWatch로 보내려면 다음 예시 설정 파일을 고려해 봐요. 이 예시는 로그 레코드에 ec2_instance_id와 az(가용 영역)를 추가하는 aws 필터를 사용해요. 이후 출력 구성 섹션에서 ec2_instance_id를 메트릭 차원으로 설정해요.
fluent-bit.yaml:
service:
log_level: info
pipeline:
inputs:
- name: mem
tag: mem
filters:
- name: aws
match: '*'
outputs:
- name: cloudwatch_logs
match: '*'
region: us-west-2
log_stream_name: fluent-bit-cloudwatch
log_group_name: fluent-bit-cloudwatch
log_format: json/emf
metric_namespace: fluent-bit-metrics
metric_dimensions: ec2_instance_id
auto_create_group: true
fluent-bit.conf:
[SERVICE]
Log_Level info
[INPUT]
Name mem
Tag mem
[FILTER]
Name aws
Match *
[OUTPUT]
Name cloudwatch_logs
Match *
Region us-west-2
Log_Stream_Name fluent-bit-cloudwatch
Log_Group_Name fluent-bit-cloudwatch
Log_Format json/emf
Metric_Namespace fluent-bit-metrics
Metric_Dimensions ec2_instance_id
Auto_Create_Group On
다음 구성은 모든 메트릭에 ec2_instance_id와 az 두 개의 차원을 설정해요.
fluent-bit.yaml:
service:
log_level: info
pipeline:
inputs:
- name: mem
tag: mem
filters:
- name: aws
match: '*'
outputs:
- name: cloudwatch_logs
match: '*'
region: us-west-2
log_stream_name: fluent-bit-cloudwatch
log_group_name: fluent-bit-cloudwatch
log_format: json/emf
metric_namespace: fluent-bit-metrics
metric_dimensions: ec2_instance_id,az
auto_create_group: true
fluent-bit.conf:
[SERVICE]
Log_Level info
[INPUT]
Name mem
Tag mem
[FILTER]
Name aws
Match *
[OUTPUT]
Name cloudwatch_logs
Match *
Region us-west-2
Log_Stream_Name fluent-bit-cloudwatch
Log_Group_Name fluent-bit-cloudwatch
Log_Format json/emf
Metric_Namespace fluent-bit-metrics
Metric_Dimensions ec2_instance_id,az
Auto_Create_Group On
AWS for Fluent Bit
Amazon은 Fluent Bit와 이 플러그인들을 포함한 컨테이너 이미지를 배포해요.
GitHub
github.com/aws/aws-for-fluent-bit
Amazon ECR Public Gallery
이미지는 Amazon ECR Public Gallery에서 사용할 수 있어요. 다음 명령으로 서로 다른 태그의 이미지를 다운로드할 수 있어요.
docker pull public.ecr.aws/aws-observability/aws-for-fluent-bit:<tag>
예를 들어 latest 버전의 이미지를 가져오려면 다음과 같이 해요.
docker pull public.ecr.aws/aws-observability/aws-for-fluent-bit:latest
이미지 풀 제한 오류가 보이면 AWS 자격 증명으로 public ECR에 로그인해 봐요.
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws
자세한 내용은 Amazon ECR Public 공식 문서에서 확인할 수 있어요.
Docker Hub
Amazon ECR
SSM public 파라미터를 사용해 내 지역의 Amazon ECR 이미지 URI를 찾을 수 있어요.
aws ssm get-parameters-by-path --path /aws/service/aws-for-fluent-bit/
자세한 내용은 AWS for Fluent Bit GitHub 저장소를 참고해요.