Skip to content

Latest commit

 

History

History
153 lines (120 loc) · 4.14 KB

README.md

File metadata and controls

153 lines (120 loc) · 4.14 KB

Parseable Logo
Parseable Golang SDK

Docker Pulls Slack Docs Build

Parseable is a lightweight, cloud native log observability engine. Written in Rust, Parseable is built for high ingestion rates and low resource consumption. It is compatible with all major log agents and can be configured to collect logs from any source. Read more in Parseable docs.

Parseable Golang SDK

This golang sdk can be embeeded in applications to send logs directly to parseable.

Installation

The Parseable golang sdk can be installed using go:

go get github.com/parseablehq/parseable-sdk-go/parseable

Example Usage

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"time"

	"github.com/parseablehq/parseable-sdk-go/parseable"
)

// Sample log data in JSON format
var log = `[
	{
		"message": "tenants creation failed",
		"type": "error",
		"reason": "TenantsCreationFailed",
		"object": "baaz/tenants"
	}
]`

// Default duration for the query
var duration = "10h"

// QueryBuilder represents a query builder for fetching logs
type QueryBuilder struct {
	Query     string    `json:"query"`     // Query string
	StartTime time.Time `json:"startTime"` // Start time for the query
	EndTime   time.Time `json:"endTime"`   // End time for the query
}

func main() {
	// Set environment variables for Parseable SDK
	os.Setenv("PARSEABLE_URL", "http://localhost:9000")
	os.Setenv("PARSEABLE_USERNAME", "admin")
	os.Setenv("PARSEABLE_PASSWORD", "admin")

	// Define stream name
	stream := "mystream"

	// Create a new stream
	streamBuilder := parseable.NewStreamBuilder(
		stream,
		nil, // No initial data
		nil, // No metadata
		nil, // No tags
	)

	resp, err := streamBuilder.CreateStream()
	if err != nil && resp == 400 {
		fmt.Println(err)
		os.Exit(1)
	}

	// Labels associated with the log data
	labels := map[string]string{
		"type": "json",
	}

	// Tags associated with the log data
	tags := map[string]string{
		"app": "myapp",
	}

	// Create a new stream with log data, labels, and tags
	logStream := parseable.NewStreamBuilder(
		stream,
		[]byte(log), // Log data
		labels,      // Labels
		tags,        // Tags
	)

	// Insert logs into the stream
	_, err = logStream.InsertLogs()
	if err != nil {
		fmt.Println(err)
	}

	// Parse duration string to time.Duration
	duration, err := time.ParseDuration("-" + duration)
	if err != nil {
		fmt.Println(err)
	}

	// Get current time
	currentTime := time.Now()

	// Calculate start time by subtracting duration from current time
	startTime := currentTime.Add(duration)

	// Construct a new query with stream name and start/end times
	newQuery := QueryBuilder{
		Query:     fmt.Sprintf("select * from %s", stream),
		StartTime: startTime,
		EndTime:   currentTime,
	}

	// Marshal query to JSON format
	body, err := json.Marshal(newQuery)
	if err != nil {
		fmt.Println(err)
	}

	// Create a new query builder with the query body
	queryBuilder := parseable.NewQueryBuilder(string(body))

	// Query logs using the specified criteria
	resp, err := queryBuilder.QueryStream()
	if err != nil {
		fmt.Println(err)
	}

	// Print query response
	fmt.Println(resp)
}