すべてのプロダクト
Search
ドキュメントセンター

Simple Log Service:SDK を使用したメトリックの送信

最終更新日:Mar 26, 2026

SDK を使用して、Simple Log Service へメトリックを送信できます。本トピックでは、Java、Go、Python 各言語向け SDK のコード例を示します。

説明
  • SDK を使用してメトリックを送信する場合、データはメトリックフォーマットに準拠している必要があります。

  • SDK を使用してメトリックを送信する場合、TopicSource、および LogTags パラメーターは無視され、metricStore には保存されません。

  • データ送信には、プロデューサー(Producer)ライブラリの使用を推奨します。プロデューサー(Producer)ライブラリは、JavaGoC 向けに提供されています。その他の言語では、ネットワークリクエスト数を削減するために、複数のデータレコードをロググループにまとめて送信してください。

  • __time_nano__ フィールドは、秒、ミリ秒、マイクロ秒、ナノ秒単位のタイムスタンプをサポートしています。

  • 本トピックの SDK サンプルコードでは、AccessKey ペアの設定に環境変数が使用されています。詳細については、「環境変数の設定」をご参照ください。

Java SDK のサンプル

インストール手順については、「Aliyun Log Java Producer のインストール」をご参照ください。

import com.aliyun.openservices.aliyun.log.producer.Callback;
import com.aliyun.openservices.aliyun.log.producer.LogProducer;
import com.aliyun.openservices.aliyun.log.producer.Producer;
import com.aliyun.openservices.aliyun.log.producer.ProducerConfig;
import com.aliyun.openservices.aliyun.log.producer.ProjectConfig;
import com.aliyun.openservices.aliyun.log.producer.Result;
import com.aliyun.openservices.aliyun.log.producer.errors.ProducerException;
import com.aliyun.openservices.log.common.LogItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
    private static final Random random = new Random();

    public static void main(String[] args) throws InterruptedException {
        final String project = "";
        final String logstore = "";
        // Simple Log Service のエンドポイントです。この例では中国 (杭州) リージョンのエンドポイントを使用しています。実際のエンドポイントに置き換えてください。
        final String endpoint = "https://cn-hangzhou.log.aliyuncs.com";
        // この例では、AccessKey ID および AccessKey Secret を環境変数から取得しています。
        final String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        final String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        int sendThreadCount = 8;
        final int times = 10;
        LOGGER.info(
                "project={}, logstore={}, endpoint={}, sendThreadCount={}, times={}",
                project, logstore, endpoint, sendThreadCount, times);
        ExecutorService executorService = Executors.newFixedThreadPool(sendThreadCount);
        ProducerConfig producerConfig = new ProducerConfig();
        producerConfig.setBatchSizeThresholdInBytes(3 * 1024 * 1024);
        producerConfig.setBatchCountThreshold(40960);

        final Producer producer = new LogProducer(producerConfig);
        producer.putProjectConfig(new ProjectConfig(project, endpoint, accessKeyId, accessKeySecret));

        final AtomicInteger completedCount = new AtomicInteger(0);
        LOGGER.info("テストを開始します。");
        long t1 = System.currentTimeMillis();
        final Map<String, String> labels = new HashMap<String, String>();
        labels.put("test_k", "test_v");
        for (int i = 0; i < sendThreadCount; ++i) {
            executorService.submit(
                    new Runnable() {
                        @Override
                        public void run() {
                            try {
                                for (int i = 0; i < times; ++i) {
                                    int r = random.nextInt(times);
                                    producer.send(project, logstore, generateTopic(r), generateSource(r),
                                            buildLogItem("test_metric", labels, i),
                                            new Callback() {
                                                @Override
                                                public void onCompletion(Result result) {
                                                    completedCount.incrementAndGet();
                                                    if (!result.isSuccessful()) {
                                                        LOGGER.error(
                                                                "ログの送信に失敗しました。project={}, logstore={}, result={}",
                                                                project,
                                                                logstore,
                                                                result);
                                                    }
                                                }
                                            });
                                }
                            } catch (Exception e) {
                                LOGGER.error("ログの送信に失敗しました。e=", e);
                            }
                        }
                    });
        }
        while (completedCount.get() < sendThreadCount * times) {
            Thread.sleep(100);
        }
        long t2 = System.currentTimeMillis();
        LOGGER.info("テストを終了します。");
        LOGGER.info("======概要======");
        LOGGER.info("合計件数:" + sendThreadCount * times + " 件。");
        long timeCost = t2 - t1;
        LOGGER.info("処理時間:" + timeCost + " ミリ秒");
        try {
            producer.close();
        } catch (ProducerException e) {
            LOGGER.error("プロデューサーのクローズに失敗しました。e=", e);
        }
        executorService.shutdown();
    }

    private static String generateTopic(int r) {
        return "topic-" + r % 5;
    }

    private static String generateSource(int r) {
        return "source-" + r % 10;
    }

    /**
     * @param metricName: メトリック名(例:http_requests_count)
     * @param labels:     ラベルのマップ(例:{'idc': 'idc1', 'ip': '192.0.2.0', 'hostname': 'appserver1'})
     * @param value:      double 型の値(例:1.234)
     * @return LogItem
     */
    public static LogItem buildLogItem(String metricName, Map<String, String> labels, double value) {
        String labelsKey = "__labels__";
        String timeKey = "__time_nano__";
        String valueKey = "__value__";
        String nameKey = "__name__";
        LogItem logItem = new LogItem();
        int timeInSec = (int) (System.currentTimeMillis() / 1000);
        logItem.SetTime(timeInSec);
        logItem.PushBack(timeKey, timeInSec + "000000");
        logItem.PushBack(nameKey, metricName);
        logItem.PushBack(valueKey, String.valueOf(value));

        // ラベルをアルファベット順にソートします。既にソート済みの場合はこのステップをスキップできます。
        TreeMap<String, String> sortedLabels = new TreeMap<String, String>(labels);
        StringBuilder labelsBuilder = new StringBuilder();

        boolean hasPrev = false;
        for (Map.Entry<String, String> entry : sortedLabels.entrySet()) {
            if (hasPrev) {
                labelsBuilder.append("|");
            }
            hasPrev = true;
            labelsBuilder.append(entry.getKey());
            labelsBuilder.append("#$#");
            labelsBuilder.append(entry.getValue());
        }
        logItem.PushBack(labelsKey, labelsBuilder.toString());
        return logItem;
    }
}
            

