Skip to content

Commit 1e29a94

Browse files
authored
feat:Cosmos instrumentation (#515)
Instrumented Azure Cosmos DB SDK
1 parent 2f863ba commit 1e29a94

24 files changed

+2492
-17
lines changed

.circleci/config.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,21 @@ workflows:
106106
- build:
107107
name: "go1.17"
108108
image: "cimg/go:1.17"
109-
exclude_dirs: "./internal/bin/sql ./example/gin ./example/sql-redis ./example/gorm-postgres ./example/gorm-sqlite ./example/grpc-client-server ./instrumentation/cloud.google.com/go ./instrumentation/instabeego ./example/beego ./instrumentation/instaawsv2 ./instrumentation/instaecho ./instrumentation/instagorm ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase"
109+
exclude_dirs: "./internal/bin/sql ./example/gin ./example/sql-redis ./example/gorm-postgres ./example/gorm-sqlite ./example/grpc-client-server ./instrumentation/cloud.google.com/go ./instrumentation/instabeego ./example/beego ./instrumentation/instaawsv2 ./instrumentation/instaecho ./instrumentation/instagorm ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./instrumentation/instacosmos ./example/cosmos"
110110
- build:
111111
name: "go1.16"
112112
image: "cimg/go:1.16"
113-
exclude_dirs: "./internal/bin/sql ./example/gin ./example/sql-redis ./example/grpc-client-server ./instrumentation/instafiber ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/cloud.google.com/go ./instrumentation/instabeego ./example/beego ./instrumentation/instaawsv2 ./instrumentation/instapgx ./instrumentation/instaecho ./instrumentation/instagorm ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama"
113+
exclude_dirs: "./internal/bin/sql ./example/gin ./example/sql-redis ./example/grpc-client-server ./instrumentation/instafiber ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/cloud.google.com/go ./instrumentation/instabeego ./example/beego ./instrumentation/instaawsv2 ./instrumentation/instapgx ./instrumentation/instaecho ./instrumentation/instagorm ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama ./instrumentation/instacosmos ./example/cosmos"
114114
- build:
115115
name: "go1.15"
116116
image: "cimg/go:1.15"
117-
exclude_dirs: "./instrumentation/instaredigo ./internal/bin/sql ./example/gin ./example/sql-redis ./instrumentation/instagorm ./example/grpc-client-server ./instrumentation/instafiber ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/cloud.google.com/go ./instrumentation/instabeego ./example/beego ./instrumentation/instaawsv2 ./instrumentation/instapgx ./instrumentation/instaecho ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama"
117+
exclude_dirs: "./instrumentation/instaredigo ./internal/bin/sql ./example/gin ./example/sql-redis ./instrumentation/instagorm ./example/grpc-client-server ./instrumentation/instafiber ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/cloud.google.com/go ./instrumentation/instabeego ./example/beego ./instrumentation/instaawsv2 ./instrumentation/instapgx ./instrumentation/instaecho ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama ./instrumentation/instacosmos ./example/cosmos"
118118
- build:
119119
name: "go1.14"
120120
image: "cimg/go:1.14"
121-
exclude_dirs: "./instrumentation/instaecho ./instrumentation/instaredigo ./internal/bin/sql ./instrumentation/cloud.google.com/go ./example/gin ./instrumentation/instagorm ./example/grpc-client-server ./instrumentation/instafiber ./instrumentation/instaawsv2 ./example/sql-redis ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/instabeego ./example/beego ./instrumentation/instapgx ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama"
121+
exclude_dirs: "./instrumentation/instaecho ./instrumentation/instaredigo ./internal/bin/sql ./instrumentation/cloud.google.com/go ./example/gin ./instrumentation/instagorm ./example/grpc-client-server ./instrumentation/instafiber ./instrumentation/instaawsv2 ./example/sql-redis ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/instabeego ./example/beego ./instrumentation/instapgx ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama ./instrumentation/instacosmos ./example/cosmos"
122122
- build:
123123
name: "go1.13"
124124
image: "cimg/go:1.13"
125-
exclude_dirs: "./instrumentation/instaecho ./instrumentation/instaredigo ./internal/bin/sql ./instrumentation/cloud.google.com/go ./example/gin ./instrumentation/instagorm ./example/grpc-client-server ./instrumentation/instafiber ./instrumentation/instaawsv2 ./example/sql-redis ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/instabeego ./example/beego ./instrumentation/instapgx ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama"
125+
exclude_dirs: "./instrumentation/instaecho ./instrumentation/instaredigo ./internal/bin/sql ./instrumentation/cloud.google.com/go ./example/gin ./instrumentation/instagorm ./example/grpc-client-server ./instrumentation/instafiber ./instrumentation/instaawsv2 ./example/sql-redis ./example/gorm-postgres ./example/gorm-sqlite ./instrumentation/instasarama ./instrumentation/instasarama/example ./example/kafka-producer-consumer ./instrumentation/instabeego ./example/beego ./instrumentation/instapgx ./instrumentation/instaamqp ./instrumentation/instaawssdk ./instrumentation/instagocb ./example/couchbase ./example/sarama ./instrumentation/instacosmos ./example/cosmos"
126126

.github/workflows/coverage_run_and_upload.yml

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ jobs:
99
build:
1010
name: Code Coverage
1111
runs-on: ubuntu-latest
12+
env:
13+
COSMOS_CONNECTION_URL: ${{ secrets.COSMOS_CONNECTION_URL }}
14+
COSMOS_KEY: ${{ secrets.COSMOS_KEY }}
1215
steps:
1316
- uses: actions/checkout@v3
1417
with:
@@ -39,15 +42,15 @@ jobs:
3942
do echo "Generating test coverage for $lib" && cd "$lib" && go mod tidy && go test -v -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.out ./... -json > $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.json && cd -;
4043
done
4144

42-
echo "Generating test coverage for ./instrumentation/instagocb"
43-
cd ./instrumentation/instagocb
44-
go mod tidy
45-
go test -v -tags=integration -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.out ./... -json > $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.json && cd -
45+
INTEGRATIONS_TESTS=("instagocb" "instapgx" "instacosmos")
4646

47-
echo "Generating test coverage for ./instrumentation/instapgx"
48-
cd ./instrumentation/instapgx
49-
go mod tidy
50-
go test -v -tags=integration -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.out ./... -json > $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.json && cd -
47+
for str in ${INTEGRATIONS_TESTS[@]}; do
48+
dir=./instrumentation/$str
49+
echo "Generating test coverage for $dir"
50+
cd $dir
51+
go mod tidy
52+
go test -v -tags=integration -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.out ./... -json > $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.json && cd -
53+
done
5154

5255
- name: Upload code coverage
5356
uses: actions/upload-artifact@v3

Makefile

+20-3
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ endif
1818

1919
INSTAPGX_EXCLUDED := $(findstring ./instrumentation/instapgx, $(EXCLUDE_DIRS))
2020
INSTAGOCB_EXCLUDED := $(findstring ./instrumentation/instagocb, $(EXCLUDE_DIRS))
21+
INSTACOSMOS_EXCLUDED := $(findstring ./instrumentation/instacosmos, $(EXCLUDE_DIRS))
2122
integration: $(INTEGRATION_TESTS)
2223
ifndef INSTAPGX_EXCLUDED
2324
cd instrumentation/instapgx && go test -tags=integration
2425
endif
2526
ifndef INSTAGOCB_EXCLUDED
2627
cd instrumentation/instagocb && go test -v -coverprofile cover.out -tags=integration ./...
2728
endif
29+
ifndef INSTACOSMOS_EXCLUDED
30+
cd instrumentation/instacosmos && go test -v -coverprofile cover.out -tags=integration ./...
31+
endif
2832

2933
$(INTEGRATION_TESTS):
30-
go test $(GOFLAGS) -tags "$@ integration" $(shell grep --exclude-dir=instagocb --exclude-dir=instapgx -lR '^// +build \($@,\)\?integration\(,$@\)\?' .)
34+
go test $(GOFLAGS) -tags "$@ integration" $(shell grep --exclude-dir=instagocb --exclude-dir=instapgx --exclude-dir=instacosmos -lR '^// +build \($@,\)\?integration\(,$@\)\?' .)
3135

3236
$(LINTER):
3337
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/a2bc9b7a99e3280805309d71036e8c2106853250/install.sh \
@@ -49,8 +53,21 @@ instrumentation/% :
4953
printf '// (c) Copyright IBM Corp. %s\n// (c) Copyright Instana Inc. %s\n\npackage %s\n\nconst Version = "0.0.0"\n' $(shell date +%Y) $(shell date +%Y) $(notdir $@) > $@/version.go
5054

5155
fmtcheck:
52-
@gofmt -l .
53-
@test -z $(shell gofmt -l . && exit 1)
56+
@exclude_string=""; \
57+
for exclude_dir in $$(echo $$EXCLUDE_DIRS | tr ' ' '\n'); do \
58+
exclude_string="$$exclude_string -not -path \"$$exclude_dir/*\""; \
59+
done; \
60+
command="find . -type f -name \"*.go\" $$exclude_string"; \
61+
gofmt_output=$$(gofmt -l $$(eval "$$command")); \
62+
if [ -n "$$gofmt_output" ]; then \
63+
echo "Some files are not formatted properly:"; \
64+
echo "$$gofmt_output"; \
65+
exit 1; \
66+
else \
67+
echo "All Go files are formatted properly."; \
68+
fi
69+
@gofmt -l $$(eval "$$command")
70+
@test -z $(shell gofmt -l $$(eval "$$command") && exit 1)
5471

5572
importcheck:
5673
@test -z $(shell goimports -l . && exit 1)

example/cosmos/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Example Usage of instacosmos
2+
3+
### Prerequisites
4+
5+
* Create a cosmos database named "trace-data" in your Azure Cosmos DB account.
6+
* export the endpoint and key as environment variable
7+
```sh
8+
export COSMOS_CONNECTION_URL="<endpoint>"
9+
export COSMOS_KEY="<key>"
10+
```
11+
12+
# setup cosmos example
13+
```sh
14+
cd ./examples/cosmos
15+
go mod tidy
16+
go run .
17+
```
18+
19+
### Test the application
20+
```sh
21+
# Test success
22+
curl http://localhost:9990/cosmos-test
23+
24+
# Test error
25+
curl http://localhost:9990/cosmos-test?error=true
26+
```

example/cosmos/go.mod

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module cosmos.example
2+
3+
go 1.18
4+
5+
require (
6+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0
7+
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6
8+
github.com/google/uuid v1.4.0
9+
github.com/instana/go-sensor v1.58.0
10+
github.com/instana/go-sensor/instrumentation/instacosmos v1.0.0
11+
)
12+
13+
require (
14+
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible // indirect
15+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.0 // indirect
16+
github.com/looplab/fsm v1.0.1 // indirect
17+
github.com/opentracing/opentracing-go v1.2.0 // indirect
18+
golang.org/x/net v0.17.0 // indirect
19+
golang.org/x/text v0.13.0 // indirect
20+
)
21+
22+
replace (
23+
github.com/instana/go-sensor => ../../
24+
github.com/instana/go-sensor/instrumentation/instacosmos v1.0.0 => ../../instrumentation/instacosmos
25+
)

example/cosmos/go.sum

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
2+
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
3+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0 h1:fb8kj/Dh4CSwgsOzHeZY4Xh68cFVbzXx+ONXGMY//4w=
4+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0/go.mod h1:uReU2sSxZExRPBAg3qKzmAucSi51+SP1OhohieR821Q=
5+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.0.0 h1:Yoicul8bnVdQrhDMTHxdEckRGX01XvwXDHUT9zYZ3k0=
6+
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6 h1:oBqQLSI1pZwGOdXJAoJJSzmff9tlfD4KroVfjQQmd0g=
7+
github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos v0.3.6/go.mod h1:Beh5cHIXJ0oWEDWk9lNFtuklCojLLQ5hl+LqSNTTs0I=
8+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.0 h1:d81/ng9rET2YqdVkVwkb6EXeRrLJIwyGnJcAlAWKwhs=
9+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.0/go.mod h1:s4kgfzA0covAXNicZHDMN58jExvcng2mC/DepXiF1EI=
10+
github.com/AzureAD/microsoft-authentication-library-for-go v0.4.0 h1:WVsrXCnHlDDX8ls+tootqRE87/hL9S/g4ewig9RsD/c=
11+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
13+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/golang-jwt/jwt v3.2.1+incompatible h1:73Z+4BJcrTC+KczS6WvTPvRGOp1WmfEP4Q1lOd9Z/+c=
15+
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
16+
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
17+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
18+
github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU=
19+
github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=
20+
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
21+
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
22+
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 h1:Qj1ukM4GlMWXNdMBuXcXfz/Kw9s1qm0CLY32QxuSImI=
23+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
24+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
25+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
26+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
27+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
28+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
29+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
30+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
31+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
32+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
33+
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
34+
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
35+
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
36+
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
37+
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
38+
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
39+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
40+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
42+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

example/cosmos/main.go

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// (c) Copyright IBM Corp. 2024
2+
3+
package main
4+
5+
import (
6+
"context"
7+
"encoding/json"
8+
"errors"
9+
"fmt"
10+
"io"
11+
"log"
12+
"net/http"
13+
"os"
14+
15+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16+
"github.com/Azure/azure-sdk-for-go/sdk/data/azcosmos"
17+
"github.com/google/uuid"
18+
instana "github.com/instana/go-sensor"
19+
"github.com/instana/go-sensor/instrumentation/instacosmos"
20+
)
21+
22+
// The following variables are database id and container name created in azure.
23+
// this values needs to be changed to if the database and container are created
24+
// with different names.
25+
const (
26+
database = "trace-data"
27+
container = "spans"
28+
)
29+
30+
// environment variables to be exported
31+
const (
32+
CONNECTION_URL = "COSMOS_CONNECTION_URL"
33+
KEY = "COSMOS_KEY"
34+
)
35+
36+
type SpanType string
37+
38+
const (
39+
EntrySpan SpanType = "entry"
40+
ExitSpan SpanType = "exit"
41+
)
42+
43+
// Span is the item to be inserted in the azure container.
44+
type Span struct {
45+
ID string `json:"id"`
46+
SpanID string `json:"SpanID"`
47+
Type SpanType `json:"type"`
48+
Description string `json:"description"`
49+
}
50+
51+
var (
52+
collector instana.TracerLogger
53+
)
54+
55+
var (
56+
endpoint = ""
57+
key = ""
58+
)
59+
60+
func init() {
61+
validateAzureCreds()
62+
collector = instana.InitCollector(&instana.Options{
63+
Service: "sample-app-cosmos",
64+
})
65+
}
66+
67+
func main() {
68+
http.HandleFunc("/cosmos-test", instana.TracingHandlerFunc(collector, "/cosmos-test", handler))
69+
log.Fatal(http.ListenAndServe("localhost:9990", nil))
70+
}
71+
72+
func handler(w http.ResponseWriter, r *http.Request) {
73+
74+
erStr := r.URL.Query().Get("error")
75+
needError := erStr == "true"
76+
77+
itemResponse, err := cosmosTest(r.Context(), needError)
78+
if err != nil {
79+
var responseErr *azcore.ResponseError
80+
errors.As(err, &responseErr)
81+
defer responseErr.RawResponse.Body.Close()
82+
errBytes, err := io.ReadAll(responseErr.RawResponse.Body)
83+
if err != nil {
84+
log.Fatal("Failed to read error body")
85+
}
86+
log.Println("Error:", string(errBytes))
87+
sendErrResp(w, responseErr.StatusCode)
88+
} else {
89+
sendOkResp(w)
90+
log.Printf("Status %d. ActivityId %s. Consuming %v Request Units.\n",
91+
itemResponse.RawResponse.StatusCode,
92+
itemResponse.ActivityID,
93+
itemResponse.RequestCharge)
94+
}
95+
}
96+
97+
func sendErrResp(w http.ResponseWriter, statusCode int) {
98+
w.WriteHeader(statusCode)
99+
_, _ = w.Write([]byte(`{message:"something went wrong"}`))
100+
}
101+
102+
func sendOkResp(w http.ResponseWriter) {
103+
w.WriteHeader(http.StatusOK)
104+
_, _ = w.Write([]byte("{message:Status OK! Check terminal for full log!}"))
105+
}
106+
107+
func cosmosTest(ctx context.Context, needError bool) (azcosmos.ItemResponse, error) {
108+
109+
// Create a CosmosDB client
110+
client, err := getClient(collector)
111+
if err != nil {
112+
log.Fatal("Failed to create Azure Cosmos DB client: ", err)
113+
}
114+
115+
// Create container client
116+
containerClient, err := client.NewContainer(database, container)
117+
if err != nil {
118+
log.Fatal("Failed to create a container client:", err)
119+
}
120+
121+
id := uuid.New().String()
122+
partitionKey := fmt.Sprintf("span-%s", id)
123+
124+
// Specifies the value of the partition key
125+
pk := containerClient.NewPartitionKeyString(partitionKey)
126+
127+
if needError {
128+
partitionKey = "invalidPartitionKey"
129+
}
130+
131+
span := Span{
132+
ID: id,
133+
SpanID: partitionKey,
134+
Type: EntrySpan,
135+
Description: "sample span",
136+
}
137+
138+
b, err := json.Marshal(span)
139+
if err != nil {
140+
log.Fatal("Failed to marshal span data:", err)
141+
}
142+
143+
// setting item options upon creating ie. consistency level
144+
itemOptions := azcosmos.ItemOptions{
145+
ConsistencyLevel: azcosmos.ConsistencyLevelSession.ToPtr(),
146+
}
147+
148+
itemResponse, err := containerClient.CreateItem(ctx, pk, b, &itemOptions)
149+
return itemResponse, err
150+
}
151+
152+
func getClient(sensor instana.TracerLogger) (instacosmos.Client, error) {
153+
cred, err := instacosmos.NewKeyCredential(key)
154+
if err != nil {
155+
return nil, err
156+
}
157+
158+
client, err := instacosmos.NewClientWithKey(sensor, endpoint, cred, &azcosmos.ClientOptions{})
159+
if err != nil {
160+
return nil, err
161+
}
162+
163+
return client, nil
164+
}
165+
166+
func validateAzureCreds() {
167+
endpoint, _ = os.LookupEnv(CONNECTION_URL)
168+
key, _ = os.LookupEnv(KEY)
169+
170+
if endpoint == "" || key == "" {
171+
log.Fatal("Azure credentials are not provided")
172+
}
173+
}

0 commit comments

Comments
 (0)