Skip to content

Commit

Permalink
Merge branch 'main' into envconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Aneurysm9 committed Mar 3, 2022
2 parents d7bd1bf + 18f4cb8 commit 347b8ce
Show file tree
Hide file tree
Showing 105 changed files with 1,823 additions and 4,410 deletions.
9 changes: 0 additions & 9 deletions .github/dependabot.yml
Expand Up @@ -227,15 +227,6 @@ updates:
schedule:
day: sunday
interval: weekly
- package-ecosystem: gomod
directory: /internal/metric
labels:
- dependencies
- go
- "Skip Changelog"
schedule:
day: sunday
interval: weekly
- package-ecosystem: gomod
directory: /internal/tools
labels:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### ⚠️ Notice ⚠️

This update is a breaking change of the unstable Metrics API. Code instrumented with the `go.opentelemetry.io/otel/metric` <= v0.27.0 will need to be modified.

### Added

- Added support to configure the span limits with environment variables.
Expand All @@ -24,6 +28,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- For tracestate's members, prepend the new element and remove the oldest one, which is over capacity (#2592)
- Add event and link drop counts to the exported data from the `oltptrace` exporter. (#2601)
- The metrics API has been significantly changed. (#2587)
- Unify path cleaning functionally in the `otlpmetric` and `otlptrace` config. (#2639)
- Change the debug message from the `sdk/trace.BatchSpanProcessor` to reflect the count is cumulative. (#2640)
- Introduce new internal envconfig package for OTLP exporters (#2608)

Expand Down
2 changes: 1 addition & 1 deletion bridge/opencensus/aggregation.go
Expand Up @@ -21,8 +21,8 @@ import (

"go.opencensus.io/metric/metricdata"

"go.opentelemetry.io/otel/metric/number"
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
"go.opentelemetry.io/otel/sdk/metric/number"
)

var (
Expand Down
18 changes: 9 additions & 9 deletions bridge/opencensus/exporter.go
Expand Up @@ -27,13 +27,13 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/number"
"go.opentelemetry.io/otel/metric/sdkapi"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/export"
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
"go.opentelemetry.io/otel/sdk/metric/number"
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
"go.opentelemetry.io/otel/sdk/resource"
)

Expand Down Expand Up @@ -170,17 +170,17 @@ func convertDescriptor(ocDescriptor metricdata.Descriptor) (sdkapi.Descriptor, e
// Includes TypeGaugeDistribution, TypeCumulativeDistribution, TypeSummary
return sdkapi.Descriptor{}, fmt.Errorf("%w; descriptor type: %v", errConversion, ocDescriptor.Type)
}
opts := []metric.InstrumentOption{
metric.WithDescription(ocDescriptor.Description),
opts := []instrument.Option{
instrument.WithDescription(ocDescriptor.Description),
}
switch ocDescriptor.Unit {
case metricdata.UnitDimensionless:
opts = append(opts, metric.WithUnit(unit.Dimensionless))
opts = append(opts, instrument.WithUnit(unit.Dimensionless))
case metricdata.UnitBytes:
opts = append(opts, metric.WithUnit(unit.Bytes))
opts = append(opts, instrument.WithUnit(unit.Bytes))
case metricdata.UnitMilliseconds:
opts = append(opts, metric.WithUnit(unit.Milliseconds))
opts = append(opts, instrument.WithUnit(unit.Milliseconds))
}
cfg := metric.NewInstrumentConfig(opts...)
cfg := instrument.NewConfig(opts...)
return sdkapi.NewDescriptor(ocDescriptor.Name, ikind, nkind, cfg.Description(), cfg.Unit()), nil
}
24 changes: 12 additions & 12 deletions bridge/opencensus/exporter_test.go
Expand Up @@ -26,15 +26,15 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/metrictest"
"go.opentelemetry.io/otel/metric/number"
"go.opentelemetry.io/otel/metric/sdkapi"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/metric/controller/controllertest"
"go.opentelemetry.io/otel/sdk/metric/export"
"go.opentelemetry.io/otel/sdk/metric/export/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metrictest"
"go.opentelemetry.io/otel/sdk/metric/number"
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
"go.opentelemetry.io/otel/sdk/resource"
)

Expand Down Expand Up @@ -400,8 +400,8 @@ func TestConvertDescriptor(t *testing.T) {
"foo",
sdkapi.GaugeObserverInstrumentKind,
number.Int64Kind,
metric.WithDescription("bar"),
metric.WithUnit(unit.Bytes),
instrument.WithDescription("bar"),
instrument.WithUnit(unit.Bytes),
),
},
{
Expand All @@ -416,8 +416,8 @@ func TestConvertDescriptor(t *testing.T) {
"foo",
sdkapi.GaugeObserverInstrumentKind,
number.Float64Kind,
metric.WithDescription("bar"),
metric.WithUnit(unit.Milliseconds),
instrument.WithDescription("bar"),
instrument.WithUnit(unit.Milliseconds),
),
},
{
Expand All @@ -432,8 +432,8 @@ func TestConvertDescriptor(t *testing.T) {
"foo",
sdkapi.CounterObserverInstrumentKind,
number.Int64Kind,
metric.WithDescription("bar"),
metric.WithUnit(unit.Dimensionless),
instrument.WithDescription("bar"),
instrument.WithUnit(unit.Dimensionless),
),
},
{
Expand All @@ -448,8 +448,8 @@ func TestConvertDescriptor(t *testing.T) {
"foo",
sdkapi.CounterObserverInstrumentKind,
number.Float64Kind,
metric.WithDescription("bar"),
metric.WithUnit(unit.Dimensionless),
instrument.WithDescription("bar"),
instrument.WithUnit(unit.Dimensionless),
),
},
{
Expand Down
62 changes: 33 additions & 29 deletions example/prometheus/main.go
Expand Up @@ -25,7 +25,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"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"
Expand All @@ -35,6 +35,9 @@ import (

var (
lemonsKey = attribute.Key("ex.com/lemons")

// TODO Bring back Global package
meterProvider metric.MeterProvider
)

func initMeter() {
Expand All @@ -54,7 +57,9 @@ func initMeter() {
if err != nil {
log.Panicf("failed to initialize prometheus exporter %v", err)
}
global.SetMeterProvider(exporter.MeterProvider())
// TODO Bring back Global package
// global.SetMeterProvider(exporter.MeterProvider())
meterProvider = exporter.MeterProvider()

http.HandleFunc("/", exporter.ServeHTTP)
go func() {
Expand All @@ -67,23 +72,33 @@ func initMeter() {
func main() {
initMeter()

meter := global.Meter("ex.com/basic")
// TODO Bring back Global package
// meter := global.Meter("ex.com/basic")
meter := meterProvider.Meter("ex.com/basic")
observerLock := new(sync.RWMutex)
observerValueToReport := new(float64)
observerLabelsToReport := new([]attribute.KeyValue)
cb := func(_ context.Context, result metric.Float64ObserverResult) {

gaugeObserver, err := meter.AsyncFloat64().Gauge("ex.com.one")
if err != nil {
log.Panicf("failed to initialize instrument: %v", err)
}
_ = meter.RegisterCallback([]instrument.Asynchronous{gaugeObserver}, func(ctx context.Context) {
(*observerLock).RLock()
value := *observerValueToReport
labels := *observerLabelsToReport
(*observerLock).RUnlock()
result.Observe(value, labels...)
}
_ = metric.Must(meter).NewFloat64GaugeObserver("ex.com.one", cb,
metric.WithDescription("A GaugeObserver set to 1.0"),
)
gaugeObserver.Observe(ctx, value, labels...)
})

histogram := metric.Must(meter).NewFloat64Histogram("ex.com.two")
counter := metric.Must(meter).NewFloat64Counter("ex.com.three")
histogram, err := meter.SyncFloat64().Histogram("ex.com.two")
if err != nil {
log.Panicf("failed to initialize instrument: %v", err)
}
counter, err := meter.SyncFloat64().Counter("ex.com.three")
if err != nil {
log.Panicf("failed to initialize instrument: %v", err)
}

commonLabels := []attribute.KeyValue{lemonsKey.Int(10), attribute.String("A", "1"), attribute.String("B", "2"), attribute.String("C", "3")}
notSoCommonLabels := []attribute.KeyValue{lemonsKey.Int(13)}
Expand All @@ -94,38 +109,27 @@ func main() {
*observerValueToReport = 1.0
*observerLabelsToReport = commonLabels
(*observerLock).Unlock()
meter.RecordBatch(
ctx,
commonLabels,
histogram.Measurement(2.0),
counter.Measurement(12.0),
)

histogram.Record(ctx, 2.0, commonLabels...)
counter.Add(ctx, 12.0, commonLabels...)

time.Sleep(5 * time.Second)

(*observerLock).Lock()
*observerValueToReport = 1.0
*observerLabelsToReport = notSoCommonLabels
(*observerLock).Unlock()
meter.RecordBatch(
ctx,
notSoCommonLabels,
histogram.Measurement(2.0),
counter.Measurement(22.0),
)
histogram.Record(ctx, 2.0, notSoCommonLabels...)
counter.Add(ctx, 22.0, notSoCommonLabels...)

time.Sleep(5 * time.Second)

(*observerLock).Lock()
*observerValueToReport = 13.0
*observerLabelsToReport = commonLabels
(*observerLock).Unlock()
meter.RecordBatch(
ctx,
commonLabels,
histogram.Measurement(12.0),
counter.Measurement(13.0),
)
histogram.Record(ctx, 12.0, commonLabels...)
counter.Add(ctx, 13.0, commonLabels...)

fmt.Println("Example finished updating, please visit :2222")

Expand Down
Expand Up @@ -12,29 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package global_test
// Package internal contains common functionality for all OTLP exporters.
package internal // import "go.opentelemetry.io/otel/exporters/otlp/internal"

import (
"os"
"testing"

ottest "go.opentelemetry.io/otel/internal/internaltest"
"go.opentelemetry.io/otel/internal/metric/global"
"fmt"
"path"
"strings"
)

// Ensure struct alignment prior to running tests.
func TestMain(m *testing.M) {
fieldsMap := global.AtomicFieldOffsets()
fields := make([]ottest.FieldOffset, 0, len(fieldsMap))
for name, offset := range fieldsMap {
fields = append(fields, ottest.FieldOffset{
Name: name,
Offset: offset,
})
// CleanPath returns a path with all spaces trimmed and all redundancies removed. If urlPath is empty or cleaning it results in an empty string, defaultPath is returned instead.
func CleanPath(urlPath string, defaultPath string) string {
tmp := path.Clean(strings.TrimSpace(urlPath))
if tmp == "." {
return defaultPath
}
if !ottest.Aligned8Byte(fields, os.Stderr) {
os.Exit(1)
if !path.IsAbs(tmp) {
tmp = fmt.Sprintf("/%s", tmp)
}

os.Exit(m.Run())
return tmp
}
83 changes: 83 additions & 0 deletions exporters/otlp/internal/config_test.go
@@ -0,0 +1,83 @@
// 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 internal

import "testing"

func TestCleanPath(t *testing.T) {
type args struct {
urlPath string
defaultPath string
}
tests := []struct {
name string
args args
want string
}{
{
name: "clean empty path",
args: args{
urlPath: "",
defaultPath: "DefaultPath",
},
want: "DefaultPath",
},
{
name: "clean metrics path",
args: args{
urlPath: "/prefix/v1/metrics",
defaultPath: "DefaultMetricsPath",
},
want: "/prefix/v1/metrics",
},
{
name: "clean traces path",
args: args{
urlPath: "https://env_endpoint",
defaultPath: "DefaultTracesPath",
},
want: "/https:/env_endpoint",
},
{
name: "spaces trimmed",
args: args{
urlPath: " /dir",
},
want: "/dir",
},
{
name: "clean path empty",
args: args{
urlPath: "dir/..",
defaultPath: "DefaultTracesPath",
},
want: "DefaultTracesPath",
},
{
name: "make absolute",
args: args{
urlPath: "dir/a",
},
want: "/dir/a",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CleanPath(tt.args.urlPath, tt.args.defaultPath); got != tt.want {
t.Errorf("CleanPath() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 347b8ce

Please sign in to comment.