Skip to content

Commit

Permalink
refactor: Using simpler method for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
daviderli614 committed Sep 26, 2024
2 parents a9fe67c + fa4d6c0 commit 9733b4b
Show file tree
Hide file tree
Showing 10 changed files with 631 additions and 42 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ docker run --rm -p 4000-4003:4000-4003 \
- [table](table/README.md)
- [object](object/README.md)
- [healthcheck](healthcheck/README.md)
- [opentelemetry](opentelemetry/README.md)
- [hints](hints/README.md)

## Query
Expand Down
1 change: 1 addition & 0 deletions examples/healthcheck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
_, err := client.HealthCheck(context.Background())
if err != nil {
log.Println("failed to health check:", err)
return
}
log.Println("the greptimedb is health")
}
95 changes: 54 additions & 41 deletions examples/hints/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,101 +21,114 @@ import (

greptime "github.com/GreptimeTeam/greptimedb-ingester-go"

"github.com/GreptimeTeam/greptimedb-ingester-go/pkg/hint"
"github.com/GreptimeTeam/greptimedb-ingester-go/pkg/hints"
"github.com/GreptimeTeam/greptimedb-ingester-go/table"
"github.com/GreptimeTeam/greptimedb-ingester-go/table/types"
)

const (
INSERT = 0
)

var (
client *greptime.Client
// The GreptimeDB address.
host = "127.0.0.1"

// The database name.
database = "public"
)

func init() {
cfg := greptime.NewConfig("127.0.0.1").WithDatabase("public")
type client struct {
client *greptime.Client
}

cli_, err := greptime.NewClient(cfg)
func newClient() (*client, error) {
cfg := greptime.NewConfig(host).WithDatabase(database)
gtClient, err := greptime.NewClient(cfg)
if err != nil {
log.Panic(err)
return nil, err
}

c := &client{
client: gtClient,
}
client = cli_

return c, nil
}

func initData() []*table.Table {
func initData() ([]*table.Table, error) {
time1 := time.Now()
time2 := time.Now()
time3 := time.Now()

itbl, err := table.New("monitor_table_with_hints")
if err != nil {
log.Println(err)
return nil
return nil, err
}
// add column at first. This is to define the schema of the table.
if err := itbl.AddTagColumn("id", types.INT64); err != nil {
log.Println(err)
return nil
return nil, err
}
if err := itbl.AddFieldColumn("host", types.STRING); err != nil {
log.Println(err)
return nil
return nil, err
}
if err := itbl.AddFieldColumn("temperature", types.FLOAT); err != nil {
log.Println(err)
return nil
return nil, err
}
if err := itbl.AddTimestampColumn("timestamp", types.TIMESTAMP_MICROSECOND); err != nil {
log.Println(err)
return nil
return nil, err
}

if err := itbl.AddRow(1, "hello", 1.1, time1); err != nil {
log.Println(err)
return nil
return nil, err
}
if err := itbl.AddRow(2, "hello", 2.2, time2); err != nil {
log.Println(err)
return nil
return nil, err
}
if err := itbl.AddRow(3, "hello", 3.3, time3); err != nil {
log.Println(err)
return nil
return nil, err
}

return []*table.Table{itbl}
return []*table.Table{itbl}, nil
}

func write(data *table.Table) {
var hints []hint.Hint
hints = append(hints,
hint.Hint{Key: "ttl", Value: "3d"},
hint.Hint{Key: "merge_mode", Value: "last_non_null"},
hint.Hint{Key: "append_mode", Value: "false"},
func (c client) write(data *table.Table) error {
var h []hints.Hint
h = append(h,
hints.Hint{Key: "ttl", Value: "3d"},
hints.Hint{Key: "merge_mode", Value: "last_non_null"},
hints.Hint{Key: "append_mode", Value: "false"},
)

ctx := hint.CreateContextWithHints(hints)
ctx := hints.CreateContextWithHints(h)
ctx, cancel := context.WithTimeout(ctx, time.Second*3)
defer cancel()

resp, err := client.Write(ctx, data)
resp, err := c.client.Write(ctx, data)
if err != nil {
log.Println(err)
return
return err
}
tableName, err := data.GetName()
if err != nil {
log.Println(err)
return
return err
}

log.Printf("create table, name: '%s'", tableName)
log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())
return nil
}

func main() {
data := initData()
data, err := initData()
if err != nil {
log.Fatalf("failed to init data: %v:", err)
}

c, err := newClient()
if err != nil {
log.Fatalf("failed to new client: %v:", err)
}

write(data[INSERT])
err = c.write(data[INSERT])
if err != nil {
log.Fatalf("failed to write data: %v:", err)
}
}
73 changes: 73 additions & 0 deletions examples/opentelemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Enable Opentelemetry

## Install Jaeger
Use [Jaeger](https://github.com/jaegertracing/jaeger) as the backend storage for tracing:
```
docker run --rm --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one
```

## Install Prometheus
Write the [Prometheus](https://prometheus.io/) configuration file and save it as **prometheus.yml**:
```
global:
scrape_interval: 10s
scrape_configs:
- job_name: greptimedb-ingester-go
static_configs:
- targets: ['host.docker.internal:2233']
```

Run prometheus:
```
docker run \
-p 9090:9090 \
-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
```


## Example
Enable opentelemetry in the configuration:
```go
cfg := greptime.NewConfig(host).WithDatabase(database).
WithTraceProvider(tracerProvider).WithTracesEnabled(true).
WithMeterProvider(meterProvider).WithMetricsEnabled(true)
```

Running the example:
```go
go run main.go
```

Output:

```log
2024/09/19 11:36:29 Waiting for connection...
2024/09/19 11:36:29 serving metrics at localhost:2233/metrics
2024/09/19 11:36:29 affected rows: 1
2024/09/19 11:36:29 Sleep 30s...
2024/09/19 11:36:39 Done!
```

## View metrics
We can use `curl localhost:2233/metrics` to view the metrics data. Also, Open the address http://localhost:9090/ to view metrics data.

![prometheus.png](../../images/prometheus.png)

## View tracing
Open the address http://localhost:16686/ to view tracing data.

![jaeger.png](../../images/jaeger.png)
42 changes: 42 additions & 0 deletions examples/opentelemetry/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module opentelemetry-example

go 1.22

toolchain go1.22.7

require (
github.com/GreptimeTeam/greptimedb-ingester-go v0.5.3-0.20240918162118-d3f1530a9887
github.com/prometheus/client_golang v1.20.4
go.opentelemetry.io/otel v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.30.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.30.0
go.opentelemetry.io/otel/exporters/prometheus v0.52.0
go.opentelemetry.io/otel/sdk v1.30.0
go.opentelemetry.io/otel/sdk/metric v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
google.golang.org/grpc v1.66.2
github.com/GreptimeTeam/greptime-proto v0.7.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.48.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
Loading

0 comments on commit 9733b4b

Please sign in to comment.