TelemetryFlow GO SDK
6.6K
Enterprise-grade Go SDK for TelemetryFlowโ - the observability platform that provides unified metrics, logs, and traces collection following OpenTelemetry standards.
telemetryflow-gen: SDK integration generatortelemetryflow-restapi: DDD + CQRS RESTful API generatorgo get github.com/telemetryflow/telemetryflow-go-sdk
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
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...
}
// 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)
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)
Core business entities and value objects:
Credentials: Immutable API key pair with validationTelemetryConfig: Aggregate root containing all configurationCQRS implementation:
RecordMetricCommand, EmitLogCommand, StartSpanCommand, etc.GetMetricQuery, GetLogsQuery, GetTraceQuery, etc.Technical implementations:
OTLPExporterFactory: Creates OTLP exporters (gRPC/HTTP)TelemetryCommandHandler: Handles telemetry commandsclient := 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()
// 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",
})
// 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",
})
// 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)
The SDK includes powerful code generators to bootstrap your integration.
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
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.
# 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
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
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
// gRPC (default, recommended)
config.WithProtocol(domain.ProtocolGRPC)
// HTTP
config.WithProtocol(domain.ProtocolHTTP)
config.WithRetry(
true, // enabled
3, // max retries
5 * time.Second, // backoff duration
)
config.WithBatchSettings(
10 * time.Second, // batch timeout
512, // max batch size
)
// Enable specific signals only
config.WithSignals(
true, // metrics
false, // logs
true, // traces
)
// Or use convenience methods
config.WithMetricsOnly()
config.WithTracesOnly()
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()
// Set service namespace for multi-tenant environments
client := telemetryflow.NewBuilder().
WithAutoConfiguration().
WithServiceNamespace("my-namespace").
MustBuild()
// Set collector ID for TelemetryFlow backend authentication
client := telemetryflow.NewBuilder().
WithAutoConfiguration().
WithCollectorID("my-unique-collector-id").
MustBuild()
config.
WithCustomAttribute("team", "backend").
WithCustomAttribute("region", "us-east-1").
WithCustomAttribute("datacenter", "dc1")
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()
By default, the SDK uses secure connections. Only disable for testing:
// Production (default)
config.WithInsecure(false)
// Testing only
config.WithInsecure(true)
client.Shutdown(ctx)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...
}
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
# 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/...
| Layer | Target |
|---|---|
| Domain | 90%+ |
| Application | 85%+ |
| Infrastructure | 80%+ |
| Client | 85%+ |
Contributions are welcome! Please read our Contributing Guideโ for details.
This project is licensed under the Apache License 2.0 - see the LICENSEโ file for details.
Built with โค๏ธ by the DevOpsCorner Indonesia
Content type
Image
Digest
sha256:bbeca302aโฆ
Size
7.3 MB
Last updated
3 months ago
docker pull telemetryflow/telemetryflow-go-sdk