带有千分尺 Elasticsearch 注册表的 Spring Boot 仅索引空文档

Mar*_*vic 3 elasticsearch spring-boot micrometer spring-micrometer

我有一个使用 micrometer Elasticsearch 注册表(使用 Elasticsearch 7.2.0)的简单 spring boot 2.1.7.RELEASE项目。该项目可在这里GitHub上。它只有两个类,看起来像这样

pom.xml具有以下依赖项:

<dependencies>
  <dependency>
    <artifactId>spring-boot-starter-web</artifactId>
    <groupId>org.springframework.boot</groupId>
  </dependency>

  <dependency>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <groupId>org.springframework.boot</groupId>
  </dependency>

  <dependency>
    <artifactId>micrometer-registry-elastic</artifactId>
    <groupId>io.micrometer</groupId>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

和两个类: MicrometerElasticApplication

@SpringBootApplication
public class MicrometerElasticApplication {
  public static void main(final String[] args) {
    SpringApplication.run(MicrometerElasticApplication.class, args);
  }
}
Run Code Online (Sandbox Code Playgroud)

和测试控制器

@RestController
public class TestController {
  @ResponseStatus(HttpStatus.OK)
  @GetMapping("/test")
  public void sendMessage() {
    System.out.println("Received a test message");
  }
}
Run Code Online (Sandbox Code Playgroud)

启动应用程序后,我可以在日志中看到

i.m.elastic.ElasticMeterRegistry         : publishing metrics to elastic every 1m
Run Code Online (Sandbox Code Playgroud)

这意味着发送了指标,但是当我检查 Elasticsearch 中索引的内容时,我只能看到这样的文档

{
    "_index": "metrics-2019-08",
    "_type": "_doc",
    "_id": "nWuMdWwBxBoi4XILEHVK",
    "_score": 1.0
}
Run Code Online (Sandbox Code Playgroud)

所以没有字段,只是记录元数据。即使在访问/test端点服务器次数之后,metrics索引中也没有任何变化。

我从阅读官方文档了解这里,并检查公共属性在这里是Spring默认情况下是要添加针对JVM,CPU ......甚至是衡量所有MVC请求时序。现在,我什么也没有得到,只有空文件。将该属性设置management.metrics.web.server.auto-time-requeststrue不会改变任何内容。

有人看到我在这里缺少什么吗?

更新

我给ElasticMeterRegistry.publish方法设置了一个断点,发送到 Elaticsearch /_bulkAPI的请求对我来说看起来不错

POST http://localhost:9200/metrics-2019-08/_bulk

{ "index" : {} }
{"@timestamp":"2019-08-09T10:49:18.826Z","name":"jvm_memory_max","type":"gauge","area":"heap","id":"PS Survivor Space","value":1.5204352E7}
{ "index" : {} }
{"@timestamp":"2019-08-09T10:49:18.826Z","name":"jvm_threads_states","type":"gauge","state":"terminated","value":0.0}
...
Run Code Online (Sandbox Code Playgroud)

当我使用 Postman 发送此请求时,所有文档都保存为空文档,尽管 Elasticsearch"errors": false在响应中没有报告任何错误

{
    "took": 8,
    "errors": false,
    "items": [
        ...
    ]
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*vic 10

由其metrics-2019-08创建的索引ElasticMeterRegistry在其映射中设置_source为 false

GET http://localhost:9200/metrics-2019-08/_mapping
Run Code Online (Sandbox Code Playgroud)

响应是

"metrics-2019-08": {
    "mappings": {
        "_source": {
            "enabled": false
        }
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

来自此处的 Elasticserch 文档

_source 字段本身未编入索引(因此不可搜索),但它会被存储,以便在执行获取请求(如 get 或 search)时可以返回它。

因此,如果_source为 false,则每个获取文档的请求都返回空源。禁用此功能背后的原因是文档仅用于聚合(avg、min、max、sum...),_source这些不需要,因此_source不存储节省磁盘空间。

要禁用此行为,请设置management.metrics.export.elastic.auto-create-indexfalse。如果您已经使用true运行它,则需要使用以下命令删除现有模板

DELETE http://localhost:9200/_template/metrics_template
Run Code Online (Sandbox Code Playgroud)

之后,为指标索引创建模板,如下所示:

POST http://localhost:9200/_template/metrics_template
{
  "index_patterns": [
    "metrics*"
  ],
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "name": {
        "type": "keyword"
      },
      "count": {
        "type": "double"
      },
      "value": {
        "type": "double"
      },
      "sum": {
        "type": "double"
      },
      "mean": {
        "type": "double"
      },
      "duration": {
        "type": "double"
      },
      "max": {
        "type": "double"
      },
      "total": {
        "type": "double"
      },
      "unknown": {
        "type": "double"
      },
      "active": {
        "type": "double"
      }
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

此模板与 micrometer 使用的模板相同,_source但设置为true。在此之后,文档将出现在获取请求中,例如获取或搜索。