Skip to content

Commit

Permalink
chore(go-sdk): sync go-sdk changes (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyjames authored Oct 24, 2024
2 parents 309cb3c + 7e8525c commit 5374fc6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 7 deletions.
15 changes: 15 additions & 0 deletions config/clients/go/CHANGELOG.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Changelog

## v0.6.3

### [0.6.3](https://github.com/openfga/go-sdk/compare/v0.6.2...v0.6.3) (2024-10-22)

- fix: fix metrics data race issues (#139)

## v0.6.2

### [0.6.2](https://github.com/openfga/go-sdk/compare/v0.6.1...v0.6.2) (2024-10-21)

- fix: fix batch check consistency (#131)
- fix: fix data race on TelemetryInstances (#136) - thanks @Kryvchun!

NOTE: `TelemetryInstances` in `telemetry.go` has been deprecated, as its usage is intended to be internal. It will be removed in a future release.

## v0.6.1

### [0.6.1](https://github.com/openfga/go-sdk/compare/v0.6.0...v0.6.1) (2024-09-23)
Expand Down
2 changes: 1 addition & 1 deletion config/clients/go/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"sdkId": "go",
"gitRepoId": "go-sdk",
"packageName": "openfga",
"packageVersion": "0.6.1",
"packageVersion": "0.6.3",
"packageDescription": "Go SDK for OpenFGA",
"packageDetailedDescription": "This is an autogenerated Go SDK for OpenFGA. It provides a wrapper around the [OpenFGA API definition](https://openfga.dev/api).",
"fossaComplianceNoticeId": "41c01c64-f74a-414a-9e39-7aeca87bc47b",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
fetch-depth: 0

Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
needs: [test]

steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
fetch-depth: 0

Expand Down
17 changes: 13 additions & 4 deletions config/clients/go/template/telemetry/metrics.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package telemetry
import (
"context"
"net/http"
"sync"
"time"

"go.opentelemetry.io/otel/metric"
)

type Metrics struct {
Meter metric.Meter
Counters map[string]metric.Int64Counter
Histograms map[string]metric.Float64Histogram
Configuration *MetricsConfiguration
Meter metric.Meter
countersLock sync.Mutex
Counters map[string]metric.Int64Counter
histogramsLock sync.Mutex
Histograms map[string]metric.Float64Histogram
Configuration *MetricsConfiguration
}

type MetricsInterface interface {
Expand All @@ -25,6 +28,9 @@ type MetricsInterface interface {
}

func (m *Metrics) GetCounter(name string, description string) (metric.Int64Counter, error) {
m.countersLock.Lock()
defer m.countersLock.Unlock()
if counter, exists := m.Counters[name]; exists {
return counter, nil
}
Expand All @@ -34,6 +40,9 @@ func (m *Metrics) GetCounter(name string, description string) (metric.Int64Count
}

func (m *Metrics) GetHistogram(name string, description string, unit string) (metric.Float64Histogram, error) {
m.histogramsLock.Lock()
defer m.histogramsLock.Unlock()
if histogram, exists := m.Histograms[name]; exists {
return histogram, nil
}
Expand Down
67 changes: 67 additions & 0 deletions config/clients/go/template/telemetry/metrics_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package telemetry

import (
"context"
"sync"
"testing"

"go.opentelemetry.io/otel/metric"
Expand Down Expand Up @@ -81,6 +82,41 @@ func TestGetCounter(t *testing.T) {
}
}

// Run this test with the "-race" flag.
//
// go test -race -run ^TestGetCounterRace github.com/openfga/go-sdk/telemetry
func TestGetCounterRace(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Counters: make(map[string]metric.Int64Counter),
}

t.Parallel()

var wg sync.WaitGroup

for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := metrics.GetCounter("test_counter", "A test counter")
if err != nil {
t.Errorf("Expected no error, but got %v", err)
return
}
Get(TelemetryFactoryParameters{})
}()
}

wg.Wait()
}

func TestGetHistogram(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
Expand Down Expand Up @@ -110,6 +146,37 @@ func TestGetHistogram(t *testing.T) {
}
}

// go test -race -run ^TestGetHistogramRace github.com/openfga/go-sdk/telemetry
func TestGetHistogramRace(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
histograms: make(map[string]metric.Float64Histogram),
}
metrics := &Metrics{
Meter: mockMeter,
Histograms: make(map[string]metric.Float64Histogram),
}

t.Parallel()

var wg sync.WaitGroup

for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, err := metrics.GetHistogram("test_histogram", "A test histogram", "ms")
if err != nil {
t.Errorf("Expected no error, but got %v", err)
return
}
}()
}

wg.Wait()
}

func TestCredentialsRequest(t *testing.T) {
mockMeter := &MockMeter{
counters: make(map[string]metric.Int64Counter),
Expand Down

0 comments on commit 5374fc6

Please sign in to comment.