Skip to content

Latest commit

 

History

History
 
 

go

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

OpenMeter Go SDK

Install

go get github.com/openmeterio/openmeter/api/client/go

Usage

Initialize client.

import (
  cloudevents "github.com/cloudevents/sdk-go/v2/event"
  om "github.com/openmeterio/openmeter/api/client/go"
)

func main() {
  // Initialize OpenMeter client
  om, err := openmeter.NewClientWithResponses("http://localhost:8888")
  if err != nil {
      panic(err.Error())
  }

  // Use OpenMeter client
  // ...
}

Ingest Event

Report usage to OpenMeter.

e := cloudevents.New()
e.SetID("00001")
e.SetSource("my-app")
e.SetType("tokens")
e.SetSubject("user-id")
e.SetTime(time.Now())
e.SetData("application/json", map[string]string{
  "tokens": "15",
  "model": "gpt-4",
})

_, err := client.IngestEventWithResponse(ctx, e)

Query Meter

Retreive usage from OpenMeter.

slug := "token-usage"
subject := []string{"user-id"}
from, _ := time.Parse(time.RFC3339, "2023-01-01T00:00:00Z")
to, _ := time.Parse(time.RFC3339, "2023-01-02T00:00:00Z")
resp, _ := client.QueryMeterWithResponse(ctx, slug, &om.QueryMeterParams{
    Subject: &subject,
    From:    &from,
    To:      &to,
})
// resp.JSON200.Data