lindb

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2023 License: Apache-2.0 Imports: 3 Imported by: 1

README

LinDB Client Go

LICENSE Language Go Report Card GitHub release codecov Github Actions Status GoDoc

This repository contains the reference Go client for LinDB.

Features

  • Write data
    • Write data use asynchronous
    • Support field type(sum/min/max/last/first/histogram)
    • FlatBuf Protocol
  • Query data
    • Query metric data/metadata

How To Use

Installation

Go 1.19 or later is required.

  • Add the client package to your project dependencies (go.mod).

    go get github.com/lindb/client_go
    
  • Add import github.com/lindb/client_go to your source code.

Write data
package main

import (
	"context"
	"fmt"
	"time"

	lindb "github.com/lindb/client_go"
	"github.com/lindb/client_go/api"
)

func main() {
	// create write client with options
	cli := lindb.NewClientWithOptions(
		"http://localhost:9000",
		lindb.DefaultOptions().SetBatchSize(200).
			SetReqTimeout(60).
			SetRetryBufferLimit(100).
			SetMaxRetries(3),
	)
	// get write client
	w := cli.Write("_internal")
	// get error chan
	errCh := w.Errors()
	go func() {
		for err := range errCh {
			fmt.Printf("got err:%s\n", err)
		}
	}()

	// write some metric data
	for i := 0; i < 10; i++ {
		// write cpu data
		w.AddPoint(context.TODO(), api.NewPoint("cpu").
			AddTag("host", "host1").
			AddField(api.NewSum("load", 10.0)).
			AddField(api.NewLast("usage", 24.0)))
		// write memory data
		w.AddPoint(context.TODO(), api.NewPoint("memory").
			AddTag("host", "host1").
			AddField(api.NewLast("used", 10.0)).
			AddField(api.NewLast("total", 24.0)))
	}

	// close write client
	w.Close()
}
Reading background process errors

Write client doesn't log any error. Can use Errors() method, which returns the channel for reading errors occurring during async writes.

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/lindb/client_go"
	"github.com/lindb/client_go/api"
)

func main() {
	// create write client
	cli := lindb.NewClient("http://localhost:9000")
	w := cli.Write("_internal")
	// get error chan
	errCh := w.Errors()
	go func() {
		for err := range errCh {
			fmt.Printf("got err:%s\n", err)
		}
	}()

	// write data
	for i := 0; i < 10; i++ {
		p := api.NewPoint("cpu").
			AddTag("host", "host1").
			AddField(api.NewSum("load", 10.0)).
			AddField(api.NewLast("usage", 24.0))
		w.AddPoint(context.TODO(), p)
	}

	// close write client
	w.Close()
}
Query data

More examples

package main

import (
	"context"
	"fmt"

	lindb "github.com/lindb/client_go"
)

func main() {
	// create write client
	cli := lindb.NewClient("http://localhost:9000")
	query := cli.DataQuery()
	// LinQL ref: https://lindb.io/guide/lin-ql.html#metric-query
	data, err := query.DataQuery(context.TODO(),
		"_internal",
		"select heap_objects from lindb.runtime.mem where time>now()-2m and 'role' in ('Broker') group by node")
	if err != nil {
		fmt.Println(err)
		return
	}
	// print table
	_, table := data.ToTable()
	fmt.Println(table)
}
Options
package http

import "crypto/tls"

type Options struct {
	// Request timeout(s), default 30.
	reqTimeout int64
	// TLS configuration for secure connection, default nil.
	tlsConfig *tls.Config
}

See tls.Config for detail.

package api

type WriteOptions struct {
	// Number of series sent in single write request, default 1000.
	batchSize int
	// Flush interval(ms) which is buffer flushed if it has not been already written, default 1000.
	flushInterval int64
	// Whether to use GZip compress write data, default true.
	useGZip bool
	// Default tags are added to each written series.
	defaultTags map[string]string
	// Maximum count of retry attempts of failed writes, default 3.
	maxRetries int
	// Maximum number of write request to keep for retry, default 100.
	retryBufferLimit int
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Write returns an asynchronous write client.
	Write(database string) api.Write
	// DataQuery returns a metric data query client.
	DataQuery() api.DataQuery
}

Client represents the api to communicate with LinDB backend server. Ref InfluxDB client: https://github.com/influxdata/influxdb-client-go

func NewClient

func NewClient(brokerEndpoint string) Client

NewClient creates a Client with backend endpoint and default options.

func NewClientWithOptions

func NewClientWithOptions(brokerEndpoint string, options *Options) Client

NewClientWithOptions creates a Client with backend endpoint and options.

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options represents configuration options for client.

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions creates an Options with default.

func (*Options) AddDefaultTag

func (o *Options) AddDefaultTag(key, value string) *Options

AddDefaultTag adds default tag for all metrics.

func (*Options) HTTPOptions

func (o *Options) HTTPOptions() *http.Options

HTTPOptions returns the HTTP options, if not set return default options.

func (*Options) SetBatchSize

func (o *Options) SetBatchSize(batchSize int) *Options

SetBatchSize sets batch size in single write request.

func (*Options) SetFlushInterval

func (o *Options) SetFlushInterval(interval int64) *Options

SetFlushInterval sets flush interval(ms)

func (*Options) SetMaxRetries

func (o *Options) SetMaxRetries(maxRetries int) *Options

SetMaxRetries sets maximum count of retry attempts of failed write.

func (*Options) SetReqTimeout

func (o *Options) SetReqTimeout(timeout int64) *Options

SetReqTimeout sets HTTP request timeout(sec)

func (*Options) SetRetryBufferLimit

func (o *Options) SetRetryBufferLimit(retryBufferLimit int) *Options

SetRetryBufferLimit sets maximum number of write request to keep for retry.

func (*Options) SetTLSConfig

func (o *Options) SetTLSConfig(tlsConfig *tls.Config) *Options

SetTLSConfig sets TLS configuration for secure connection.

func (*Options) SetUseGZip

func (o *Options) SetUseGZip(useGZip bool) *Options

SetUseGZip sets whether to use GZip compress write data.

func (*Options) WriteOptions

func (o *Options) WriteOptions() *api.WriteOptions

WriteOptions returns the write options, if not set return default options.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL