forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure_helpers.go
More file actions
121 lines (96 loc) · 3.12 KB
/
azure_helpers.go
File metadata and controls
121 lines (96 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package azure
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/grafana/tempo/tempodb/backend/instrumentation"
"github.com/Azure/azure-pipeline-go/pipeline"
blob "github.com/Azure/azure-storage-blob-go/azblob"
"github.com/cristalhq/hedgedhttp"
)
const (
maxRetries = 1
)
func GetContainerURL(ctx context.Context, cfg *Config, hedge bool) (blob.ContainerURL, error) {
accountName := cfg.StorageAccountName.String()
accountKey := cfg.StorageAccountKey.String()
if accountName == "" {
accountName = os.Getenv("AZURE_STORAGE_ACCOUNT")
}
if accountKey == "" {
accountKey = os.Getenv("AZURE_STORAGE_KEY")
}
c, err := blob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
return blob.ContainerURL{}, err
}
retryOptions := blob.RetryOptions{
MaxTries: int32(maxRetries),
Policy: blob.RetryPolicyExponential,
}
if deadline, ok := ctx.Deadline(); ok {
retryOptions.TryTimeout = time.Until(deadline)
}
customTransport := http.DefaultTransport.(*http.Transport).Clone()
// add instrumentation
transport := instrumentation.NewAzureTransport(customTransport)
var stats *hedgedhttp.Stats
// hedge if desired (0 means disabled)
if hedge && cfg.HedgeRequestsAt != 0 {
transport, stats = hedgedhttp.NewRoundTripperAndStats(cfg.HedgeRequestsAt, cfg.HedgeRequestsUpTo, transport)
instrumentation.PublishHedgedMetrics(stats)
}
client := http.Client{Transport: transport}
httpSender := pipeline.FactoryFunc(func(next pipeline.Policy, po *pipeline.PolicyOptions) pipeline.PolicyFunc {
return func(ctx context.Context, request pipeline.Request) (pipeline.Response, error) {
// Send the request over the network
resp, err := client.Do(request.WithContext(ctx))
return pipeline.NewHTTPResponse(resp), err
}
})
p := blob.NewPipeline(c, blob.PipelineOptions{
Retry: retryOptions,
Telemetry: blob.TelemetryOptions{Value: "Tempo"},
HTTPSender: httpSender,
})
u, err := url.Parse(fmt.Sprintf("https://%s.%s", accountName, cfg.Endpoint))
// If the endpoint doesn't start with blob.core we can assume Azurite is being used
// So the endpoint should follow Azurite URL style
if !strings.HasPrefix(cfg.Endpoint, "blob.core") {
u, err = url.Parse(fmt.Sprintf("http://%s/%s", cfg.Endpoint, accountName))
}
if err != nil {
return blob.ContainerURL{}, err
}
service := blob.NewServiceURL(*u, p)
return service.NewContainerURL(cfg.ContainerName), nil
}
func GetContainer(ctx context.Context, conf *Config, hedge bool) (blob.ContainerURL, error) {
c, err := GetContainerURL(ctx, conf, hedge)
if err != nil {
return blob.ContainerURL{}, err
}
return c, nil
}
func GetBlobURL(ctx context.Context, conf *Config, blobName string) (blob.BlockBlobURL, error) {
c, err := GetContainerURL(ctx, conf, false)
if err != nil {
return blob.BlockBlobURL{}, err
}
return c.NewBlockBlobURL(blobName), nil
}
func CreateContainer(ctx context.Context, conf *Config) (blob.ContainerURL, error) {
c, err := GetContainerURL(ctx, conf, false)
if err != nil {
return blob.ContainerURL{}, err
}
_, err = c.Create(
ctx,
blob.Metadata{},
blob.PublicAccessNone)
return c, err
}