You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS AppRunner中Kotlin应用:通过Agent或直接向AMP发送指标可行吗

Kotlin应用(Micrometer埋点)对接Amazon Managed Prometheus(AMP)方案

一、直接从应用远程写入AMP

Micrometer原生支持Prometheus远程写入协议,配合AWS SigV4签名就能直接把指标推送到AMP,无需额外Agent中转。具体操作如下:

  • 先获取AMP的远程写入端点,格式为https://aps-workspaces.${region}.amazonaws.com/workspaces/${workspace-id}/api/v1/remote_write,替换成你的AWS区域和AMP工作区ID。
  • 给AppRunner的执行角色添加aps:RemoteWrite权限,确保应用具备访问AMP的权限。
  • 在Kotlin代码中配置PrometheusRemoteWriteRegistry,借助AWS SDK自动处理SigV4签名,示例代码:
import io.micrometer.prometheus.PrometheusConfig
import io.micrometer.prometheus.PrometheusMeterRegistry
import io.micrometer.prometheus.remote.PrometheusRemoteWriteRegistry
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.amp.AmpClient

fun setupAMPRemoteWrite() {
    val workspaceId = "你的AMP工作区ID"
    val region = Region.of("us-east-1") // 替换为你的AWS区域
    val remoteWriteUrl = "https://aps-workspaces.${region.id()}.amazonaws.com/workspaces/${workspaceId}/api/v1/remote_write"

    val promConfig = PrometheusConfig.DEFAULT
    val meterRegistry = PrometheusMeterRegistry(promConfig)

    val remoteWriteRegistry = PrometheusRemoteWriteRegistry.builder()
        .url(remoteWriteUrl)
        .config(promConfig)
        .client { request ->
            val ampClient = AmpClient.builder()
                .credentialsProvider(DefaultCredentialsProvider.create())
                .region(region)
                .build()
            val signedRequest = ampClient.createSignedRequest(request)
            signedRequest.execute()
        }
        .build()

    remoteWriteRegistry.add(meterRegistry)
}

二、用Victoria Metrics Agent或Grafana Agent中转

如果不想在应用内处理AWS认证逻辑,或者需要统一收集多个AppRunner实例的指标,用Agent中转是更合适的选择,两种Agent都支持从应用暴露的Prometheus端点拉取指标,再推送到AMP。

1. Grafana Agent配置

可将Grafana Agent作为sidecar容器,与Kotlin应用一同部署在AppRunner中:

  • 编写agent.yaml配置文件:
metrics:
  wal_directory: /tmp/grafana-agent-wal
  configs:
    - name: amp-writer
      scrape_configs:
        - job_name: kotlin-app-metrics
          static_configs:
            - targets: ["localhost:8080"] # 应用暴露的Prometheus指标端口
      remote_write:
        - url: "https://aps-workspaces.${region}.amazonaws.com/workspaces/${workspace-id}/api/v1/remote_write"
          sigv4:
            region: "${region}" # 替换为你的AWS区域
          queue_config:
            max_samples_per_send: 1000
            max_shards: 200
            capacity: 2500
  • 给Agent所在的AppRunner执行角色添加aps:RemoteWrite权限,确保其能向AMP写入数据。

2. Victoria Metrics Agent配置

Victoria Metrics Agent配置更轻量化,同样可作为sidecar部署:

  • 启动命令(替换占位符):
./vmagent-prod \
  -promscrape.config=/etc/vmagent/scrape.yaml \
  -remoteWrite.url=https://aps-workspaces.${region}.amazonaws.com/workspaces/${workspace-id}/api/v1/remote_write \
  -remoteWrite.sigv4.region=${region}
  • 对应的scrape.yaml配置:
scrape_configs:
  - job_name: kotlin-app
    static_configs:
      - targets: ["localhost:8080"]
  • 权限要求与Grafana Agent一致,需确保IAM角色具备aps:RemoteWrite权限。

内容的提问来源于stack exchange,提问作者Piyush Nahar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:39:56