Skip to content

Enterprise-grade Go SDK for TelemetryFlow - the observability platform that provides unified metrics, logs, and traces collection following OpenTelemetry standards.

License

Notifications You must be signed in to change notification settings

telemetryflow/telemetryflow-go-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

41 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
TelemetryFlow Logo

TelemetryFlow GO SDK

Version License Go Version OTEL SDK OpenTelemetry Docker

Enterprise-grade Go SDK for TelemetryFlow - the observability platform that provides unified metrics, logs, and traces collection following OpenTelemetry standards.


🌟 Features

  • 100% OTLP Compliant: Full OpenTelemetry Protocol implementation
  • DDD Architecture: Domain-Driven Design with clear bounded contexts
  • CQRS Pattern: Separate command and query responsibilities
  • Easy Integration: Builder pattern and environment-based configuration
  • Exemplars Support: Metrics-to-traces correlation for powerful debugging
  • Service Graph Ready: Compatible with OTEL Collector servicegraph connector
  • Enhanced gRPC Settings: Configurable keepalive, buffer sizes, and message limits
  • TelemetryFlow Headers: Custom headers for collector authentication
  • Code Generators: CLI tools for quick project setup
    • telemetryflow-gen: SDK integration generator
    • telemetryflow-restapi: DDD + CQRS RESTful API generator
  • Docker Ready: Multi-stage Dockerfile and Docker Compose configurations
  • Production Ready: Comprehensive error handling, retries, and batching
  • Type Safe: Strong typing with compile-time safety
  • Zero Dependencies (core): Minimal external dependencies in core SDK
  • E2E Testing: Complete end-to-end testing infrastructure
  • Extensible: Easy to extend for custom use cases

πŸ“¦ Installation

go get github.com/telemetryflow/telemetryflow-go-sdk

πŸš€ Quick Start

1. Environment Variables Setup

Create a .env file:

TELEMETRYFLOW_API_KEY_ID=tfk_your_key_id_here
TELEMETRYFLOW_API_KEY_SECRET=tfs_your_secret_here
TELEMETRYFLOW_ENDPOINT=api.telemetryflow.id:4317
TELEMETRYFLOW_SERVICE_NAME=my-go-service
TELEMETRYFLOW_SERVICE_VERSION=1.1.1
TELEMETRYFLOW_SERVICE_NAMESPACE=telemetryflow
TELEMETRYFLOW_COLLECTOR_ID=my-collector-id
ENV=production

2. Initialize SDK

package main

import (
    "context"
    "log"

    "github.com/telemetryflow/telemetryflow-go-sdk/pkg/telemetryflow"
)

func main() {
    // Create client from environment variables
    client, err := telemetryflow.NewFromEnv()
    if err != nil {
        log.Fatal(err)
    }

    // Initialize the SDK
    ctx := context.Background()
    if err := client.Initialize(ctx); err != nil {
        log.Fatal(err)
    }
    defer client.Shutdown(ctx)

    // Your application code...
}

3. Send Telemetry

// Send a metric
client.IncrementCounter(ctx, "api.requests", 1, map[string]interface{}{
    "method": "GET",
    "status": 200,
})

// Send a log
client.LogInfo(ctx, "User logged in", map[string]interface{}{
    "user_id": "12345",
})

// Create a trace span
spanID, _ := client.StartSpan(ctx, "process-order", "internal", map[string]interface{}{
    "order_id": "67890",
})
defer client.EndSpan(ctx, spanID, nil)

πŸ—οΈ Architecture

The SDK follows Domain-Driven Design (DDD) and CQRS patterns:

pkg/telemetryflow/
β”œβ”€β”€ domain/          # Domain layer (entities, value objects)
β”‚   β”œβ”€β”€ credentials.go
β”‚   └── config.go
β”œβ”€β”€ application/     # Application layer (use cases, CQRS)
β”‚   β”œβ”€β”€ commands.go
β”‚   └── queries.go
β”œβ”€β”€ infrastructure/  # Infrastructure layer (OTLP exporters)
β”‚   β”œβ”€β”€ exporters.go
β”‚   └── handlers.go
└── client.go        # Public API (interface layer)

Domain Layer

Core business entities and value objects:

  • Credentials: Immutable API key pair with validation
  • TelemetryConfig: Aggregate root containing all configuration

Application Layer

CQRS implementation:

  • Commands: RecordMetricCommand, EmitLogCommand, StartSpanCommand, etc.
  • Queries: GetMetricQuery, GetLogsQuery, GetTraceQuery, etc.
  • Command/Query Buses: Route requests to appropriate handlers

Infrastructure Layer

Technical implementations:

  • OTLPExporterFactory: Creates OTLP exporters (gRPC/HTTP)
  • TelemetryCommandHandler: Handles telemetry commands
  • OpenTelemetry SDK integration

