|
| 1 | +package cockpit |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "slices" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/cyclimse/mcp-scaleway-functions/internal/constants" |
| 12 | + cockpit "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1" |
| 13 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + tokenName = constants.ProjectName |
| 18 | + |
| 19 | + // queryTemplateServerless is the template used to query logs from Loki for Serverless Functions & Containers. |
| 20 | + // Because Serverless logs are sent as JSON, we only get the message field. |
| 21 | + queryTemplateServerless = `{resource_name="%s", resource_type="%s"} |~ "^{.*}$" | json | line_format "{{.message}}"` |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + ErrNoScalewayLogsDataSource = errors.New( |
| 26 | + "no Scaleway logs data source found; please wait a few minutes and try again", |
| 27 | + ) |
| 28 | + ErrTokenHasNoSecretKey = errors.New("token has no secret key") |
| 29 | +) |
| 30 | + |
| 31 | +type Log struct { |
| 32 | + Timestamp time.Time `json:"timestamp"` |
| 33 | + Message string `json:"message"` |
| 34 | +} |
| 35 | + |
| 36 | +type Client interface { |
| 37 | + ListFunctionLogs( |
| 38 | + ctx context.Context, |
| 39 | + resourceName string, |
| 40 | + start time.Time, |
| 41 | + end time.Time, |
| 42 | + ) ([]Log, error) |
| 43 | + // note(cyclimse): makes me think we should have a buildID in Scaleway Functions build logs |
| 44 | + // to link logs to a specific build. |
| 45 | + ListFunctionBuildLogs( |
| 46 | + ctx context.Context, |
| 47 | + resourceName string, |
| 48 | + start time.Time, |
| 49 | + end time.Time, |
| 50 | + ) ([]Log, error) |
| 51 | +} |
| 52 | + |
| 53 | +type client struct { |
| 54 | + cockpitAPI *cockpit.RegionalAPI |
| 55 | + projectID string |
| 56 | + |
| 57 | + initLokiClientOnce sync.Once |
| 58 | + lokiClient LokiClient |
| 59 | +} |
| 60 | + |
| 61 | +func NewClient(scwClient *scw.Client, projectID string) Client { |
| 62 | + return &client{ |
| 63 | + cockpitAPI: cockpit.NewRegionalAPI(scwClient), |
| 64 | + projectID: projectID, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// ListFunctionLogs implements Client. |
| 69 | +func (c *client) ListFunctionLogs( |
| 70 | + ctx context.Context, |
| 71 | + resourceName string, |
| 72 | + start time.Time, |
| 73 | + end time.Time, |
| 74 | +) ([]Log, error) { |
| 75 | + lokiClient, err := c.getLokiClient(ctx) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("getting Loki client: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + logs, err := lokiClient.Query( |
| 81 | + ctx, |
| 82 | + fmt.Sprintf(queryTemplateServerless, resourceName, "serverless_function"), |
| 83 | + start, |
| 84 | + end, |
| 85 | + ) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("querying logs: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + return logs, nil |
| 91 | +} |
| 92 | + |
| 93 | +// ListFunctionBuildLogs implements Client. |
| 94 | +func (*client) ListFunctionBuildLogs( |
| 95 | + _ context.Context, |
| 96 | + _ string, |
| 97 | + _ time.Time, |
| 98 | + _ time.Time, |
| 99 | +) ([]Log, error) { |
| 100 | + panic("unimplemented") |
| 101 | +} |
| 102 | + |
| 103 | +//nolint:nonamedreturns // actually like it this way. |
| 104 | +func (c *client) getLokiClient(ctx context.Context) (lokiClient LokiClient, err error) { |
| 105 | + c.initLokiClientOnce.Do(func() { |
| 106 | + var ( |
| 107 | + dataSource string |
| 108 | + token string |
| 109 | + ) |
| 110 | + |
| 111 | + dataSource, err = c.getScalewayLogsDataSourceURL(ctx) |
| 112 | + if err != nil { |
| 113 | + err = fmt.Errorf( |
| 114 | + "getting Scaleway logs data source for project %q: %w", |
| 115 | + c.projectID, |
| 116 | + err, |
| 117 | + ) |
| 118 | + |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + token, err = c.createToken(ctx) |
| 123 | + if err != nil { |
| 124 | + err = fmt.Errorf("creating token: %w", err) |
| 125 | + |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + c.lokiClient = NewLokiClient(dataSource, token) |
| 130 | + }) |
| 131 | + |
| 132 | + return c.lokiClient, err |
| 133 | +} |
| 134 | + |
| 135 | +func (c *client) createToken(ctx context.Context) (string, error) { |
| 136 | + resp, err := c.cockpitAPI.ListTokens(&cockpit.RegionalAPIListTokensRequest{ |
| 137 | + TokenScopes: []cockpit.TokenScope{cockpit.TokenScopeReadOnlyLogs}, |
| 138 | + ProjectID: c.projectID, |
| 139 | + }, scw.WithAllPages(), scw.WithContext(ctx)) |
| 140 | + if err != nil { |
| 141 | + return "", fmt.Errorf("listing tokens: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + i := slices.IndexFunc(resp.Tokens, func(t *cockpit.Token) bool { |
| 145 | + return t.Name == tokenName |
| 146 | + }) |
| 147 | + |
| 148 | + if i != -1 { |
| 149 | + // Unfortunately, the SecretKey is only shown once. So we're going to have to |
| 150 | + // delete and recreate it. |
| 151 | + err := c.cockpitAPI.DeleteToken(&cockpit.RegionalAPIDeleteTokenRequest{ |
| 152 | + TokenID: resp.Tokens[i].ID, |
| 153 | + }, scw.WithContext(ctx)) |
| 154 | + if err != nil { |
| 155 | + return "", fmt.Errorf("deleting existing token: %w", err) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + token, err := c.cockpitAPI.CreateToken(&cockpit.RegionalAPICreateTokenRequest{ |
| 160 | + Name: tokenName, |
| 161 | + TokenScopes: []cockpit.TokenScope{cockpit.TokenScopeReadOnlyLogs}, |
| 162 | + }, scw.WithContext(ctx)) |
| 163 | + if err != nil { |
| 164 | + return "", fmt.Errorf("creating token: %w", err) |
| 165 | + } |
| 166 | + |
| 167 | + if token.SecretKey == nil { |
| 168 | + return "", ErrTokenHasNoSecretKey |
| 169 | + } |
| 170 | + |
| 171 | + return *token.SecretKey, nil |
| 172 | +} |
| 173 | + |
| 174 | +func (c *client) getScalewayLogsDataSourceURL(ctx context.Context) (string, error) { |
| 175 | + resp, err := c.cockpitAPI.ListDataSources(&cockpit.RegionalAPIListDataSourcesRequest{ |
| 176 | + Origin: cockpit.DataSourceOriginScaleway, |
| 177 | + Types: []cockpit.DataSourceType{cockpit.DataSourceTypeLogs}, |
| 178 | + ProjectID: c.projectID, |
| 179 | + // There should be at most one such data source. |
| 180 | + Page: scw.Int32Ptr(1), |
| 181 | + PageSize: scw.Uint32Ptr(1), |
| 182 | + }, scw.WithContext(ctx)) |
| 183 | + if err != nil { |
| 184 | + return "", fmt.Errorf("listing data sources: %w", err) |
| 185 | + } |
| 186 | + |
| 187 | + if len(resp.DataSources) == 0 { |
| 188 | + return "", ErrNoScalewayLogsDataSource |
| 189 | + } |
| 190 | + |
| 191 | + dataSource := resp.DataSources[0] |
| 192 | + |
| 193 | + return dataSource.URL, nil |
| 194 | +} |
0 commit comments