elastic kibana可视化数据统计案例

部署:

复制代码
docker run -d --name es   -p 9200:9200   -e "discovery.type=single-node"   -e "xpack.security.enabled=false"   -e "ES_JAVA_OPTS=-Xms1g -Xmx1g"   docker.elastic.co/elasticsearch/elasticsearch:9.2.3

docker run -d --name kibana   -p 5601:5601   --link es:elasticsearch   -e ELASTICSEARCH_HOSTS=http://es:9200   docker.elastic.co/kibana/kibana:9.2.3

后台:

localhost:5601

保存es数据:

1、创建索引

注意时间格式要求 "@timestamp":

"2025-12-25T23:54:34.252Z"

复制代码
from elasticsearch import Elasticsearch

# 连接 ES
es = Elasticsearch("http://*****:9200")

es.indices.delete(index='llm-agent-logs')


index_name = "llm-agent-logs"

#如果索引不存在,创建索引并定义 mapping
if not es.indices.exists(index=index_name):
    mapping = {
        "mappings": {
            "properties": {
                "@timestamp": {"type": "date"},  # 时间戳
     
                "user": {
                    "properties": {
                        "user_id": {"type": "keyword"},
                        "user_id_type": {"type": "keyword"},
                        "anonymous_id": {"type": "keyword"},
                        "level": {"type": "keyword"},
                        "platform": {"type": "keyword"}
                    }
                },
                "messages": {
                    "properties": {
                        "user_query": {"type": "text"},
                        "llm_response": {"type": "text"}
                    }
                },
                "tools": {
                     "properties": {
                        "name":   { "type": "keyword" },
                        "args":   { "type": "object", "enabled": False },
                        "result": { "type": "text" }
  }
                },
                "llm_metrics": {
                    "properties": {
                        "model": {"type": "keyword"},
                        "tokens_in": {"type": "integer"},
                        "tokens_out": {"type": "integer"},
                        "tokens_all": {"type": "integer"},
                        "tokens_speed": {"type": "float"},
                        "llm_time": {"type": "float"}
                    }
                }
            }
        }
    }

    es.indices.create(index=index_name, body=mapping)
    print(f"索引 {index_name} 创建成功!")
else:
    print(f"索引 {index_name} 已存在")

2、插入数据

复制代码
from elasticsearch import AsyncElasticsearch

es = AsyncElasticsearch(
            ["http://*****:9200"],  # 修正 URL 格式

            verify_certs=False,
            max_retries=3,
            retry_on_timeout=True
        )
        # 测试连接
        await es.info()

def save_to_es(log, index_name="llm-agent-logs"):
    global es
    if es is None:
        logger.error("ES 客户端未初始化")
        return None
    try:
        resp = await es.index(index=index_name, document=log)
        
        return resp
    except Exception as e:
        logger.error(f"ES 索引失败: {str(e)}")
        return None

es_log ={"@timestamp": es_timestamp_cst(),"tools": []}

es_log["user"] = {
            "user_id": user_id,
            "user_id_type": user_id_type,
            "anonymous_id": anonymous_id,
            "level": level_desc,
            "platform": platform_type
        }


save_to_es(es_log)

数据分析可视化

discover主要是原始数据

dashboards可视化后台

1、创建自定义可视化后台



相关推荐
Fcy6481 小时前
Linux下 进程(一)(冯诺依曼体系、操作系统、进程基本概念与基本操作)
linux·运维·服务器·进程
袁袁袁袁满1 小时前
Linux怎么查看最新下载的文件
linux·运维·服务器
代码游侠2 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
Harvey9032 小时前
通过 Helm 部署 Nginx 应用的完整标准化步骤
linux·运维·nginx·k8s
珠海西格电力科技3 小时前
微电网能量平衡理论的实现条件在不同场景下有哪些差异?
运维·服务器·网络·人工智能·云计算·智慧城市
释怀不想释怀3 小时前
Linux环境变量
linux·运维·服务器
zzzsde3 小时前
【Linux】进程(4):进程优先级&&调度队列
linux·运维·服务器
SEO_juper4 小时前
2026内容营销破局指南:告别流量内卷,以价值赢信任
人工智能·ai·数字营销·2026
七夜zippoe4 小时前
脉向AI|当豆包手机遭遇“全网封杀“:GUI Agent是通向AGI的必经之路吗?
人工智能·ai·智能手机·agent·gui
聆风吟º5 小时前
CANN开源项目实战指南:使用oam-tools构建自动化故障诊断与运维可观测性体系
运维·开源·自动化·cann