Skip to content

Commit

Permalink
Merge branch 'main' into aws_ecs_resource_attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Michele Mancioppi authored Sep 18, 2022
2 parents ef483b1 + 2a588c2 commit 2fe086b
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"go.opentelemetry.io/contrib/propagators/b3"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/metric/metrictest"
"go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
Expand All @@ -41,6 +40,11 @@ import (
"github.com/stretchr/testify/require"
)

// TODO(#2762): Add metric integration tests for the instrumentation. These
// tests depend on
// https://github.com/open-telemetry/opentelemetry-go/issues/3031 being
// resolved.

// ------------------------------------------ Test Controller

const defaultReply = "hello world"
Expand Down Expand Up @@ -189,7 +193,6 @@ func TestStatic(t *testing.T) {
defer replaceBeego()
sr := tracetest.NewSpanRecorder()
tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
meterProvider, metricExporter := metrictest.NewTestMeterProvider()
file, err := os.CreateTemp("", "static-*.html")
require.NoError(t, err)
defer file.Close()
Expand All @@ -202,7 +205,6 @@ func TestStatic(t *testing.T) {

mw := otelbeego.NewOTelBeegoMiddleWare(middleWareName,
otelbeego.WithTracerProvider(tracerProvider),
otelbeego.WithMeterProvider(meterProvider),
)

rr := httptest.NewRecorder()
Expand All @@ -221,7 +223,6 @@ func TestStatic(t *testing.T) {
spans := sr.Ended()
require.Len(t, spans, 1)
assertSpan(t, spans[0], tc)
assertMetrics(t, metricExporter.GetRecords(), tc)
}

func TestRender(t *testing.T) {
Expand Down Expand Up @@ -287,7 +288,6 @@ func TestRender(t *testing.T) {
func runTest(t *testing.T, tc *testCase, url string) {
sr := tracetest.NewSpanRecorder()
tracerProvider := trace.NewTracerProvider(trace.WithSpanProcessor(sr))
meterProvider, metricExporter := metrictest.NewTestMeterProvider()
addTestRoutes(t)
defer replaceBeego()

Expand All @@ -306,7 +306,6 @@ func runTest(t *testing.T, tc *testCase, url string) {
append(
tc.options,
otelbeego.WithTracerProvider(tracerProvider),
otelbeego.WithMeterProvider(meterProvider),
)...,
)

Expand All @@ -326,7 +325,6 @@ func runTest(t *testing.T, tc *testCase, url string) {
} else {
require.Len(t, spans, 0)
}
assertMetrics(t, metricExporter.GetRecords(), tc)
}

func defaultAttributes() []attribute.KeyValue {
Expand All @@ -345,14 +343,6 @@ func assertSpan(t *testing.T, span trace.ReadOnlySpan, tc *testCase) {
}
}

func assertMetrics(t *testing.T, records []metrictest.ExportRecord, tc *testCase) {
for _, record := range records {
for _, att := range tc.expectedAttributes {
require.Contains(t, record.Attributes, att)
}
}
}

// ------------------------------------------ Test Cases

const middleWareName = "test-router"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
go.opentelemetry.io/contrib/propagators/b3 v1.10.0
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/sdk v1.10.0
go.opentelemetry.io/otel/sdk/metric v0.31.0
)

require (
Expand All @@ -29,7 +28,7 @@ require (
github.com/prometheus/procfs v0.1.3 // indirect
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.35.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ github.com/astaxie/beego v1.12.3 h1:SAQkdD2ePye+v8Gn1r4X6IKZM1wd28EyUOVQ3PDSOOQ=
github.com/astaxie/beego v1.12.3/go.mod h1:p3qIm0Ryx7zeBHLljmd7omloyca1s4yu1a8kM1FkpIA=
github.com/beego/goyaml2 v0.0.0-20130207012346-5545475820dd/go.mod h1:1b+Y/CofkYwXMUU0OhQqGvsY2Bvgr4j6jfT699wyZKQ=
github.com/beego/x2j v0.0.0-20131220205130-a0352aadc542/go.mod h1:kSeGC/p1AbBiEp5kat81+DSQrZenVBZXklMLaELspWU=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -151,12 +150,10 @@ github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqI
github.com/yuin/gopher-lua v0.0.0-20171031051903-609c9cd26973/go.mod h1:aEV29XrmTYFr3CiRxZeGHpkvbwq+prZduBqMaascyCU=
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
go.opentelemetry.io/otel/metric v0.31.0 h1:6SiklT+gfWAwWUR0meEMxQBtihpiEs4c+vL9spDTqUs=
go.opentelemetry.io/otel/metric v0.31.0/go.mod h1:ohmwj9KTSIeBnDBm/ZwH2PSZxZzoOaG2xZeekTRzL5A=
go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9 h1:Ks2fKHtMzWUGr/faEwyAHfeU0J5uHzDpysl6x2hmRRw=
go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9/go.mod h1:PVDNTt297p8ehm949jsIzd+Z2bIZJYQQG/uuHTeWFHY=
go.opentelemetry.io/otel/sdk v1.10.0 h1:jZ6K7sVn04kk/3DNUdJ4mqRlGDiXAVuIG+MMENpTNdY=
go.opentelemetry.io/otel/sdk v1.10.0/go.mod h1:vO06iKzD5baltJz1zarxMCNHFpUlUiOy4s65ECtn6kE=
go.opentelemetry.io/otel/sdk/metric v0.31.0 h1:2sZx4R43ZMhJdteKAlKoHvRgrMp53V1aRxvEf5lCq8Q=
go.opentelemetry.io/otel/sdk/metric v0.31.0/go.mod h1:fl0SmNnX9mN9xgU6OLYLMBMrNAsaZQi7qBwprwO3abk=
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
131 changes: 93 additions & 38 deletions instrumentation/github.com/gocql/gocql/otelgocql/example/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.18
// +build go1.18

package main

// This example will create the keyspace
// "gocql_integration_example" and a single table
// with the following schema:
Expand All @@ -24,8 +29,6 @@
// The example will insert fictional books into the database and
// then truncate the table.

package main

import (
"context"
"fmt"
Expand All @@ -37,16 +40,16 @@ import (
"time"

"github.com/gocql/gocql"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/prometheus"
zipkintrace "go.opentelemetry.io/otel/exporters/zipkin"
otelprom "go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/exporters/zipkin"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/view"
"go.opentelemetry.io/otel/sdk/trace"

"go.opentelemetry.io/contrib/instrumentation/github.com/gocql/gocql/otelgocql"
Expand Down Expand Up @@ -124,51 +127,103 @@ func main() {
wg.Wait()
}

func initMetrics() error {
// Start prometheus
cont := controller.New(
processor.NewFactory(
simple.NewWithHistogramDistribution(
histogram.WithExplicitBoundaries([]float64{0.001, 0.01, 0.1, 0.5, 1, 2, 5, 10}),
),
aggregation.CumulativeTemporalitySelector(),
processor.WithMemory(true),
),
func views() ([]view.View, error) {
var vs []view.View
// TODO: Remove renames when the Prometheus exporter natively supports
// metric instrument name sanitation
// (https://github.com/open-telemetry/opentelemetry-go/issues/3183).
v, err := view.New(
view.MatchInstrumentName("db.cassandra.queries"),
view.WithRename("db_cassandra_queries"),
)
if err != nil {
return nil, err
}
vs = append(vs, v)

v, err = view.New(
view.MatchInstrumentName("db.cassandra.rows"),
view.WithRename("db_cassandra_rows"),
view.WithSetAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.01, 0.1, 0.5, 1, 2, 5, 10},
}),
)
if err != nil {
return nil, err
}
vs = append(vs, v)

v, err = view.New(
view.MatchInstrumentName("db.cassandra.batch.queries"),
view.WithRename("db_cassandra_batch_queries"),
)
if err != nil {
return nil, err
}
vs = append(vs, v)

v, err = view.New(
view.MatchInstrumentName("db.cassandra.connections"),
view.WithRename("db_cassandra_connections"),
)
metricExporter, err := prometheus.New(prometheus.Config{}, cont)
if err != nil {
return nil, err
}
vs = append(vs, v)

v, err = view.New(
view.MatchInstrumentName("db.cassandra.latency"),
view.WithRename("db_cassandra_latency"),
view.WithSetAggregation(aggregation.ExplicitBucketHistogram{
Boundaries: []float64{0.001, 0.01, 0.1, 0.5, 1, 2, 5, 10},
}),
)
if err != nil {
return nil, err
}
vs = append(vs, v)

return vs, nil
}

func initMetrics() error {
vs, err := views()
if err != nil {
return err
}
global.SetMeterProvider(metricExporter.MeterProvider())

server := http.Server{Addr: ":2222"}
http.HandleFunc("/", metricExporter.ServeHTTP)
exporter := otelprom.New()
provider := metric.NewMeterProvider(metric.WithReader(exporter, vs...))
global.SetMeterProvider(provider)

err = prometheus.Register(exporter.Collector)
if err != nil {
return err
}
http.Handle("/", promhttp.Handler())
log.Print("Serving metrics at :2222/")
go func() {
defer wg.Done()
wg.Add(1)
log.Print(server.ListenAndServe())
err := http.ListenAndServe(":2222", nil)
if err != nil {
log.Print(err)
}
}()

// ctrl+c will stop the server gracefully
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt)
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
wg.Add(1)
go func() {
<-shutdown
if err := server.Shutdown(context.Background()); err != nil {
log.Printf("problem shutting down server, %v", err)
} else {
log.Print("gracefully shutting down server")
}
err := cont.Stop(context.Background())
defer wg.Done()
<-ctx.Done()
err := provider.Shutdown(context.Background())
if err != nil {
log.Printf("error stopping metric controller: %s", err)
log.Printf("error stopping MeterProvider: %s", err)
}
}()
return nil
}

func initTracer() (*trace.TracerProvider, error) {
exporter, err := zipkintrace.New("http://localhost:9411/api/v2/spans")
exporter, err := zipkin.New("http://localhost:9411/api/v2/spans")
if err != nil {
return nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions instrumentation/github.com/gocql/gocql/otelgocql/example/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package main provides an example use of the otelgocql instrumentation.
package main
18 changes: 9 additions & 9 deletions instrumentation/github.com/gocql/gocql/otelgocql/example/go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module go.opentelemetry.io/contrib/instrumentation/github.com/gocql/gocql/otelgocql/example

go 1.17
go 1.18

replace go.opentelemetry.io/contrib/instrumentation/github.com/gocql/gocql/otelgocql => ../

require (
github.com/gocql/gocql v0.0.0-20200624222514-34081eda590e
github.com/prometheus/client_golang v1.13.0
go.opentelemetry.io/contrib/instrumentation/github.com/gocql/gocql/otelgocql v0.35.0
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/exporters/prometheus v0.31.0
go.opentelemetry.io/otel/exporters/prometheus v0.31.1-0.20220916150821-e1a1f07e44e9
go.opentelemetry.io/otel/exporters/zipkin v1.10.0
go.opentelemetry.io/otel/metric v0.31.0
go.opentelemetry.io/otel/metric v0.31.1-0.20220916150821-e1a1f07e44e9
go.opentelemetry.io/otel/sdk v1.10.0
go.opentelemetry.io/otel/sdk/metric v0.31.0
go.opentelemetry.io/otel/sdk/metric v0.31.1-0.20220916150821-e1a1f07e44e9
)

require (
Expand All @@ -25,12 +26,11 @@ require (
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/openzipkin/zipkin-go v0.4.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
google.golang.org/protobuf v1.27.1 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
)
Loading

0 comments on commit 2fe086b

Please sign in to comment.