InfluxCommunity / influxdb3-go

The go package that provides a simple and convenient way to interact with InfluxDB 3.
https://pkg.go.dev/github.com/InfluxCommunity/influxdb3-go
MIT License
21 stars 11 forks source link

Url to instantiate v2 client vs sql not the same #20

Closed Anaisdg closed 1 year ago

Anaisdg commented 1 year ago

Specifications

InfluxCloud 3.0

Code sample to reproduce problem

If my URL is --build-arg INFLUXDB_URL=us-east-1-1.aws.cloud2.influxdata.com:443 Then the following script works

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/InfluxCommunity/influxdb3-go/influx"
)

type Value map[string]interface{}

var lines []string // Array to store lines in influx line protocol format

func main() {
    // Use env variables to initialize client
    url := os.Getenv("INFLUXDB_URL")
    token := os.Getenv("INFLUXDB_TOKEN")
    database := os.Getenv("INFLUXDB_DATABASE")
    fmt.Printf("url", url)

    // Create a new client using an InfluxDB server base URL and an authentication token
    client, err := influx.New(influx.Configs{
        HostURL:   url,
        AuthToken: token,
    })

    if err != nil {
        panic(err)
    }
    // Close client at the end and escalate error if present
    defer func(client *influx.Client) {
        err := client.Close()
        if err != nil {
            panic(err)
        }
    }(client)

    // Prepare FlightSQL query
        query := `
        SELECT *
        FROM "cpu"
      `

        iterator, err := client.Query(context.Background(), database, query)

        if err != nil {
            panic(err)
        }

        // Specify your configuration here
        measurement := "cpu_test"
        timestamp := "time"
        tags := []string{"host", "cpu"}
        fields := []string{"usage_user"}

        for iterator.Next() {
            value := iterator.Value()

            // Collect tag set
            var tagSet []string
            for _, tag := range tags {
                tagSet = append(tagSet, fmt.Sprintf("%s=%v", tag, value[tag]))
            }

            // Collect field set
            var fieldSet []string
            for _, field := range fields {
                fieldSet = append(fieldSet, fmt.Sprintf("%s=%v", field, value[field]))
            }

            // Here we convert each value into InfluxDB line protocol format.
            line := fmt.Sprintf("%s,%s %s %v",
                measurement,
                strings.Join(tagSet, ","),
                strings.Join(fieldSet, ","),
                value[timestamp])

            fmt.Println(line)
            lines = append(lines, line) // Append the line to lines array
        }

However I can't perform:

    line := fmt.Sprintf("stat,unit=temperature avg=%f,max=%f", 23.5, 45.0)
    err = client.Write(context.Background(), database, []byte(line))
    if err != nil {
        panic(err)
    }

I get the following error: panic: error calling us-east-1-1.aws.cloud2.influxdata.com:///api/v2/write?bucket=demo&org=&precision=ns: Post "us-east-1-1.aws.cloud2.influxdata.com:///api/v2/write?bucket=demo&org=&precision=ns": unsupported protocol scheme "us-east-1-1.aws.cloud2.influxdata.com" I need to change the url to https://us-east-1-1.aws.cloud2.influxdata.com for writes. It would be great if on url could be provided. Thank you!

Expected behavior

Expect to be able to provide a single url: us-east-1-1.aws.cloud2.influxdata.com

Actual behavior

The write and query method require different urls. Query requires: us-east-1-1.aws.cloud2.influxdata.com:443 Write requires: https://us-east-1-1.aws.cloud2.influxdata.com

Additional info

Thank you!!!

bednar commented 1 year ago

@Sciator, update the client to support URL without schema => assumes the endpoint is https.

@Anaisdg as a workaround you can use "https://us-east-1-1.aws.cloud2.influxdata.com" for query also for writes.

Anaisdg commented 1 year ago

Thank you!