πŸ“š Usage Examples

Builder Pattern

client := telemetryflow.NewBuilder().
    WithAPIKey("tfk_...", "tfs_...").
    WithEndpoint("api.telemetryflow.id:4317").
    WithService("my-service", "1.0.0").
    WithEnvironment("production").
    WithGRPC().
    WithSignals(true, true, true). // metrics, logs, traces
    WithCustomAttribute("team", "backend").
    MustBuild()

Metrics

// Counter
client.IncrementCounter(ctx, "requests.total", 1, map[string]interface{}{
    "method": "POST",
    "endpoint": "/api/users",
})

// Gauge
client.RecordGauge(ctx, "memory.usage", 512.0, map[string]interface{}{
    "unit": "MB",
})

// Histogram
client.RecordHistogram(ctx, "request.duration", 0.25, "s", map[string]interface{}{
    "endpoint": "/api/orders",
})

Logs

// Info level
client.LogInfo(ctx, "Application started", map[string]interface{}{
    "version": "1.0.0",
    "port": 8080,
})

// Warning level
client.LogWarn(ctx, "High memory usage", map[string]interface{}{
    "usage_mb": 512,
    "threshold_mb": 400,
})

// Error level
client.LogError(ctx, "Database connection failed", map[string]interface{}{
    "error": "timeout",
    "host": "db.example.com",
})

Traces

// Start a span
spanID, err := client.StartSpan(ctx, "process-payment", "internal", map[string]interface{}{
    "payment_method": "credit_card",
    "amount": 99.99,
})

// Add events to span
client.AddSpanEvent(ctx, spanID, "validation.complete", map[string]interface{}{
    "valid": true,
})

// End span (with optional error)
client.EndSpan(ctx, spanID, nil)

πŸ› οΈ Code Generators

The SDK includes powerful code generators to bootstrap your integration.

SDK Generator (telemetryflow-gen)

Generates TelemetryFlow SDK integration code for existing projects.

# Install
go install github.com/telemetryflow/telemetryflow-go-sdk/cmd/generator@latest

# Initialize integration
telemetryflow-gen init \
    --project "my-app" \
    --service "my-service" \
    --key-id "tfk_..." \
    --key-secret "tfs_..."

# Generate examples
telemetryflow-gen example http-server
telemetryflow-gen example worker

RESTful API Generator (telemetryflow-restapi)

Generates complete DDD + CQRS RESTful API projects with Echo framework.

# Install
go install github.com/telemetryflow/telemetryflow-go-sdk/cmd/generator-restfulapi@latest

# Create new project
telemetryflow-restapi new \
    --name order-service \
    --module github.com/myorg/order-service \
    --db-driver postgres

# Add entities
telemetryflow-restapi entity \
    --name Order \
    --fields 'customer_id:uuid,total:decimal,status:string'

See Generator Documentation and RESTful API Generator for details.

🐳 Docker

Using Docker

# Run SDK generator
docker run --rm -v $(pwd):/workspace telemetryflow/telemetryflow-sdk:latest \
    init --project myapp --output /workspace

# Run RESTful API generator
docker run --rm -v $(pwd):/workspace telemetryflow/telemetryflow-sdk:latest \
    telemetryflow-restapi new --name myapi --output /workspace

Docker Compose

Development environment with full observability stack:

# Start development environment
docker-compose up -d

# Start with observability tools (Jaeger, Prometheus, Grafana)
docker-compose --profile full up -d

# Run E2E tests
docker-compose -f docker-compose.e2e.yml up --abort-on-container-exit

Generated Structure

your-project/
β”œβ”€β”€ telemetry/
β”‚   β”œβ”€β”€ init.go           # SDK initialization
β”‚   β”œβ”€β”€ metrics/
β”‚   β”‚   └── metrics.go    # Metrics helpers
β”‚   β”œβ”€β”€ logs/
β”‚   β”‚   └── logs.go       # Logging helpers
β”‚   β”œβ”€β”€ traces/
β”‚   β”‚   └── traces.go     # Tracing helpers
β”‚   └── README.md
β”œβ”€β”€ .env.telemetryflow    # Configuration template
└── example_*.go          # Generated examples

🎯 Advanced Configuration

Protocol Selection

// gRPC (default, recommended)
config.WithProtocol(domain.ProtocolGRPC)

// HTTP
config.WithProtocol(domain.ProtocolHTTP)

Retry Configuration

config.WithRetry(
    true,                    // enabled
    3,                       // max retries
    5 * time.Second,        // backoff duration
)

Batch Settings

config.WithBatchSettings(
    10 * time.Second,       // batch timeout
    512,                     // max batch size
)

