launchdarkly / go-server-sdk

LaunchDarkly Server-side SDK for Go
Other
40 stars 17 forks source link

LD client doesn't correctly interpret file data sources #157

Open constant-mihai opened 2 weeks ago

constant-mihai commented 2 weeks ago

Is this a support request? No.

Describe the bug I am trying to use a file data source, but can't seem to manage to do so. I cannot be sure if there is some issue in my yaml file. I have tried multiple configurations, none seem to work.

To reproduce Run the following program:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time"

    "gopkg.in/launchdarkly/go-sdk-common.v2/lduser"
    sdk "gopkg.in/launchdarkly/go-server-sdk.v5"
    "gopkg.in/launchdarkly/go-server-sdk.v5/ldcomponents"
    "gopkg.in/launchdarkly/go-server-sdk.v5/ldfiledata"
    "gopkg.in/launchdarkly/go-server-sdk.v5/ldfilewatch"
)

func main() {
    sigs := make(chan os.Signal, 1)
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

    ldClient, _ := sdk.MakeCustomClient("sdk-key-123abc",
        sdk.Config{
            Events: ldcomponents.NoEvents(),
            DataSource: ldfiledata.DataSource().
                FilePaths("./ld.yaml").
                Reloader(ldfilewatch.WatchFiles),
        }, 0)

    // Not sure how MakeCustomClient works, so I'm putting a sleep here.
    time.Sleep(3 * time.Second)
    context := lduser.NewUserBuilder("user-key-1").Build()
    optIn, err := ldClient.BoolVariation("opt-in", context, false)
    if err != nil {
        fmt.Printf("%s\n", err.Error())
        return
    }
    fmt.Printf("opt-in: %v\n", optIn)
}

ld.yaml

flags:
  opt-in:
    key: opt-in
    on: true
    fallthrough:
      variation: 0
    variations:
      - true
      - false
    targets:
      - contextKind: user
        variation: 0
        values:
        - user-key-1
    rules:
      - variation: 0
        clauses:
          - contextKind: user
            attribute: key
            op: in
            values:
              - user-key-1

The program prints:

[LaunchDarkly] 2024/06/17 10:44:05 INFO: Starting LaunchDarkly client 5.10.1
[LaunchDarkly] 2024/06/17 10:44:05 INFO: FileDataSource: Reloading flag data after detecting a change
opt-in: false

Expected behavior Expected the program to print:

[LaunchDarkly] 2024/06/17 10:44:05 INFO: Starting LaunchDarkly client 5.10.1
[LaunchDarkly] 2024/06/17 10:44:05 INFO: FileDataSource: Reloading flag data after detecting a change
opt-in: true

Logs See above.

SDK version 5.10.1

Language version, developer tools go 1.22.1

OS/platform Ubuntu 22.04

Additional context N/A.

cwaldren-ld commented 2 weeks ago

Hi @constant-mihai , custom contexts are supported only in SDK versions >= 6.

The 5.10 version went end-of-life on December 7th of 2023, and is not supported by LaunchDarkly. Would you mind testing out with a newer version, and if it still doesn't work, I can assist?