Skip to content

Commit

Permalink
correctly pass loki tenant id to app from config (#76)
Browse files Browse the repository at this point in the history
* correctly pass loki tenant id to app from config

Signed-off-by: Annanay Agarwal <annanay.agarwal@grafana.com>

* swap out mariadb image for mysql:8.0

Signed-off-by: Annanay Agarwal <annanay.agarwal@grafana.com>

* fix flaky test

Signed-off-by: Annanay Agarwal <annanay.agarwal@grafana.com>

---------

Signed-off-by: Annanay Agarwal <annanay.agarwal@grafana.com>
  • Loading branch information
annanay25 authored Jul 27, 2024
1 parent ee9e425 commit 12e001d
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ai-training-api/air.ai-training-api.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tmp_dir = "tmp"
# Just plain old shell command. You could use `make` as well.
cmd = "BUILD_VERSION=$(cat .git_version) BUILD_COMMIT=$(cat .git_commit) BUILD_BRANCH=$(cat .git_branch) make exe"
# Customize binary.
full_bin = "./dist/ai-training-api --log.level=debug --const-tenant=0 --database-type=mysql --database-address='user:pass@tcp(db:3306)/aitraining?charset=utf8mb4&parseTime=True&loc=Local'"
full_bin = "./dist/ai-training-api --log.level=debug --const-tenant=0 --database-type=mysql --database-address='root:rootpass@tcp(db:3306)/aitraining?charset=utf8mb4&parseTime=True&loc=Local'"
# Watch these filename extensions.
include_ext = ["go", "tpl", "tmpl", "html"]
# Ignore these filename extensions or directories.
Expand Down
4 changes: 2 additions & 2 deletions ai-training-api/app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ func (a *App) addModelMetrics(tenantID string, req *http.Request) (interface{},
// TODO: Integrate with GCom API to find the corresponding Loki TenantID associated
// with the tenantID.

// For now, we can just forward the request body as is, to the Loki endpoint.
// Read the request body.
body, err := io.ReadAll(req.Body)
if err != nil {
return nil, middleware.ErrBadRequest(err)
Expand All @@ -424,13 +422,15 @@ func (a *App) addModelMetrics(tenantID string, req *http.Request) (interface{},
}
lokiResp, err := httpClient.Do(lokiReq)
if err != nil {
level.Error(a.logger).Log("msg", "error forwarding model-metrics to Loki", "err", err)
return nil, middleware.ErrBadRequest(err)
}
defer lokiResp.Body.Close()

// Read the response body.
lokiRespBody, err := io.ReadAll(lokiResp.Body)
if err != nil {
level.Error(a.logger).Log("msg", "error reading response body from Loki", "err", err)
return nil, middleware.ErrBadRequest(err)
}

Expand Down
10 changes: 2 additions & 8 deletions ai-training-api/app/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,8 @@ func TestAppCreatesNewProcess(t *testing.T) {
gpr := read[getProcessResponse](t, resp)
assert.Equal(t, cpr.Data.ID, gpr.Data.ID)
assert.Len(t, gpr.Data.Metadata, 2)
assert.Equal(t, "key1", gpr.Data.Metadata[0].Key)
assert.Equal(t, "string", gpr.Data.Metadata[0].Type)
assert.Equal(t, "value1", string(gpr.Data.Metadata[0].Value))
assert.Equal(t, "key2", gpr.Data.Metadata[1].Key)
assert.Equal(t, "int", gpr.Data.Metadata[1].Type)
value, err := model.UnmarshalMetadataValue(gpr.Data.Metadata[1].Value, gpr.Data.Metadata[1].Type)
require.NoError(t, err)
assert.Equal(t, 2, value)
assert.Contains(t, gpr.Data.Metadata, model.MetadataKV{TenantID: "0", Key: "key1", Type: "string", Value: []byte("value1"), ProcessID: cpr.Data.ID})
assert.Contains(t, gpr.Data.Metadata, model.MetadataKV{TenantID: "0", Key: "key2", Type: "int", Value: []byte("2"), ProcessID: cpr.Data.ID})
}

func TestAppCreatesNewProcessAndGroup(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions ai-training-api/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func New(
_db: db,
server: s,
lokiAddress: lokiAddress,
lokiTenant: lokiTenant,
logger: logger,
}

Expand Down
7 changes: 2 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ services:
- "9009:9009"

db:
image: mariadb:10.3
image: mysql:8.0
environment:
MARIADB_ROOT_PASSWORD: rootpass
MARIADB_DATABASE: aitraining
MARIADB_USER: user
MARIADB_PASSWORD: pass
MYSQL_ROOT_PASSWORD: rootpass
volumes:
- ./docker-compose/0-createdb.sql:/docker-entrypoint-initdb.d/0-createdb.sql

Expand Down
3 changes: 1 addition & 2 deletions docker-compose/0-createdb.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
CREATE DATABASE IF NOT EXISTS aitraining;
GRANT ALL ON aitraining.* TO user;
CREATE DATABASE IF NOT EXISTS aitraining;

0 comments on commit 12e001d

Please sign in to comment.