Python SDK のサンプル

詳細については、「Simple Log Service SDK for Python のインストール」をご参照ください。

# encoding: utf-8
import time
import os
from aliyun.log import *

def build_log_item(metric_name, labels, value):
    """
    ログ項目の作成
    :param metric_name: メトリック名(例:http_requests_count)
    :param labels: ラベルの辞書(例:{'idc': 'idc1', 'ip': '192.0.2.0', 'hostname': 'appserver1'})
    :param value: double 型の値(例:1.234)
    :return: LogItem
    """
    sorted_labels = sorted(labels.items())
    log_item = LogItem()
    now = int(time.time())
    log_item.set_time(now)
    contents = [('__time_nano__', str(now)), ('__name__', metric_name), ('__value__', str(value))]

    labels_str = ''
    for i, kv in enumerate(sorted_labels):
        labels_str += kv[0]
        labels_str += '#$#'
        labels_str += kv[1]
        if i < len(sorted_labels) - 1:
            labels_str += '|'

    contents.append(('__labels__', labels_str))
    log_item.set_contents(contents)
    return log_item

def main():
    # Simple Log Service のエンドポイントです。この例では中国 (杭州) リージョンのエンドポイントを使用しています。実際のエンドポイントに置き換えてください。
    endpoint = 'cn-hangzhou.log.aliyuncs.com'
    # この例では、AccessKey ID および AccessKey Secret を環境変数から取得しています。
    accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
    accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
    project = ''
    logstore = ''

    client = LogClient(endpoint, accessKeyId, accessKey)
    item1 = build_log_item('test1', {'k1': 'v1'}, 1)
    item2 = build_log_item('test2', {'k2': 'v2'}, 2)
    item3 = build_log_item('test3', {'k3': 'v3'}, 3)
    items = [item1, item2, item3]
    res = client.put_logs(PutLogsRequest(project=project, logstore=logstore, logitems=items))
    res.log_print()

