<commit_before>// Copyright 2015 The Cockroach 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.
//
// Author: <NAME> (<EMAIL>)

package tracing

import (
	"fmt"
	"os"

	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/standardtracer"
	"golang.org/x/net/context"
)

const traceTimeFormat = "15:04:05.000000"

type testRecorder struct{}

func (testRecorder) RecordSpan(sp *standardtracer.RawSpan) {
	fmt.Fprintf(os.Stderr, "[Trace %s]\n", sp.Operation)
	for _, log := range sp.Logs {
		fmt.Fprintln(os.Stderr, " * ", log.Timestamp.Format(traceTimeFormat), log.Event)
	}
}

type noopRecorder struct{}

func (noopRecorder) RecordSpan(sp *standardtracer.RawSpan) {}

var recorder standardtracer.SpanRecorder = noopRecorder{}

// SetTestTracing sets up subsequently created tracers returned by
// NewTracer() so that they record their traces to stderr.
// Not to be called concurrently with any tracer operations; the
// right place is init() in main_test.go.
func SetTestTracing() {
	recorder = testRecorder{}
}

// Disable changes the environment so that all newly created tracers are
// trivial (i.e. noop tracers). This is a crutch to turn off tracing during
// benchmarks until future iterations come with a tunable probabilistic tracing
// setting. Concurrent calls are not safe. Prescribed use is:
//
// func BenchmarkSomething(b *testing.B) {
//   defer tracing.Disable()() // note the double ()()
// }<commit_msg>Move tracer output back under `log.V(2)`<commit_after>// Copyright 2015 The Cockroach 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.
//
// Author: <NAME> (<EMAIL>)

package tracing

import (
	"bytes"
	"fmt"

	"github.com/cockroachdb/cockroach/util/log"
	"github.com/opentracing/opentracing-go"
	"github.com/opentracing/opentracing-go/standardtracer"
	"golang.org/x/net/context"
)

const traceTimeFormat = "15:04:05.000000"

type testRecorder struct{}

func (testRecorder) RecordSpan(sp *standardtracer.RawSpan) {
	if log.V(2) {
		var buf bytes.Buffer
		fmt.Fprintf(&buf, "[Trace %s]", sp.Operation)
		for _, log := range sp.Logs {
			fmt.Fprint(&buf, "\n * ", log.Timestamp.Format(traceTimeFormat), log.Event)
		}
		log.Info(buf.String())
	}
}

type noopRecorder struct{}

func (noopRecorder) RecordSpan(sp *standardtracer.RawSpan) {}

var recorder standardtracer.SpanRecorder = noopRecorder{}

// SetTestTracing sets up subsequently created tracers returned by
// NewTracer() so that they record their traces to stderr.
// Not to be called concurrently with any tracer operations; the
// right place is init() in main_test.go.
func SetTestTracing() {
	recorder = testRecorder{}
}

// Disable changes the environment so that all newly created tracers are
// trivial (i.e. noop tracers). This is a crutch to turn off tracing during
// benchmarks until future iterations come with a tunable probabilistic tracing
// setting. Concurrent calls are not safe. Prescribed use is:
//
// func BenchmarkSomething(b *testing.B) {
//   defer tracing.Disable()() // note the double ()()
// }
{'fraction_non_alphanumeric': 0.11321683485109525, 'fraction_numerical': 0.010337189269013045, 'mean_word_length': 3.9320388349514563, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1423200', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1207, 'n_words': 443}