How to interact with the Measurely API using the golang package.
Measurely-go is a lightweight library for interacting with the Measurely API, enabling developers to manage and track custom metrics programmatically using Golang.
To use the Measurely package in your Golang project, you need to import it and initialize it with your Measurely API key.
go get github.com/measurely-dev/measurely-go
Before you can send any metrics to Measurely, you need to initialize the package with your API key. The Init
function accepts your API key as a string and sets it for use in subsequent API calls.
package main
import (
"github.com/measurely-dev/measurely-go"
"fmt"
)
func main() {
// Initialize the Measurely package with your API key
measurely.Init("YOUR_API_KEY")
}
The Capture
function is used to send metric data to Measurely. You need to pass the metric identifier (which is a unique name or ID for the metric) and the metric value you want to track.
package main
import (
"github.com/measurely-dev/measurely-go"
"fmt"
)
func main() {
// Initialize the Measurely package with your API key
measurely.Init("YOUR_API_KEY")
// Create a metric payload with value and optional filters
payload := measurely.CapturePayload{
Value: 42,
Filters: map[string]string{
"environment": "production",
"region": "us-east",
},
}
// Capture the metric and get the result
result := measurely.Capture("example_metric", payload)
// Handle the result
if result.Success {
fmt.Println("Metric captured successfully!")
} else {
fmt.Println("Error capturing metric:", result.Message)
}
}
The Capture
function returns a CaptureResult
struct that contains two fields:
Success
(bool): Indicates if the API call was successful.Message
(string): Contains the response message from the server, which could either be a success message or an error message.Init(NEW_API_KEY string)
NEW_API_KEY
: The API key provided by Measurely.Capture(metric_identifier string, payload CapturePayload) CaptureResult
metric_identifier
: The unique identifier for the metric you are capturing.payload
: A CapturePayload
struct that contains the metric value to be recorded and optional filters.CaptureResult
struct that contains the success status and response message.CapturePayload
type CapturePayload struct {
Value int `json:"value"` // The metric value to be recorded.
Filters map[string]string `json:"filters"` // Optional filters for categorizing the metric.
}
Value
(int): The metric value that you want to track.Filters
(map[string]string): Optional key-value pairs for categorizing the metric (e.g., "environment": "production").CaptureResult
type CaptureResult struct {
Success bool // Indicates if the API call was successful.
Message string // Contains the server's response or an error message.
}
Success
(bool): Indicates if the metric capture was successful.Message
(string): Contains the server's response or an error message.Contributions are welcome! Please open an issue or submit a pull request to improve the library.
This library is licensed under the MIT License.