Skip to content

Commit

Permalink
add dton service
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Dec 13, 2023
1 parent 3021764 commit 8f8a9c9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/prometheus/client_golang v1.17.0
github.com/tonkeeper/tonapi-go v0.0.2
github.com/tonkeeper/tongo v1.4.1
github.com/xssnick/tonutils-go v1.8.8
)

require (
Expand Down Expand Up @@ -35,7 +36,6 @@ require (
github.com/segmentio/asm v1.2.0 // indirect
github.com/sigurn/crc16 v0.0.0-20211026045750-20ab5afb07e3 // indirect
github.com/snksoft/crc v1.1.0 // indirect
github.com/xssnick/tonutils-go v1.8.8 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package main

import (
public_config "api_monitoring_stats/services/public-config"
"api_monitoring_stats/services/tonhub"
"fmt"
"log"
"net/http"
"time"

"api_monitoring_stats/config"
"api_monitoring_stats/services/dton"
public_config "api_monitoring_stats/services/public-config"
"api_monitoring_stats/services/tonapi"
"api_monitoring_stats/services/toncenter"

"api_monitoring_stats/config"
"api_monitoring_stats/services/tonhub"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

Expand All @@ -29,6 +29,7 @@ func main() {
}()
sources := []metrics{
tonapi.NewMonitoring(),
dton.NewMonitoring("dton", "https://dton.io/graphql"),
toncenter.NewV2Monitoring("toncenter.com v2", "https://toncenter.com/api/v2"),
toncenter.NewV3Monitoring("toncenter.com v3", "https://toncenter.com/api/v3"),
toncenter.NewV2Monitoring("orbs http-api", "https://ton.access.orbs.network/route/1/mainnet/toncenter-api-v2"),
Expand Down
102 changes: 102 additions & 0 deletions services/dton/graphql.go
Original file line number Diff line number Diff line change
@@ -1 +1,103 @@
package dton

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"api_monitoring_stats/config"
"api_monitoring_stats/services"
)

type Monitoring struct {
name string
prefix string
}

type graphQLQuery struct {
Query string `json:"query"`
}

func NewMonitoring(name, prefix string) *Monitoring {
return &Monitoring{
name: name,
prefix: prefix,
}
}

func (m *Monitoring) GetMetrics(ctx context.Context) services.ApiMetrics {
metrics := services.ApiMetrics{
ServiceName: m.prefix,
}
start := time.Now()
metrics.TotalChecks++
query := fmt.Sprintf(`{account_states(account: {address_friendly: "%v"}) {account_storage_balance_grams}}`, config.ElectorAccountID.ToHuman(true, false))
body, _ := json.Marshal(graphQLQuery{Query: query})
if _, err := sendRequest(m.prefix, body); err != nil {
metrics.Errors = append(metrics.Errors, fmt.Errorf("failed to get account state: %w", err))
} else {
metrics.SuccessChecks++
}
metrics.HttpsLatency = time.Since(start).Seconds()

metrics.TotalChecks++
query = fmt.Sprintf(`{transactions(order_desc: true, page_size: 1, address_friendly: "%v"){gen_utime}}`, config.ElectorAccountID.ToHuman(true, false))
body, _ = json.Marshal(graphQLQuery{Query: query})
responseBody, err := sendRequest(m.prefix, body)
if err != nil {
metrics.Errors = append(metrics.Errors, fmt.Errorf("failed to get account transactions: %w", err))
return metrics
}
var result struct {
Data struct {
Transactions []struct {
GenUtime string `json:"gen_utime"`
} `json:"transactions"`
} `json:"data"`
}
if err = json.Unmarshal(responseBody, &result); err != nil {
metrics.Errors = append(metrics.Errors, fmt.Errorf("failed to decode response body: %w", err))
return metrics
}
if len(result.Data.Transactions) == 0 {
metrics.Errors = append(metrics.Errors, fmt.Errorf("no transactions found"))
return metrics
}
metrics.SuccessChecks++

moscowLocation, _ := time.LoadLocation("Europe/Moscow")
parsedTime, err := time.ParseInLocation("2006-01-02T15:04:05", result.Data.Transactions[0].GenUtime, moscowLocation)
if err != nil {
metrics.Errors = append(metrics.Errors, fmt.Errorf("failed to parse time"))
return metrics
}
metrics.IndexingLatency = float64(time.Now().Unix() - parsedTime.Unix())
return metrics
}

func sendRequest(url string, body []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

respBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("")
}

return respBody, nil
}

0 comments on commit 8f8a9c9

Please sign in to comment.