Signal Control

// Enable specific signals only
config.WithSignals(
    true,   // metrics
    false,  // logs
    true,   // traces
)

// Or use convenience methods
config.WithMetricsOnly()
config.WithTracesOnly()

Exemplars Support (v1.1.1+)

Exemplars enable metrics-to-traces correlation for powerful debugging:

// Enabled by default, disable if not needed
client := telemetryflow.NewBuilder().
    WithAutoConfiguration().
    WithExemplars(false).  // Disable exemplars
    MustBuild()

Service Namespace (v1.1.1+)

// Set service namespace for multi-tenant environments
client := telemetryflow.NewBuilder().
    WithAutoConfiguration().
    WithServiceNamespace("my-namespace").
    MustBuild()

Collector ID (v1.1.1+)

// Set collector ID for TelemetryFlow backend authentication
client := telemetryflow.NewBuilder().
    WithAutoConfiguration().
    WithCollectorID("my-unique-collector-id").
    MustBuild()

Custom Resource Attributes

config.
    WithCustomAttribute("team", "backend").
    WithCustomAttribute("region", "us-east-1").
    WithCustomAttribute("datacenter", "dc1")

πŸ”’ Security

API Key Management

API keys should never be hardcoded. Use environment variables or secure secret management:

// βœ… Good: From environment
client, _ := telemetryflow.NewFromEnv()

// βœ… Good: From secret manager (example)
apiKey := secretManager.GetSecret("telemetryflow/api-key")
client, _ := telemetryflow.NewBuilder().
    WithAPIKey(apiKey.KeyID, apiKey.KeySecret).
    Build()

// ❌ Bad: Hardcoded
client, _ := telemetryflow.NewBuilder().
    WithAPIKey("tfk_hardcoded", "tfs_secret"). // DON'T DO THIS
    Build()

TLS/SSL

By default, the SDK uses secure connections. Only disable for testing:

// Production (default)
config.WithInsecure(false)

// Testing only
config.WithInsecure(true)

πŸ“Š Best Practices

  1. Initialize Once: Create one client instance per application
  2. Defer Shutdown: Always defer client.Shutdown(ctx)
  3. Context Propagation: Pass context through your call chain
  4. Attribute Cardinality: Limit high-cardinality attributes
  5. Batch Configuration: Tune batch settings for your workload
  6. Error Handling: Always check errors from telemetry calls
  7. Graceful Shutdown: Allow time for final flush
func main() {
    client, _ := telemetryflow.NewFromEnv()
    ctx := context.Background()

    // Initialize
    if err := client.Initialize(ctx); err != nil {
        log.Fatal(err)
    }

    // Ensure graceful shutdown
    defer func() {
        shutdownCtx, cancel := context.WithTimeout(
            context.Background(),
            10*time.Second,
        )
        defer cancel()

        client.Flush(shutdownCtx)
        client.Shutdown(shutdownCtx)
    }()

    // Application code...
}

πŸ§ͺ Testing

The SDK includes comprehensive tests organized by type:

tests/
β”œβ”€β”€ unit/              # Unit tests for individual components
β”‚   β”œβ”€β”€ domain/        # Credentials, Config tests
β”‚   β”œβ”€β”€ application/   # Command/Query tests
β”‚   β”œβ”€β”€ infrastructure/# Template, HTTP, Database tests
β”‚   └── client/        # Client, Builder tests
β”œβ”€β”€ integration/       # Cross-layer integration tests
β”œβ”€β”€ e2e/               # End-to-end pipeline tests
β”‚   └── testdata/      # E2E test fixtures (OTEL config, nginx)
β”œβ”€β”€ mocks/             # Mock implementations
└── fixtures/          # Test fixtures

Running Tests

# Run all tests
go test ./...

# Run unit tests only
go test ./tests/unit/...

# Run with verbose output
go test -v ./...

# Run with coverage
go test -cover ./...

# Generate coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

# Run short tests (skip integration)
go test -short ./...

# Run e2e tests (requires environment setup)
TELEMETRYFLOW_E2E=true go test ./tests/e2e/...

Test Coverage Goals

Layer Target
Domain 90%+
Application 85%+
Infrastructure 80%+
Client 85%+

πŸ“– Documentation

🀝 Contributing

Contributions are welcome! Please read our Contributing Guide for details.

πŸ“„ License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

πŸ†˜ Support

πŸ™ Acknowledgments

  • OpenTelemetry for the excellent instrumentation standard
  • All contributors who have helped shape this SDK

Built with ❀️ by the DevOpsCorner Indonesia

About

Enterprise-grade Go SDK for TelemetryFlow - the observability platform that provides unified metrics, logs, and traces collection following OpenTelemetry standards.

Topics

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published