if __name__ == '__main__':
    main()

Go SDK のサンプル

詳細については、「Go SDK のインストール」をご参照ください。

package main

import (
   "fmt"
   sls "github.com/aliyun/aliyun-log-go-sdk"
   "github.com/aliyun/aliyun-log-go-sdk/producer"
   "github.com/golang/protobuf/proto"
   "os"
   "os/signal"
   "sort"
   "strconv"
   "sync"
   "time"
)

func buildLogItem(metricName string, labels map[string]string, value float64) *sls.Log {

   now := uint32(time.Now().Unix())
   log := &sls.Log{Time: proto.Uint32(now)}

   var contents []*sls.LogContent
   contents = append(contents, &sls.LogContent{
      Key:   proto.String("__time_nano__"),
      Value: proto.String(strconv.FormatInt(int64(now), 10)),
   })
   contents = append(contents, &sls.LogContent{
      Key:   proto.String("__name__"),
      Value: proto.String(metricName),
   })
   contents = append(contents, &sls.LogContent{
      Key:   proto.String("__value__"),
      Value: proto.String(strconv.FormatFloat(value, 'f', 6, 64)),
   })

   keys := make([]string, 0, len(labels))
   for k := range labels {
      keys = append(keys, k)
   }
   sort.Strings(keys)

   labelsStr := ""
   for i, k := range keys {
      labelsStr += k
      labelsStr += "#$#"
      labelsStr += labels[k]
      if i < len(keys) - 1 {
         labelsStr += "|"
      }
   }

   contents = append(contents, &sls.LogContent{Key: proto.String("__labels__"), Value: proto.String(labelsStr)})
   log.Contents = contents
   return log

}

func main() {
   project := ""
   logstore := ""

   producerConfig := producer.GetDefaultProducerConfig()
   // Simple Log Service のエンドポイントです。この例では中国 (杭州) リージョンのエンドポイントを使用しています。実際のエンドポイントに置き換えてください。
   producerConfig.Endpoint = "https://cn-hangzhou.log.aliyuncs.com"
   // この例では、AccessKey ID および AccessKey Secret を環境変数から取得しています。
   producerConfig.AccessKeyID = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
   producerConfig.AccessKeySecret = os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
   producerInstance, err := producer.NewProducer(producerConfig)
   if err != nil {
     panic(err)
   }
   ch := make(chan os.Signal)
   signal.Notify(ch)
   producerInstance.Start()
   var m sync.WaitGroup
   for i := 0; i < 10; i++ {
      m.Add(1)
      go func() {
         defer m.Done()
         for i := 0; i < 1000; i++ {
            // `GenerateLog` は、Simple Log Service 形式でログを作成するためのヘルパー関数です。高いパフォーマンスを実現するには、ネイティブの Log インターフェイスを直接使用することを推奨します。
            log := buildLogItem("test_metric", map[string]string{"test_k":"test_v"}, float64(i))
            err := producerInstance.SendLog(project, logstore, "topic", "127.0.0.1", log)
            if err != nil {
               fmt.Println(err)
            }
         }
      }()
   }
   m.Wait()
   fmt.Println("送信完了")
   if _, ok := <-ch; ok {
      fmt.Println("シャットダウン信号を受信しました。シャットダウンを開始します。")
      producerInstance.Close(60000)
   }
}