{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "Tce3stUlHN0L" }, "source": [ "##### Copyright 2020 The TensorFlow IO Authors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "tuOe1ymfHZPu" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "qFdPvlXBOdUN" }, "source": [ "# Prometheus サーバーからメトリックを読み込む" ] }, { "cell_type": "markdown", "metadata": { "id": "MfBg1C5NB3X0" }, "source": [ "
![]() | \n",
" ![]() | \n",
" ![]() | \n",
" ![]() | \n",
"
query
はメトリックを選択するため Prometheus サーバーに渡され、length
は Dataset に読み込む期間です。\n",
"\n",
"`\"coredns_dns_request_count_total\"`と`\"5\"`(秒)から始めて、以下のデータセットを作成します。チュートリアルの前半で 2 つの DNS クエリが送信されたため、`\"coredns_dns_request_count_total\"`のメトリックは時系列の終わりに`\"2.0\"`になると予想されます。"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"id": "h21RdP7meGzP"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dataset Spec:\n",
"(TensorSpec(shape=(), dtype=tf.int64, name=None), {'coredns': {'localhost:9153': {'coredns_dns_request_count_total': TensorSpec(shape=(), dtype=tf.float64, name=None)}}})\n",
"\n",
"CoreDNS Time Series:\n",
"2020-03-03 22:35:17: 2.0\n",
"2020-03-03 22:35:18: 2.0\n",
"2020-03-03 22:35:19: 2.0\n",
"2020-03-03 22:35:20: 2.0\n",
"2020-03-03 22:35:21: 2.0\n"
]
}
],
"source": [
"dataset = tfio.experimental.IODataset.from_prometheus(\n",
" \"coredns_dns_request_count_total\", 5, endpoint=\"http://localhost:9090\")\n",
"\n",
"\n",
"print(\"Dataset Spec:\\n{}\\n\".format(dataset.element_spec))\n",
"\n",
"print(\"CoreDNS Time Series:\")\n",
"for (time, value) in dataset:\n",
" # time is milli second, convert to data time:\n",
" time = datetime.fromtimestamp(time // 1000)\n",
" print(\"{}: {}\".format(time, value['coredns']['localhost:9153']['coredns_dns_request_count_total']))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8y-VpwcWNYTF"
},
"source": [
"データセットの仕様をさらに見てみましょう。\n",
"\n",
"```\n",
"( TensorSpec(shape=(), dtype=tf.int64, name=None), { 'coredns': { 'localhost:9153': { 'coredns_dns_request_count_total': TensorSpec(shape=(), dtype=tf.float64, name=None) } } } )\n",
"```\n",
"\n",
"データセットが`(time, values)`タプルで構成されていることは明らかです。`values`フィールドは、次のように展開された python dict です。\n",
"\n",
"```\n",
"\"job_name\": { \"instance_name\": { \"metric_name\": value, }, }\n",
"```\n",
"\n",
"上記の例では、`'coredns'`はジョブ名、`'localhost:9153'`はインスタンス名、`'coredns_dns_request_count_total'`はメトリック名です。使用する Prometheus クエリによっては、複数のジョブ/インスタンス/メトリックが返される可能性があることに注意してください。これは、データセットの構造で python dict が使用されている理由でもあります。\n",
"\n",
"別のクエリ`\"go_memstats_gc_sys_bytes\"`を例として見てみましょう。CoreDNS と Prometheus はどちらも Golang で記述されているため、`\"go_memstats_gc_sys_bytes\"`メトリックは、`\"coredns\"`ジョブと`\"prometheus\"`ジョブの両方で使用できます。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5CA3JUIkduY5"
},
"source": [
"注: このセルは、最初に実行したときにエラーになる場合があります。もう一度実行すると、パスします。"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "qCoueXYZOvqZ"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time Series CoreDNS/Prometheus Comparision:\n",
"2020-03-03 22:35:17: 2385920.0/2775040.0\n",
"2020-03-03 22:35:18: 2385920.0/2775040.0\n",
"2020-03-03 22:35:19: 2385920.0/2775040.0\n",
"2020-03-03 22:35:20: 2385920.0/2775040.0\n",
"2020-03-03 22:35:21: 2385920.0/2775040.0\n"
]
}
],
"source": [
"dataset = tfio.experimental.IODataset.from_prometheus(\n",
" \"go_memstats_gc_sys_bytes\", 5, endpoint=\"http://localhost:9090\")\n",
"\n",
"print(\"Time Series CoreDNS/Prometheus Comparision:\")\n",
"for (time, value) in dataset:\n",
" # time is milli second, convert to data time:\n",
" time = datetime.fromtimestamp(time // 1000)\n",
" print(\"{}: {}/{}\".format(\n",
" time,\n",
" value['coredns']['localhost:9153']['go_memstats_gc_sys_bytes'],\n",
" value['prometheus']['localhost:9090']['go_memstats_gc_sys_bytes']))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xO2pheWEPQSU"
},
"source": [
"作成された`Dataset`は、トレーニングまたは推論のために直接`tf.keras`に渡す準備ができています。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DhVm2fGaoyuA"
},
"source": [
"## モデルトレーニングにデータセットを使用する\n",
"\n",
"メトリクスのデータセットを作成すると、モデルのトレーニングや推論のためにデータセットを`tf.keras`に直接渡すことができます。\n",
"\n",
"デモのために、このチュートリアルでは、1 つの特徴と 2 つのステップを入力とする非常に単純な LSTM モデルを使用します。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fxObBtlvr6n_"
},
"outputs": [],
"source": [
"n_steps, n_features = 2, 1\n",
"simple_lstm_model = tf.keras.models.Sequential([\n",
" tf.keras.layers.LSTM(8, input_shape=(n_steps, n_features)),\n",
" tf.keras.layers.Dense(1)\n",
"])\n",
"\n",
"simple_lstm_model.compile(optimizer='adam', loss='mae')\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Moh_tEGZu-3_"
},
"source": [
"使用するデータセットは、10 サンプルの CoreDNS の「go_memstats_sys_bytes」の値です。ただし、`window = n_steps`および`shift = 1`のスライディングウィンドウが形成されるため、追加のサンプルが必要です (2 つの連続する要素の場合、最初のサンプルは`x`で、2 番目は`y`と見なされます) 。合計は`10 + n_steps - 1 + 1 = 12` 秒です。\n",
"\n",
"また、データ値は`[0、1]`にスケーリングされています。"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"id": "CZmStrvFvJLN"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train for 10 steps\n",
"Epoch 1/5\n",
"10/10 [==============================] - 2s 150ms/step - loss: 0.8484\n",
"Epoch 2/5\n",
"10/10 [==============================] - 0s 10ms/step - loss: 0.7808\n",
"Epoch 3/5\n",
"10/10 [==============================] - 0s 10ms/step - loss: 0.7102\n",
"Epoch 4/5\n",
"10/10 [==============================] - 0s 11ms/step - loss: 0.6359\n",
"Epoch 5/5\n",
"10/10 [==============================] - 0s 11ms/step - loss: 0.5572\n"
]
},
{
"data": {
"text/plain": [
"